1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import {isEmptyObject} from 'tinper-sparrow/src/util';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const setRows = function (rows, options) {
|
13 | var insertRows = [], _id;
|
14 | for (var i = 0; i < rows.length; i++) {
|
15 | var r = rows[i]
|
16 | _id = r.rowId || r.id;
|
17 | if (!_id)
|
18 | _id = Row.getRandomRowId()
|
19 | if (r.status == Row.STATUS.DELETE) {
|
20 | this.removeRowByRowId(_id)
|
21 | }
|
22 | else {
|
23 | var row = this.getRowByRowId(_id)
|
24 | if (row) {
|
25 | row.updateRow(r);
|
26 | if (!isEmptyObject(r.data)) {
|
27 | this.trigger(DataTable.ON_UPDATE, {
|
28 | index: i,
|
29 | rows: [row]
|
30 | })
|
31 | if (row == this.getCurrentRow()) {
|
32 | this.currentRowChange(-this.currentRowChange())
|
33 | row.currentRowChange(-row.currentRowChange())
|
34 | this.trigger(DataTable.ON_CURRENT_UPDATE, {
|
35 | index: i,
|
36 | rows: [row]
|
37 | })
|
38 | } else {
|
39 | row.currentRowChange(-row.currentRowChange())
|
40 | }
|
41 | }
|
42 |
|
43 | }
|
44 | else {
|
45 | row = new Row({parent: this, id: _id})
|
46 | row.setData(rows[i], null, options)
|
47 | insertRows.push(row)
|
48 | }
|
49 |
|
50 | if(r.status){
|
51 | row.status = r.status;
|
52 | }
|
53 | }
|
54 | }
|
55 | if (insertRows.length > 0)
|
56 | this.addRows(insertRows)
|
57 | return insertRows;
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | const addRow = function (row) {
|
65 | this.insertRow(this.rows().length, row)
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | const addRows = function (rows) {
|
72 | this.insertRows(this.rows().length, rows)
|
73 | }
|
74 |
|
75 | const insertRow = function (index, row) {
|
76 | if (!row) {
|
77 | row = new Row({parent: this})
|
78 | }
|
79 | this.insertRows(index, [row])
|
80 | }
|
81 |
|
82 | const insertRows = function (index, rows) {
|
83 | var args = [index, 0]
|
84 | for (var i = 0; i < rows.length; i++) {
|
85 | args.push(rows[i]);
|
86 | }
|
87 | this.rows.splice.apply(this.rows, args);
|
88 |
|
89 | this.updateSelectedIndices(index, '+', rows.length)
|
90 | this.updateFocusIndex(index, '+', rows.length)
|
91 | this.updatePageAll();
|
92 | this.trigger(DataTable.ON_INSERT, {
|
93 | index: index,
|
94 | rows: rows
|
95 | })
|
96 | if (this.ns){
|
97 | if (this.root.valueChange[this.ns])
|
98 | this.root.valueChange[this.ns](-this.root.valueChange[this.ns]());
|
99 | }
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | const createEmptyRow = function () {
|
106 | var r = new Row({parent: this})
|
107 | this.addRow(r)
|
108 | if (!this.getCurrentRow())
|
109 | this.setRowSelect(r);
|
110 | return r
|
111 | }
|
112 |
|
113 | export {
|
114 | setRows,
|
115 | addRow,
|
116 | addRows,
|
117 | insertRow,
|
118 | insertRows,
|
119 | createEmptyRow
|
120 | }
|