UNPKG

3.34 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createEmptyRow = exports.insertRows = exports.insertRow = exports.addRows = exports.addRow = exports.setRows = undefined;
7
8var _util = require('tinper-sparrow/src/util');
9
10/**
11 * 设置行数据
12 * @param {Object} rows
13 */
14var setRows = function setRows(rows, options) {
15 var insertRows = [],
16 _id;
17 for (var i = 0; i < rows.length; i++) {
18 var r = rows[i];
19 _id = r.rowId || r.id;
20 if (!_id) _id = Row.getRandomRowId();
21 if (r.status == Row.STATUS.DELETE) {
22 this.removeRowByRowId(_id);
23 } else {
24 var row = this.getRowByRowId(_id);
25 if (row) {
26 row.updateRow(r);
27 if (!(0, _util.isEmptyObject)(r.data)) {
28 this.trigger(DataTable.ON_UPDATE, {
29 index: i,
30 rows: [row]
31 });
32 if (row == this.getCurrentRow()) {
33 this.currentRowChange(-this.currentRowChange());
34 row.currentRowChange(-row.currentRowChange());
35 this.trigger(DataTable.ON_CURRENT_UPDATE, {
36 index: i,
37 rows: [row]
38 });
39 } else {
40 row.currentRowChange(-row.currentRowChange());
41 }
42 }
43 } else {
44 row = new Row({ parent: this, id: _id });
45 row.setData(rows[i], null, options);
46 insertRows.push(row);
47 }
48 // 如果r对象中存在状态则更新状态为返回的状态
49 if (r.status) {
50 row.status = r.status;
51 }
52 }
53 }
54 if (insertRows.length > 0) this.addRows(insertRows);
55 return insertRows;
56};
57
58/**
59 *追加行
60 */
61/**
62 * Module : kero dataTable row
63 * Author : liuyk(liuyk@yonyou.com)
64 * Date : 2016-08-01 14:34:01
65 */
66var addRow = function addRow(row) {
67 this.insertRow(this.rows().length, row);
68};
69
70/**
71 *追加多行
72 */
73var addRows = function addRows(rows) {
74 this.insertRows(this.rows().length, rows);
75};
76
77var insertRow = function insertRow(index, row) {
78 if (!row) {
79 row = new Row({ parent: this });
80 }
81 this.insertRows(index, [row]);
82};
83
84var insertRows = function insertRows(index, rows) {
85 var args = [index, 0];
86 for (var i = 0; i < rows.length; i++) {
87 args.push(rows[i]);
88 }
89 this.rows.splice.apply(this.rows, args);
90
91 this.updateSelectedIndices(index, '+', rows.length);
92 this.updateFocusIndex(index, '+', rows.length);
93 this.updatePageAll();
94 this.trigger(DataTable.ON_INSERT, {
95 index: index,
96 rows: rows
97 });
98 if (this.ns) {
99 if (this.root.valueChange[this.ns]) this.root.valueChange[this.ns](-this.root.valueChange[this.ns]());
100 }
101};
102
103/**
104 * 创建空行
105 */
106var createEmptyRow = function createEmptyRow() {
107 var r = new Row({ parent: this });
108 this.addRow(r);
109 if (!this.getCurrentRow()) this.setRowSelect(r);
110 return r;
111};
112
113exports.setRows = setRows;
114exports.addRow = addRow;
115exports.addRows = addRows;
116exports.insertRow = insertRow;
117exports.insertRows = insertRows;
118exports.createEmptyRow = createEmptyRow;
\No newline at end of file