[Fixbug]使用rowKey的表格,行拖拽无法使用的问题

This commit is contained in:
yangchch6 2019-05-22 18:20:13 +08:00
parent f0a05d327a
commit 503ad33ada
10 changed files with 96 additions and 29 deletions

View File

@ -112,7 +112,8 @@ var propTypes = {
tabIndex: _propTypes2["default"].string, tabIndex: _propTypes2["default"].string,
hoverContent: _propTypes2["default"].func, hoverContent: _propTypes2["default"].func,
size: _propTypes2["default"].oneOf(['sm', 'md', 'lg']), size: _propTypes2["default"].oneOf(['sm', 'md', 'lg']),
rowDraggAble: _propTypes2["default"].bool rowDraggAble: _propTypes2["default"].bool,
onDropRow: _propTypes2["default"].func
}; };
var defaultProps = { var defaultProps = {
@ -157,7 +158,8 @@ var defaultProps = {
tabIndex: '0', tabIndex: '0',
heightConsistent: false, heightConsistent: false,
size: 'md', size: 'md',
rowDraggAble: false rowDraggAble: false,
onDropRow: function onDropRow() {}
}; };
var Table = function (_Component) { var Table = function (_Component) {
@ -190,12 +192,16 @@ var Table = function (_Component) {
_this.onDragRow = function (currentKey, targetKey) { _this.onDragRow = function (currentKey, targetKey) {
var data = _this.state.data, var data = _this.state.data,
currentIndex = void 0, currentIndex = void 0,
targetIndex = void 0; targetIndex = void 0,
record = void 0;
data.forEach(function (da, i) { data.forEach(function (da, i) {
if (da.key == currentKey) { // tr 的唯一标识通过 data.key 或 rowKey 两种方式传进来
var trKey = da.key ? da.key : _this.getRowKey(da, i);
if (trKey == currentKey) {
currentIndex = i; currentIndex = i;
record = da;
} }
if (da.key == targetKey) { if (trKey == targetKey) {
targetIndex = i; targetIndex = i;
} }
}); });
@ -204,6 +210,7 @@ var Table = function (_Component) {
} else { } else {
data.splice(targetIndex + 1, 0, data.splice(currentIndex, 1).shift()); data.splice(targetIndex + 1, 0, data.splice(currentIndex, 1).shift());
} }
_this.props.onDropRow && _this.props.onDropRow(data, record);
_this.setState({ _this.setState({
data: data data: data
}); });
@ -720,6 +727,13 @@ var Table = function (_Component) {
}); });
}; };
/**
* 行拖拽结束时触发
* @param currentKey 当前拖拽目标的key
* @param targetKey 拖拽结束时目标位置的key
*/
/** /**
* *
* *

View File

@ -451,7 +451,8 @@ var TableRow = function (_Component) {
fixed = _props9.fixed, fixed = _props9.fixed,
bodyDisplayInRow = _props9.bodyDisplayInRow, bodyDisplayInRow = _props9.bodyDisplayInRow,
expandedIcon = _props9.expandedIcon, expandedIcon = _props9.expandedIcon,
collapsedIcon = _props9.collapsedIcon; collapsedIcon = _props9.collapsedIcon,
hoverKey = _props9.hoverKey;
var showSum = false; var showSum = false;
var className = this.props.className; var className = this.props.className;
@ -518,7 +519,7 @@ var TableRow = function (_Component) {
onMouseLeave: this.onMouseLeave, onMouseLeave: this.onMouseLeave,
className: clsPrefix + ' ' + className + ' ' + clsPrefix + '-level-' + indent, className: clsPrefix + ' ' + className + ' ' + clsPrefix + '-level-' + indent,
style: style, style: style,
'data-row-key': record && record.key ? record.key : "null" 'data-row-key': record && record.key ? record.key : hoverKey
// key={hoverKey} // key={hoverKey}
, ref: this.bindElement , ref: this.bindElement
}, },

View File

@ -2,7 +2,7 @@
* *
* @title 拖拽改变行顺序 * @title 拖拽改变行顺序
* @parent 行操作-拖拽 * @parent 行操作-拖拽
* @description `rowDraggAble`参数设置是否使用行交换顺序功能 * @description `rowDraggAble`参数设置是否使用行交换顺序功能`onDropRow` 是拖拽行后的回调函数注意表格行数据必须有唯一标识可以通过 `data.key` `rowKey` 两种方式传入
* Demo1201 * Demo1201
*/ */
@ -43,12 +43,23 @@ class Demo1201 extends Component {
console.log('内容:' , this.currentRecord); console.log('内容:' , this.currentRecord);
} }
/**
* 行拖拽结束时触发
* @param data 拖拽改变顺序后的新data数组
* @param record 拖拽行的数据
*/
onDropRow = (data, record) => {
console.log('重排序后的data ', data);
console.log('拖拽的行数据: ', record);
}
render() { render() {
return ( return (
<Table <Table
columns={columns} columns={columns}
data={data} data={data}
rowDraggAble={true} rowDraggAble={true}
onDropRow={this.onDropRow}
/> />
); );
} }

View File

@ -2,7 +2,7 @@
* *
* @title 嵌套子表格滚动加载 * @title 嵌套子表格滚动加载
* @parent 无限滚动 Infinite-scroll * @parent 无限滚动 Infinite-scroll
* @description 通过expandedRowRender参数来实现子表格注意事项传入的表格数据必须有 key 值作为唯一标识否则会导致表格的收起展开功能出现问题 * @description 通过expandedRowRender参数来实现子表格注意表格行数据必须有唯一标识可以通过 `data.key` `rowKey` 两种方式传入
* demo1402 * demo1402
*/ */

File diff suppressed because one or more lines are too long

51
dist/demo.js vendored

File diff suppressed because one or more lines are too long

2
dist/demo.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -95,6 +95,7 @@ import 'bee-table/build/Table.css';
| onFilterChange | 触发过滤输入操作以及下拉条件的回调 | function(field,value,condition) | `field` : 字段名称 <br> `value` : 字段值 <br> `condition` : 判断条件 | | onFilterChange | 触发过滤输入操作以及下拉条件的回调 | function(field,value,condition) | `field` : 字段名称 <br> `value` : 字段值 <br> `condition` : 判断条件 |
| onFilterClear | 清除过滤条件的回调函数,回调参数为清空的字段 | function(field) | `field` : 字段名称 | | onFilterClear | 清除过滤条件的回调函数,回调参数为清空的字段 | function(field) | `field` : 字段名称 |
| onRowHover | 行hover时的回调函数 | function(index,record) | `index` : 当前行的index<br> `record` : 当前行的数据 | | onRowHover | 行hover时的回调函数 | function(index,record) | `index` : 当前行的index<br> `record` : 当前行的数据 |
| onDropRow | 拖拽改变行顺序后的回调函数 | function(data,record) | `data` : 拖拽后的新data数组<br> `record` : 拖拽行的数据 |
### Column ### Column

View File

@ -57,6 +57,7 @@ const propTypes = {
hoverContent:PropTypes.func, hoverContent:PropTypes.func,
size: PropTypes.oneOf(['sm', 'md', 'lg']), size: PropTypes.oneOf(['sm', 'md', 'lg']),
rowDraggAble: PropTypes.bool, rowDraggAble: PropTypes.bool,
onDropRow: PropTypes.func
}; };
const defaultProps = { const defaultProps = {
@ -92,7 +93,8 @@ const defaultProps = {
tabIndex:'0', tabIndex:'0',
heightConsistent:false, heightConsistent:false,
size: 'md', size: 'md',
rowDraggAble:false rowDraggAble:false,
onDropRow: ()=>{}
}; };
class Table extends Component { class Table extends Component {
@ -556,13 +558,21 @@ class Table extends Component {
); );
} }
/**
* 行拖拽结束时触发
* @param currentKey 当前拖拽目标的key
* @param targetKey 拖拽结束时目标位置的key
*/
onDragRow = (currentKey,targetKey)=>{ onDragRow = (currentKey,targetKey)=>{
let {data} = this.state,currentIndex,targetIndex; let {data} = this.state,currentIndex,targetIndex,record;
data.forEach((da,i)=>{ data.forEach((da,i)=>{
if(da.key == currentKey){ // tr 的唯一标识通过 data.key 或 rowKey 两种方式传进来
let trKey = da.key ? da.key : this.getRowKey(da, i);
if(trKey == currentKey){
currentIndex = i; currentIndex = i;
record = da;
} }
if(da.key == targetKey){ if(trKey == targetKey){
targetIndex = i; targetIndex = i;
} }
}); });
@ -571,6 +581,7 @@ class Table extends Component {
}else{ }else{
data.splice((targetIndex+1), 0, data.splice(currentIndex, 1).shift()); data.splice((targetIndex+1), 0, data.splice(currentIndex, 1).shift());
} }
this.props.onDropRow && this.props.onDropRow(data,record);
this.setState({ this.setState({
data, data,
}); });

View File

@ -353,7 +353,7 @@ class TableRow extends Component{
clsPrefix, columns, record, height, visible, index, clsPrefix, columns, record, height, visible, index,
expandIconColumnIndex, expandIconAsCell, expanded, expandRowByClick,rowDraggAble, expandIconColumnIndex, expandIconAsCell, expanded, expandRowByClick,rowDraggAble,
expandable, onExpand, needIndentSpaced, indent, indentSize,isHiddenExpandIcon,fixed,bodyDisplayInRow expandable, onExpand, needIndentSpaced, indent, indentSize,isHiddenExpandIcon,fixed,bodyDisplayInRow
,expandedIcon,collapsedIcon ,expandedIcon,collapsedIcon, hoverKey
} = this.props; } = this.props;
let showSum = false; let showSum = false;
let { className } = this.props; let { className } = this.props;
@ -413,7 +413,7 @@ class TableRow extends Component{
if (!visible) { if (!visible) {
style.display = 'none'; style.display = 'none';
} }
return ( return (
<tr <tr
draggable={rowDraggAble} draggable={rowDraggAble}
@ -423,7 +423,7 @@ class TableRow extends Component{
onMouseLeave={this.onMouseLeave} onMouseLeave={this.onMouseLeave}
className={`${clsPrefix} ${className} ${clsPrefix}-level-${indent}`} className={`${clsPrefix} ${className} ${clsPrefix}-level-${indent}`}
style={style} style={style}
data-row-key={record && record.key?record.key:"null"} data-row-key={record && record.key?record.key:hoverKey}
// key={hoverKey} // key={hoverKey}
ref={this.bindElement} ref={this.bindElement}
> >