UNPKG

5.72 kBJavaScriptView Raw
1/**
2 * Module : kero dataTable rowSelect
3 * Author : liuyk(liuyk@yonyou.com)
4 * Date : 2016-08-01 14:34:01
5 */
6import {isArray, isNumber} from 'tinper-sparrow/src/util';
7import {_formatToIndicesArray} from './util';
8const setAllRowsSelect = function () {
9 var indices = new Array(this.rows().length)
10 for (var i = 0; i < indices.length; i++) {
11 indices[i] = i
12 }
13 this.setRowsSelect(indices);
14 this.allSelected(true);
15 this.trigger(DataTable.ON_ROW_ALLSELECT, {})
16}
17
18/**
19 * 设置选中行,清空之前已选中的所有行
20 */
21const setRowSelect = function (index) {
22 if (index instanceof Row) {
23 index = this.getIndexByRowId(index.rowId)
24 }
25 this.setRowsSelect([index])
26 this.setRowFocus(this.getSelectedIndex())
27}
28
29const setRowsSelect = function (indices) {
30 indices = indices || -1;
31 if (indices == -1) {
32 this.setAllRowsUnSelect({quiet: true})
33 return;
34 }
35 indices = _formatToIndicesArray(this, indices);
36 var sIns = this.selectedIndices();
37 if (isArray(indices) && isArray(sIns) && indices.join() == sIns.join()) {
38 // 避免与控件循环触发
39 return;
40 }
41
42 if(u.isArray(indices)) {
43 var rowNum = this.rows().length
44 for(var i=0;i<indices.length;i++) {
45 if(indices[i]<0 || indices[i] >= rowNum)
46 indices.splice(i, 1);
47 }
48 }
49
50 this.setAllRowsUnSelect({quiet: true});
51 try{
52 this.selectedIndices(indices);
53 }catch(e){
54
55 }
56 this.updatePageSelect();
57 var rowIds = this.getRowIdsByIndices(indices);
58 this.currentRowChange(-this.currentRowChange());
59 this.trigger(DataTable.ON_ROW_SELECT, {
60 indices: indices,
61 rowIds: rowIds
62 })
63 this.updateCurrIndex();
64
65}
66
67
68/**
69 * 添加选中行,不会清空之前已选中的行
70 */
71const addRowSelect = function (index) {
72 if (index instanceof Row) {
73 index = this.getIndexByRowId(index.rowId)
74 }
75 this.addRowsSelect([index])
76}
77
78/**
79 * 添加选中行,不会清空之前已选中的行
80 */
81const addRowsSelect = function (indices) {
82 indices = _formatToIndicesArray(this, indices)
83 var selectedIndices = this.selectedIndices().slice()
84 var needTrigger = false;
85 for (var i = 0; i < indices.length; i++) {
86 var ind = indices[i], toAdd = true
87 for (var j = 0; j < selectedIndices.length; j++) {
88 if (selectedIndices[j] == ind) {
89 toAdd = false
90 }
91 }
92 //indices[i]存在并且大于-1
93 if (toAdd && indices[i] > -1) {
94 needTrigger = true
95 selectedIndices.push(indices[i])
96 }
97 }
98 this.selectedIndices(selectedIndices)
99 this.updatePageSelect();
100 var rowIds = this.getRowIdsByIndices(selectedIndices)
101 if(needTrigger){
102 this.trigger(DataTable.ON_ROW_SELECT, {
103 indices: selectedIndices,
104 rowIds: rowIds
105 })
106 }
107 this.updateCurrIndex();
108
109}
110
111/**
112 * 全部取消选中
113 */
114const setAllRowsUnSelect = function (options) {
115 this.selectedIndices([])
116 this.updatePageSelect();
117 if (!(options && options.quiet)) {
118 this.trigger(DataTable.ON_ROW_ALLUNSELECT)
119 }
120 this.updateCurrIndex();
121 this.allSelected(false);
122}
123
124/**
125 * 取消选中
126 */
127const setRowUnSelect = function (index) {
128 if (index instanceof Row) {
129 index = this.getIndexByRowId(index.rowId)
130 }
131 this.setRowsUnSelect([index])
132}
133
134const setRowsUnSelect = function (indices) {
135 indices = _formatToIndicesArray(this, indices)
136 var selectedIndices = this.selectedIndices().slice()
137
138 // 避免与控件循环触发
139 if (selectedIndices.indexOf(indices[0]) == -1) return;
140
141 for (var i = 0; i < indices.length; i++) {
142 var index = indices[i]
143 var pos = selectedIndices.indexOf(index)
144 if (pos != -1)
145 selectedIndices.splice(pos, 1)
146 }
147 this.selectedIndices(selectedIndices)
148 this.updatePageSelect();
149 var rowIds = this.getRowIdsByIndices(indices)
150 this.trigger(DataTable.ON_ROW_UNSELECT, {
151 indices: indices,
152 rowIds: rowIds
153 })
154 this.updateCurrIndex();
155 this.allSelected(false);
156}
157
158
159 const toggleAllSelect = function(){
160 if (this.allSelected()){
161 this.setAllRowsUnSelect();
162 }else{
163 this.setAllRowsSelect();
164 }
165
166};
167
168
169/**
170 *
171 * @param {Object} index 要处理的起始行索引
172 * @param {Object} type 增加或减少 + -
173 */
174const updateSelectedIndices = function (index, type, num) {
175 if (!isNumber(num)) {
176 num = 1
177 }
178 var selectedIndices = this.selectedIndices().slice()
179 if (selectedIndices == null || selectedIndices.length == 0)
180 return
181 for (var i = 0, count = selectedIndices.length; i < count; i++) {
182 if (type == '+') {
183 if (selectedIndices[i] >= index)
184 selectedIndices[i] = parseInt(selectedIndices[i]) + num
185 }
186 else if (type == '-') {
187 if (selectedIndices[i] >= index && selectedIndices[i] <= index + num - 1) {
188 selectedIndices.splice(i, 1)
189 }
190 else if (selectedIndices[i] > index + num - 1)
191 selectedIndices[i] = selectedIndices[i] - num
192 }
193 }
194 this.selectedIndices(selectedIndices)
195 this.updatePageSelect();
196}
197export {
198 setAllRowsSelect,
199 setRowSelect,
200 setRowsSelect,
201 addRowSelect,
202 addRowsSelect,
203 setAllRowsUnSelect,
204 setRowUnSelect,
205 setRowsUnSelect,
206 toggleAllSelect,
207 updateSelectedIndices
208}