From db322425d20c8acac852e914f7b6173505087d1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BB=AF=E4=B8=80?= Date: Wed, 6 Jan 2016 11:56:16 +0800 Subject: [PATCH 1/3] [feat] add onRowClick --- examples/rowClick.html | 0 examples/rowClick.js | 87 ++++++++++++++++++++++++++++++++++++++++++ src/Table.jsx | 3 ++ src/TableRow.jsx | 3 +- 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 examples/rowClick.html create mode 100644 examples/rowClick.js diff --git a/examples/rowClick.html b/examples/rowClick.html new file mode 100644 index 000000000..e69de29bb diff --git a/examples/rowClick.js b/examples/rowClick.js new file mode 100644 index 000000000..6f9424dda --- /dev/null +++ b/examples/rowClick.js @@ -0,0 +1,87 @@ +/* eslint react/no-multi-comp: 0*/ +const React = require('react'); +const ReactDOM = require('react-dom'); +const Table = require('rc-table'); +require('rc-table/assets/index.less'); + +const onRowClick = function(record, index) { + alert(`u click the nth(${index}) element of yourFather.children, record.name: ${record.name}`); +}; + +const onOperationClick = function(text, record) { + alert(`u click ${text}, record.name is ${record.name}`); +}; + +const columns = [{ + title: '姓名', + dataIndex: 'name', + key: 'name', + width: 400, +}, { + title: '年龄', + dataIndex: 'age', + key: 'age', + width: 100, + render: (text, record) => Alert: {text}, click will pop to row click, +}, { + title: '住址', + dataIndex: 'address', + key: 'address', + width: 200, +}]; + +const data = [{ + key: 1, + name: 'a', + age: 32, + address: '我是a', + children: [{ + key: 11, + name: 'aa', + age: 33, + address: '我是aa', + }, { + key: 12, + name: 'ab', + age: 33, + address: '我是ab', + children: [{ + key: 121, + name: 'aba', + age: 33, + address: '我是aba', + }], + }, { + key: 13, + name: 'ac', + age: 33, + address: '我是ac', + children: [{ + key: 131, + name: 'aca', + age: 33, + address: '我是aca', + children: [{ + key: 1311, + name: 'acaa', + age: 33, + address: '我是acaa', + }, { + key: 1312, + name: 'acab', + age: 33, + address: '我是acab', + }], + }], + }], +}, { + key: 2, + name: 'b', + age: 32, + address: '我是b', +}]; + +ReactDOM.render( + , + document.getElementById('__react-content') +); diff --git a/src/Table.jsx b/src/Table.jsx index a73a212b7..79544f74c 100644 --- a/src/Table.jsx +++ b/src/Table.jsx @@ -18,6 +18,7 @@ const Table = React.createClass({ childrenColumnName: React.PropTypes.string, onExpandedRowsChange: React.PropTypes.func, indentSize: React.PropTypes.number, + onRowClick: React.PropTypes.func, }, getDefaultProps() { @@ -149,6 +150,7 @@ const Table = React.createClass({ const expandedRowClassName = props.expandedRowClassName; const needIndentSpaced = props.data.some(record => record[childrenColumnName] && record[childrenColumnName].length > 0); + const onRowClick = props.onRowClick; for (let i = 0; i < data.length; i++) { const record = data[i]; const key = keyFn ? keyFn(record, i) : undefined; @@ -175,6 +177,7 @@ const Table = React.createClass({ prefixCls={`${props.prefixCls}-row`} childrenColumnName={childrenColumnName} columns={columns} + onRowClick={onRowClick} key={key}/>); const subVisible = visible && isRowExpanded; diff --git a/src/TableRow.jsx b/src/TableRow.jsx index 8dafd96b0..b3df807fe 100644 --- a/src/TableRow.jsx +++ b/src/TableRow.jsx @@ -24,6 +24,7 @@ const TableRow = React.createClass({ const indent = props.indent; const indentSize = props.indentSize; const needIndentSpaced = props.needIndentSpaced; + const onRowClick = props.onRowClick; for (let i = 0; i < columns.length; i++) { const col = columns[i]; @@ -79,7 +80,7 @@ const TableRow = React.createClass({ } } return ( - {cells}); + {cells}); }, }); From 0c12d0c48f22fd60e116f2efc5ed439df882ca1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BB=AF=E4=B8=80?= Date: Wed, 6 Jan 2016 12:57:20 +0800 Subject: [PATCH 2/3] [fix] onClick={null} if !onRowClick --- src/TableRow.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TableRow.jsx b/src/TableRow.jsx index b3df807fe..f825612c9 100644 --- a/src/TableRow.jsx +++ b/src/TableRow.jsx @@ -80,7 +80,7 @@ const TableRow = React.createClass({ } } return ( - {cells}); + {cells}); }, }); From ddd516a1d18d9f792eba350e97222b245be47a67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BB=AF=E4=B8=80?= Date: Wed, 6 Jan 2016 14:13:48 +0800 Subject: [PATCH 3/3] [update] bind(null, record, index) --- src/TableRow.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TableRow.jsx b/src/TableRow.jsx index f825612c9..b35b6c2c7 100644 --- a/src/TableRow.jsx +++ b/src/TableRow.jsx @@ -80,7 +80,7 @@ const TableRow = React.createClass({ } } return ( - {cells}); + {cells}); }, });