React中使用Ant Table組件

1、Ant Design of React

  http://ant.design/docs/react/introducejavascript

2、創建webpack工程

    webpack+react demo下載css

    項目的啓動,參考html

3、簡單配置

  1.工程下載下來以後,在src目錄下新建目錄「table」,新建app.js,內容以下。java

import React from 'react';
import ReactDOM from 'react-dom';
import ExampleTable from './ExampleTable';
import 'antd/dist/antd.css';
ReactDOM.render
(
   <ExampleTable />,
   document.body
);

  注:記住引入antd.css, 不然Table組件沒法正常顯示。react

  2.新建ExampleTable.js, 內容以下。webpack

import { Table } from 'antd';
import React from 'react';

class ExampleTable extends React.Component {
  constructor(props) {
      super(props);
      this.showCurRowMessage = this.showCurRowMessage.bind(this);
  }
  componentDidMount() {
    
  }

  //展現當前行信息
  showCurRowMessage(record){
    alert("key:"+record.key + " name:"+record.name + " age:" + record.age + " address:" + record.address + " description:" + record.description);
  }

  render() {
    let self = this;

    const columns = [
      { title: '姓名', dataIndex: 'name', key: 'name' },
      { title: '年齡', dataIndex: 'age', key: 'age', render: (text, record, index) => (Math.floor(record.age/10))*10+"多歲"},
      { title: '住址', dataIndex: 'address', key: 'address' },
      { title: '描述', dataIndex: 'description', key: 'description' },
      { title: '操做', dataIndex: '', key: 'operation', render: function(text, record, index) {
          alert(text.operation + " " + record.operation)
          return <a href="#" name="delete" onClick={function() { self.showCurRowMessage(record)} } >信息</a>; } },
      //精簡寫法
      //{ title: '操做', dataIndex: '', key: 'operation', render: (text, record, index) => <a href="#" name="delete" onClick={() => self.showCurRowMessage(record)}>信息</a> },
    ];

    const data = [
        { key: 1, name: 'hyw', age: 32, address: '西湖區湖底公園1號', description: '我是hyw,今年32歲,住在西湖區湖底公園1號。', children:[
          { key: 1.1, name: 'fas', age: 32, address: '西湖區湖底公園1號', description: '我是fas,今年32歲,住在西湖區湖底公園1號。' },
          { key: 1.2, name: 'wyz', age: 42, address: '西湖區湖底公園2號', description: '我是wyz,今年42歲,住在西湖區湖底公園2號。' },
          { key: 1.3, name: 'ldz', age: 32, address: '西湖區湖底公園3號', description: '我是ldz,今年32歲,住在西湖區湖底公園3號。' },
        ]},
        { key: 2, name: 'lkx', age: 32, address: '西湖區湖底公園1號', description: '我是lkx,今年32歲,住在西湖區湖底公園1號。' },
        { key: 3, name: 'mnk', age: 42, address: '西湖區湖底公園2號', description: '我是mnk,今年42歲,住在西湖區湖底公園2號。' },
        { key: 4, name: 'xyt', age: 32, address: '西湖區湖底公園3號', description: '我是xyt,今年32歲,住在西湖區湖底公園3號。' },
      ];

    const rowSelection = {
      onChange(selectedRowKeys, selectedRows) {
        console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
      },
      onSelect(record, selected, selectedRows) {
        console.log(record, selected, selectedRows);
      },
      onSelectAll(selected, selectedRows, changeRows) {
        console.log(selected, selectedRows, changeRows);
      },
    };

    return (
      <div>
        <Table columns={columns} 
          rowSelection={rowSelection}
          dataSource={data}
          className="table"
        />
      </div>
    );

  }
}

module.exports = ExampleTable;

  3.修改入口地址git

  打開webpack.config.js,修改entry配置。github

  entry: [
      'webpack/hot/only-dev-server',
      "./src/table/app.js"
    ],

4、Table組件屬性

1.Table

參數 說明 類型 默認值
rowSelection 列表項是否可選擇,配置項 Object null
pagination 分頁器,配置項參考 pagination,設爲 false 時不顯示分頁 Object  
size 正常或迷你類型,default or small String default
dataSource 數據數組 Array  
columns 表格列的配置描述,具體項見下表 Array -
rowKey 表格行 key 的取值,能夠是字符串或一個函數 String or Function(record, index):string 'key'
rowClassName 表格行的類名 Function(record, index):string -
expandedRowRender 額外的展開行 Function -
defaultExpandedRowKeys 默認展開的行 Array -
expandedRowKeys 展開的行,控制屬性 Array -
onChange 分頁、排序、篩選變化時觸發 Function(pagination, filters, sorter)  
loading 頁面是否加載中 Boolean false
locale 默認文案設置,目前包括排序、過濾、空數據文案 Object filterConfirm: '肯定' 
filterReset: '重置' 
emptyText: '暫無數據' 
默認值
indentSize 展現樹形數據時,每層縮進的寬度,以 px 爲單位 Number 15
onRowClick 處理行點擊事件 Function(record, index) -
useFixedHeader 是否固定表頭 Boolean false
bordered 是否展現外邊框和列邊框 Boolean false
showHeader 是否顯示錶頭 Boolean true
footer 表格底部自定義渲染函數 Function(currentPageData)  
scroll 橫向或縱向支持滾動,也可用於指定滾動區域的寬高度:{{ x: true, y: 300 }} Object -

2.Column

  列描述數據對象,是 columns 中的一項。web

參數 說明 類型 默認值
title 列頭顯示文字 String or React.Element -
key React 須要的 key,建議設置 String -
dataIndex 列數據在數據項中對應的 key,支持 a.b.c 的嵌套寫法 String -
render 生成複雜數據的渲染函數,參數分別爲當前行的值,當前行數據,行索引,@return裏面能夠設置表格行/列合併 Function(text, record, index) {} -
filters 表頭的篩選菜單項 Array -
onFilter 本地模式下,肯定篩選的運行函數 Function -
filterMultiple 是否多選 Boolean true
filterDropdown 能夠自定義篩選菜單,此函數只負責渲染圖層,須要自行編寫各類交互 React.Element -
sorter 排序函數,本地排序使用一個函數,須要服務端排序可設爲 true Function or Boolean -
colSpan 表頭列合併,設置爲 0 時,不渲染 Number  
width 列寬度 String or Number -
className 列的 className String -
fixed 列是否固定,可選 true(等效於 left) 'left' 'right' Boolean or String false
filteredValue 篩選的受控屬性,外界可用此控制列的篩選狀態,值爲已篩選的 value 數組 Array -
sortOrder 排序的受控屬性,外界可用此控制列的排序,可設置爲 'ascend''descend' false Boolean or String -

3.rowSelection

  選擇功能的配置。數組

參數 說明 類型 默認值
type 多選/單選,checkbox or radio String checkbox
selectedRowKeys 指定選中項的 key 數組,須要和 onChange 進行配合 Array []
onChange 選中項發生變化的時的回調 Function(selectedRowKeys, selectedRows) -
getCheckboxProps 選擇框的默認屬性配置 Function(record) -
onSelect 用戶手動選擇/取消選擇某列的回調 Function(record, selected, selectedRows) -
onSelectAll 用戶手動選擇/取消選擇全部列的回調 Function(selected, selectedRows, changeRows) -

5、數據獲取

  開始使用table組件時,不知道如何獲取這一行的數據,第一種方法是配置rowSelection,在onSelect函數被調用的時候,能夠獲取當前行以及其子行的數據。第二種方法是配置Column中的render屬性,這個屬性對應一個函數,fun(text, record, index){}, 這是個渲染函數,參數分別爲當前行的值,當前行數據,行索引,return能夠決定表格裏最終存放的值。

  本例中,表格中「操做」這一列就是經過render渲染實現,render時咱們能夠獲取到當前行數據的引用record,併爲這一列的每一個表格的內容綁定了點擊事件,點擊以後alert當前行的數據。效果以下圖所示。

6、博客新增標籤

 

  博客中新增了「打賞」標籤,就在右下方。前幾天看見一個博客有這樣的「打賞」標籤,因而模仿着作了一個,能夠自行配置。因爲是今天完成的,就在這裏簡單的介紹一下如何在本身的博客裏引用這個功能。

  進入本身的博客, 依次點擊「管理」, 設置。在「頁首Html代碼」中加入下面的引用。

<!-- 掃碼打賞 -->
<script type="text/javascript">window.reward_config={align: "right", top: "50%", animate: true, alipay: "http://images.cnblogs.com/cnblogs_com/hujunzheng/855447/o_alipay.jpg", webChat: "http://images.cnblogs.com/cnblogs_com/hujunzheng/855447/o_webChat.png"};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://files.cnblogs.com/files/hujunzheng/my_reward.js'];</script>

  注:注意支付方式要改爲本身的,引用的my_reward.js能夠下載到本地,而後存放到本身博客的文件管理中。

  更詳細的介紹請參考「打賞」標籤中的「瞭解更多」, 或者訪問 打賞demo 

相關文章
相關標籤/搜索