Skip to content

Commit

Permalink
Merge 0df7e1a into c263d90
Browse files Browse the repository at this point in the history
  • Loading branch information
veryStarters committed Nov 21, 2018
2 parents c263d90 + 0df7e1a commit da83e9e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
14 changes: 14 additions & 0 deletions demo/GridEditDemo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,20 @@ class Demo extends React.Component {
},
mode: Constants.MODE.EDIT,
},
{
title: '上移',
callback: (rowData) => {
me.table.moveRowUp(rowData);
},
mode: Constants.MODE.VIEW,
},
{
title: '下移',
callback: (rowData) => {
me.table.moveRowDown(rowData);
},
mode: Constants.MODE.VIEW,
},
{
title: '删除',
callback: (rowData) => {
Expand Down
10 changes: 9 additions & 1 deletion demo/TreeGridDemo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@ class Demo extends React.Component {
actions: [
{
title: '上移',
callback: (rowData) => { this.table.moveRowUp(rowData); },
callback: (rowData) => {
this.table.moveRowUp(rowData);
},
},
{
title: '删除',
callback: (rowData) => {
this.table.delRow(rowData);
},
},
{
title: '下移',
Expand Down
4 changes: 2 additions & 2 deletions demo/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import Demo5 from './TableGroup';
import Demo6 from './TableRowGroup';
import '../style';

ReactDOM.render(<Demo />, document.getElementById('UXCoreDemo'));
// ReactDOM.render(<Demo />, document.getElementById('UXCoreDemo'));
// ReactDOM.render(<Demo2 />, document.getElementById('UXCoreDemo2'));
// ReactDOM.render(<Demo3 />, document.getElementById('UXCoreDemo3'));
ReactDOM.render(<Demo3 />, document.getElementById('UXCoreDemo3'));
// ReactDOM.render(<Demo4 />, document.getElementById('UXCoreDemo4'));
// ReactDOM.render(<Demo5 />, document.getElementById('UXCoreDemo5'));
// ReactDOM.render(<Demo6 />, document.getElementById('UXCoreDemo6'));
33 changes: 30 additions & 3 deletions src/innerMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,24 @@ function syncRecord(obj, cb) {
}
});
}
/**
* update row's treeId
* @param rows
* @param prefix
*/
function updateTreeId(rows, prefix) {
if (!rows || !rows.length) {
return;
}
for (let i = 0; i < rows.length; i++) {
let row = rows[i];
let treeId = prefix ? `${prefix}-${i}` : `${i}`;
row.__treeId__ = treeId;
if (row.data) {
updateTreeId(row.data, treeId);
}
}
}

/**
* remove some items from this.state.data & this.data
Expand All @@ -155,19 +173,26 @@ function removeRecords(obj, cb) {
const me = this;
const content = deepcopy(me.state.data);
const data = content.data || content.datas;
const treeIdArr = obj.__treeId__.split('-').map(item => parseInt(item, 10));
let rows = data;
for (let i = 0; i < treeIdArr.length - 1; i++) {
const rowIndex = treeIdArr[i];
rows = rows[rowIndex].data;
}
let objAux = deepcopy(obj);
if (Object.prototype.toString.call(objAux) !== '[object Array]') {
objAux = [objAux];
}
objAux.forEach((item) => {
for (let i = 0; i < data.length; i++) {
const element = data[i];
for (let i = 0; i < rows.length; i++) {
const element = rows[i];
if (element.jsxid === item.jsxid) {
data.splice(i, 1);
rows.splice(i, 1);
break;
}
}
});
updateTreeId(content.data);
me.data = content;
this.setState({
data: content,
Expand All @@ -185,4 +210,6 @@ export default {
updateRecord,
syncRecord,
removeRecords,
updateTreeId
};

6 changes: 2 additions & 4 deletions src/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ function moveRowUp(rowData, cb) {
const lastIndex = treeIdArr.pop();
if (lastIndex > 0) {
rows.splice(lastIndex, 1);
// change treeId before setState;
rows[lastIndex - 1].__treeId__ = treeIdArr.concat([lastIndex]).join('-');
rows.splice(lastIndex - 1, 0, { ...rowData, __treeId__: treeIdArr.concat([lastIndex - 1]).join('-') });
this.data = content;
this.setState({
Expand All @@ -168,6 +166,7 @@ function moveRowUp(rowData, cb) {
}
});
}
this.updateTreeId(data)
}
}

Expand All @@ -183,8 +182,6 @@ function moveRowDown(rowData, cb) {
}
const lastIndex = treeIdArr.pop();
if (lastIndex < rows.length - 1) {
// change treeId before setState;
rows[lastIndex + 1].__treeId__ = treeIdArr.concat([lastIndex]).join('-');
rows.splice(lastIndex, 1);
rows.splice(lastIndex + 1, 0, { ...rowData, __treeId__: treeIdArr.concat([lastIndex + 1]).join('-') });
this.data = content;
Expand All @@ -196,6 +193,7 @@ function moveRowDown(rowData, cb) {
}
});
}
this.updateTreeId(data)
}
}

Expand Down

0 comments on commit da83e9e

Please sign in to comment.