UNPKG

2.67 kBJavaScriptView Raw
1/**
2 * Module : kero dataTable getSelect
3 * Author : liuyk(liuyk@yonyou.com)
4 * Date : 2016-08-01 14:34:01
5 */
6
7
8/**
9 * 获取选中行索引,多选时,只返回第一个行索引
10 * @memberof DataTable
11 * @return {number} 选中行索引
12 * @example
13 * datatable.getSelectedIndex()
14 */
15const getSelectedIndex = function() {
16 var selectedIndices = this.selectedIndices()
17 if (selectedIndices == null || selectedIndices.length == 0)
18 return -1
19 return selectedIndices[0]
20};
21
22/**
23 * 获取选中的所有行索引数组
24 * @memberof DataTable
25 * @return {array} 所有行索引数组
26 * @example
27 * datatable.getSelectedIndices()
28 */
29const getSelectedIndices = function() {
30 var selectedIndices = this.selectedIndices()
31 if (selectedIndices == null || selectedIndices.length == 0)
32 return []
33 return selectedIndices
34};
35
36// 兼容保留,不要用
37const getSelectedIndexs = function() {
38 return this.getSelectedIndices();
39}
40
41/**
42 * 获取选中行的数据信息
43 * @memberof DataTable
44 * @param {boolean} [withEmptyRow=false] 未选中的数据是否使用空行代替,true表示以空行代替未选中行,false相反
45 * @return {array} 发生变化的数据信息
46 * @example
47 * datatable.getSelectedDatas()
48 * datatable.getSelectedDatas(true)
49 */
50const getSelectedDatas = function(withEmptyRow) {
51 var selectedIndices = this.selectedIndices()
52 var datas = []
53 var sIndices = []
54 for (var i = 0, count = selectedIndices.length; i < count; i++) {
55 sIndices.push(selectedIndices[i])
56 }
57 var rows = this.rows();
58 for (var i = 0, count = rows.length; i < count; i++) {
59 if (sIndices.indexOf(i) != -1)
60 datas.push(rows[i].getData())
61 else if (withEmptyRow == true)
62 datas.push(rows[i].getEmptyData())
63 }
64 return datas
65};
66
67/**
68 * 获取选中的Row对象
69 * @memberof DataTable
70 * @return {array} 选中的Row对象
71 * @example
72 * datatable.getSelectedRows()
73 */
74const getSelectedRows = function() {
75 var selectedIndices = this.selectedIndices();
76 var selectRows = [];
77 var rows = this.rows.peek();
78 var sIndices = []
79 for (var i = 0, count = selectedIndices.length; i < count; i++) {
80 sIndices.push(selectedIndices[i])
81 }
82 for (var i = 0, count = rows.length; i < count; i++) {
83 if (sIndices.indexOf(i) != -1)
84 selectRows.push(rows[i])
85 }
86 return selectRows
87}
88
89export const getSelectFunObj = {
90 getSelectedIndex: getSelectedIndex,
91 getSelectedIndices: getSelectedIndices,
92 getSelectedIndexs: getSelectedIndexs,
93 getSelectedDatas: getSelectedDatas,
94 getSelectedRows: getSelectedRows
95}