UNPKG

7.33 kBJavaScriptView Raw
1/**
2 * Module : kero dataTable rowSelect
3 * Author : liuyk(liuyk@yonyou.com)
4 * Date : 2016-08-01 14:34:01
5 */
6import {
7 isArray,
8 isNumber
9} from 'tinper-sparrow/src/util';
10import {
11 utilFunObj
12} from './util';
13
14/**
15 * 设置所有行选中
16 * @memberof DataTable
17 * @example
18 * datatable.setAllRowsSelect()
19 */
20const setAllRowsSelect = function() {
21 var indices = new Array(this.rows().length)
22 for (var i = 0; i < indices.length; i++) {
23 indices[i] = i
24 }
25 this.setRowsSelect(indices);
26 this.allSelected(true);
27 this.trigger(DataTable.ON_ROW_ALLSELECT, {})
28}
29
30/**
31 * 根据索引设置选中行,清空之前已选中的所有行
32 * @memberof DataTable
33 * @param {number} index 需要选中行的索引
34 * @example
35 * datatable.setRowSelect(1)
36 */
37const setRowSelect = function(index) {
38 if (index instanceof Row) {
39 index = this.getIndexByRowId(index.rowId)
40 }
41 this.setRowsSelect([index])
42 this.setRowFocus(this.getSelectedIndex())
43}
44
45/**
46 * 根据索引数组设置选中行,清空之前已选中的所有行
47 * @memberof DataTable
48 * @param {array} indices 需要选中行的索引数组
49 * @example
50 * datatable.setRowsSelect([1,2])
51 */
52const setRowsSelect = function(indices) {
53 indices = indices || -1;
54 if (indices == -1) {
55 this.setAllRowsUnSelect({
56 quiet: true
57 })
58 return;
59 }
60 indices = utilFunObj._formatToIndicesArray(this, indices);
61 var sIns = this.selectedIndices();
62 if (isArray(indices) && isArray(sIns) && indices.join() == sIns.join()) {
63 // 避免与控件循环触发
64 return;
65 }
66
67 if (isArray(indices)) {
68 var rowNum = this.rows().length
69 for (var i = 0; i < indices.length; i++) {
70 if (indices[i] < 0 || indices[i] >= rowNum)
71 indices.splice(i, 1);
72 }
73 }
74
75 this.setAllRowsUnSelect({
76 quiet: true
77 });
78 try {
79 this.selectedIndices(indices);
80 } catch (e) {
81
82 }
83 this.updatePageSelect();
84 var rowIds = this.getRowIdsByIndices(indices);
85 this.currentRowChange(-this.currentRowChange());
86 this.trigger(DataTable.ON_ROW_SELECT, {
87 indices: indices,
88 rowIds: rowIds
89 })
90 this.updateCurrIndex();
91
92}
93
94
95/**
96 * 根据索引添加选中行,不会清空之前已选中的行
97 * @memberof DataTable
98 * @param {number} index 需要选中行的索引
99 * @example
100 * datatable.addRowSelect(1)
101 */
102const addRowSelect = function(index) {
103 if (index instanceof Row) {
104 index = this.getIndexByRowId(index.rowId)
105 }
106 this.addRowsSelect([index])
107}
108
109/**
110 * 根据索引数组添加选中行,不会清空之前已选中的行
111 * @memberof DataTable
112 * @param {array} indices 需要选中行的索引数组
113 * @example
114 * datatabel.addRowsSelect([1,2])
115 */
116const addRowsSelect = function(indices) {
117 indices = utilFunObj._formatToIndicesArray(this, indices)
118 var selectedIndices = this.selectedIndices().slice()
119 var needTrigger = false;
120 for (var i = 0; i < indices.length; i++) {
121 var ind = indices[i],
122 toAdd = true
123 for (var j = 0; j < selectedIndices.length; j++) {
124 if (selectedIndices[j] == ind) {
125 toAdd = false
126 }
127 }
128 //indices[i]存在并且大于-1
129 if (toAdd && indices[i] > -1) {
130 needTrigger = true
131 selectedIndices.push(indices[i])
132 }
133 }
134 this.selectedIndices(selectedIndices)
135 this.updatePageSelect();
136 var rowIds = this.getRowIdsByIndices(selectedIndices)
137 if (needTrigger) {
138 this.trigger(DataTable.ON_ROW_SELECT, {
139 indices: selectedIndices,
140 rowIds: rowIds
141 })
142 }
143 this.updateCurrIndex();
144
145}
146
147/**
148 * 全部取消选中
149 * @memberof DataTable
150 * @param {object} [options] 可选参数
151 * @param {boolean} [options.quiet] 如果为true则不触发事件,否则触发事件
152 * @example
153 * datatable.setAllRowsUnSelect() // 全部取消选中
154 * datatable.setAllRowsUnSelect({quiet:true}) // 全部取消选中,不触发事件
155 */
156const setAllRowsUnSelect = function(options) {
157 this.selectedIndices([])
158 this.updatePageSelect();
159 if (!(options && options.quiet)) {
160 this.trigger(DataTable.ON_ROW_ALLUNSELECT)
161 }
162 this.updateCurrIndex();
163 this.allSelected(false);
164}
165
166/**
167 * 根据索引取消选中
168 * @memberof DataTable
169 * @param {number} index 需要取消选中的行索引
170 * @example
171 * datatable.setRowUnSelect(1)
172 */
173const setRowUnSelect = function(index) {
174 if (index instanceof Row) {
175 index = this.getIndexByRowId(index.rowId)
176 }
177 this.setRowsUnSelect([index])
178}
179
180/**
181 * 根据索引数组取消选中
182 * @memberof DataTable
183 * @param {array} indices 需要取消选中的行索引数组
184 * @example
185 * datatable.setRowsUnSelect([1,2])
186 */
187const setRowsUnSelect = function(indices) {
188 indices = utilFunObj._formatToIndicesArray(this, indices)
189 var selectedIndices = this.selectedIndices().slice()
190
191 // 避免与控件循环触发
192 if (selectedIndices.indexOf(indices[0]) == -1) return;
193
194 for (var i = 0; i < indices.length; i++) {
195 var index = indices[i]
196 var pos = selectedIndices.indexOf(index)
197 if (pos != -1)
198 selectedIndices.splice(pos, 1)
199 }
200 this.selectedIndices(selectedIndices)
201 this.updatePageSelect();
202 var rowIds = this.getRowIdsByIndices(indices)
203 this.trigger(DataTable.ON_ROW_UNSELECT, {
204 indices: indices,
205 rowIds: rowIds
206 })
207 this.updateCurrIndex();
208 this.allSelected(false);
209}
210
211/**
212 * 当全部选中时取消选中,否则全部选中
213 * @memberof DataTable
214 */
215const toggleAllSelect = function() {
216 if (this.allSelected()) {
217 this.setAllRowsUnSelect();
218 } else {
219 this.setAllRowsSelect();
220 }
221
222};
223
224
225/***
226 * 数据行发生改变时更新focusindex
227 * @memberof DataTable
228 * @param {number} index 发生改变的数据行位置
229 * @param {string} type +表示新增行,-表示减少行
230 * @param {number} num 新增/减少的行数
231 */
232const updateSelectedIndices = function(index, type, num) {
233 if (!isNumber(num)) {
234 num = 1
235 }
236 var selectedIndices = this.selectedIndices().slice()
237 if (selectedIndices == null || selectedIndices.length == 0)
238 return
239 for (var i = 0, count = selectedIndices.length; i < count; i++) {
240 if (type == '+') {
241 if (selectedIndices[i] >= index)
242 selectedIndices[i] = parseInt(selectedIndices[i]) + num
243 } else if (type == '-') {
244 if (selectedIndices[i] >= index && selectedIndices[i] <= index + num - 1) {
245 selectedIndices.splice(i, 1)
246 } else if (selectedIndices[i] > index + num - 1)
247 selectedIndices[i] = selectedIndices[i] - num
248 }
249 }
250 this.selectedIndices(selectedIndices)
251 this.updatePageSelect();
252}
253export const rowSelectFunObj = {
254 setAllRowsSelect: setAllRowsSelect,
255 setRowSelect: setRowSelect,
256 setRowsSelect: setRowsSelect,
257 addRowSelect: addRowSelect,
258 addRowsSelect: addRowsSelect,
259 setAllRowsUnSelect: setAllRowsUnSelect,
260 setRowUnSelect: setRowUnSelect,
261 setRowsUnSelect: setRowsUnSelect,
262 toggleAllSelect: toggleAllSelect,
263 updateSelectedIndices: updateSelectedIndices
264}