Usage

let column = [{
  title: 'ID',
  key: 'id',
  dataIndex: 'id',
  sortBy: 'number',
  filter: [{
    name: 'id大于等于4',
    key: '1',
    filter: function(item) {
      if (item.id >= 4)
        return true;
    }
  }, {
    name: 'id小于4',
    key: '2',
    filter: function(item) {
      if (item.id < 4)
        return true;
    }
  }]
}, {
  title: 'Category',
  key: 'category',
  dataIndex: 'category',
  sortBy: 'string'
}, {
  title: 'Flavor',
  key: 'flavor',
  dataIndex: 'flavor',
  sortBy: 'string'
}, {
  title: 'Level',
  key: 'level',
  dataIndex: 'level',
  sortBy: function(item1, item2) {
    var weight = ['First Level', 'Second Level', 'Third Level', 'Fourth Level'];
    if (weight.indexOf(item1.level) > weight.indexOf(item2.level)) {
      return 1;
    } else if (weight.indexOf(item1.level) < weight.indexOf(item2.level)) {
      return -1;
    } else {
      return 0;
    }
  }
}, {
  title: 'CPU',
  key: 'cpu',
  dataIndex: 'cpu',
  sortBy: 'number'
}, {
  title: 'Price',
  key: 'price',
  dataIndex: 'price',
  sortBy: 'number'
}, {
  title: 'Double Price',
  key: 'double_price',
  sortBy: function(item1, item2) {
    if (item1.price * 2 > item2.price * 2) {
      return 1;
    } else if (item1.price * 2 < item2.price * 2) {
      return -1;
    } else {
      return 0;
    }
  },
  render: function(col, item, index) {
    return <div style=>{item.price * 2}</div>;
  }
}, {
  title: 'Data Print',
  key: 'print',
  printData: function(col, item, e) {
    console.log('event:', e, 'GET COLUMN:', col, ' DATA:', item);
  },
  render: function(col, item, index) {
    if (item.id > 4) {
      return <div>Printing Disabled</div>
    } else {
      return <div style= 
        onClick={this.printData.bind(this, col, item)}>{'Print ID ' + item.id}</div>
    }
  }
}];

let data = [{
  id: 1,
  category: 'Micro-1',
  flavor: 'Micro',
  level: 'First Level',
  cpu: '1',
  price: '0.056'
}, {
  id: 2,
  category: 'Standard-3',
  flavor: 'Standard',
  level: 'Second Level',
  cpu: '3',
  price: '0.444'
}, {
  id: 3,
  category: 'Micro-2',
  flavor: 'Micro',
  level: 'Third Level',
  cpu: '5',
  price: '0.056'
}, {
  id: 4,
  category: 'Standard-2',
  flavor: 'Standard',
  level: 'Fourth Level',
  cpu: '4',
  price: '0.444'
}, {
  id: 5,
  category: 'Micro-3',
  flavor: 'Micro',
  level: 'Second Level',
  cpu: '1',
  price: '0.056'
}, {
  id: 6,
  category: 'Standard-1',
  flavor: 'Standard',
  level: 'Third Level',
  cpu: '7',
  price: '0.444'
}];

function checkboxOnClick(e, status, checkedData, data) {
  console.debug('click triggered!', status, checkedData, data);
}

function checkboxInitialize(item) {
  return item.level.localeCompare('Second Level') ? false : true;
}

ReactDOM.render(
  <Table 
    column={column}
    data={data}
    dataKey="id"
    checkbox={true}
    checkboxInitialize={checkboxInitialize}
    checkboxOnClick={checkboxOnClick}
    width={900}
    striped={true}
    hover={true} />,
  document
);

API

name type default description
items Array 见下方items详解
size String 设置size="small"为small样式,否则为默认样式

items

name type default description
column Array 见下方column详解
data Array 需要展示在table的数据信息
dataKey(必填) String data的唯一key值的列,例如每个data元素中的id值
checkbox Boolean 是否附带checkbox
checkboxInitialize Funtion table初始化时指定选中的row
checkboxOnchange Function table中的checkbox被点击时返回信息Function(e, status, checkedData, data),依次为被点击的DOM节点,被点击的状态,被点击的data元素信息,所有以选中的数据集合
width Number table的宽度
striped Boolean false 是否带striped样式
hover Boolean false 是否带hover样式

column

name type default description
title String column的title
key String 每个column的唯一key值
dataIndex String 列数据在数据项中对应的key
render Function 若想自定义列数据的样式或列数据的数据显示,设置render: function(col, item, index),并返回DOM对象或String
sortBy String 不设置则没有sort功能,若需要分类,sortBy可选值为number,boolean,date和string。若想自定义排序功能,sortBy: function(item1, item2),返回值为1,0或-1
Filter Array 见下方filter详解

column: { filter: }

name type default description
name String filter的title
key String 每个filter的唯一key值
filter Function filter: function(item)返回true或false,表示是否符合筛选条件

Copyright © 2017 MIT License