UNPKG

2.21 kBJavaScriptView Raw
1/**
2 * Module : kero dataTable page removeRow
3 * Author : liuyk(liuyk@yonyou.com)
4 * Date : 2016-08-08 09:59:01
5 */
6
7import {
8 isNumber
9} from 'tinper-sparrow/src/util';
10
11/**
12 * 根据rowid删除行
13 * @param {string} rowid 需要删除行的rowid
14 * @example
15 * page.removeRowByRowId('rowid1')
16 */
17const removeRowByRowId = function(rowid) {
18 for (var i = 0, count = this.rows.length; i < count; i++) {
19 if (this.rows[i].rowId == rowid) {
20 this.rows.splice(i, 1);
21 count--;
22 this.updateSelectedIndices(i, '-')
23 this.updateFocusIndex(i, '-')
24 }
25 }
26}
27
28// 新增/删除行之后更新选中行的index
29const updateSelectedIndices = function(index, type, num) {
30 if (!isNumber(num)) {
31 num = 1
32 }
33 var selectedIndices = this.selectedIndices.slice();
34 if (selectedIndices == null || selectedIndices.length == 0)
35 return
36 for (var i = 0, count = selectedIndices.length; i < count; i++) {
37 if (type == '+') {
38 if (selectedIndices[i] >= index)
39 selectedIndices[i] = parseInt(selectedIndices[i]) + num
40 } else if (type == '-') {
41 if (selectedIndices[i] >= index && selectedIndices[i] <= index + num - 1) {
42 selectedIndices.splice(i, 1)
43 } else if (selectedIndices[i] > index + num - 1)
44 selectedIndices[i] = selectedIndices[i] - num
45 }
46 }
47 this.selectedIndices = selectedIndices
48}
49
50//新增/删除行之后更新焦点行
51const updateFocusIndex = function(opIndex, opType, num) {
52 if (!isNumber(num)) {
53 num = 1
54 }
55 if (opIndex <= this.focus && this.focus != -1) {
56 if (opType === '+') {
57 this.focus = this.focus + num
58 } else if (opType === '-') {
59 if (this.focus >= opIndex && this.focus <= opIndex + num - 1) {
60 this.focus = this.focus - 1
61 } else if (this.focus > opIndex + num - 1) {
62 this.focus = this.focus - num
63 }
64 }
65 }
66}
67
68
69export const pageRemoveRowFunObj = {
70 removeRowByRowId: removeRowByRowId,
71 updateSelectedIndices: updateSelectedIndices,
72 updateFocusIndex: updateFocusIndex
73}