UNPKG

2.81 kBJavaScriptView Raw
1/**
2 * Module : kero dataTable rowFocus
3 * Author : liuyk(liuyk@yonyou.com)
4 * Date : 2016-08-08 09:59:01
5 */
6import { isNumber } from 'tinper-sparrow/src/util';
7
8/**
9 * 设置焦点行
10 * @memberof DataTable
11 * @param {number|u.Row} index 行对象或者行index
12 * @param {boolean} [quiet] 如果为true则不触发事件,否则触发事件
13 * @param {boolean} [force] 如果为true当index行与已focus的行相等时,仍然触发事件,否则不触发事件
14 * @example
15 * datatable.setRowFocus(1) // 设置第二行为焦点行
16 * datatable.setRowFocus(1,true) // 设置第二行为焦点行,不触发事件
17 * datatable.setRowFocus(1,false,true) // 设置第二行为焦点行,如果当前焦点行为第二行,仍旧触发事件
18 */
19var setRowFocus = function setRowFocus(index, quiet, force) {
20 var rowId = null;
21 if (index instanceof Row) {
22 index = this.getIndexByRowId(index.rowId);
23 rowId = index.rowId;
24 }
25 if (index === -1 || index === this.focusIndex() && !force) {
26 return;
27 }
28 this.focusIndex(index);
29 if (quiet) {
30 return;
31 }
32 this.currentRowChange(-this.currentRowChange());
33 if (!rowId) {
34 rowId = this.getRow(index).rowId;
35 }
36 this.trigger(DataTable.ON_ROW_FOCUS, {
37 index: index,
38 rowId: rowId
39 });
40 this.updateCurrIndex();
41};
42
43/**
44 * 焦点行反选
45 * @memberof DataTable
46 * @example
47 * datatable.setRowUnFocus()
48 */
49var setRowUnFocus = function setRowUnFocus() {
50 this.currentRowChange(-this.currentRowChange());
51 var indx = this.focusIndex(),
52 rowId = null;
53 if (indx !== -1) {
54 rowId = this.getRow(indx).rowId;
55 }
56 this.trigger(DataTable.ON_ROW_UNFOCUS, {
57 index: indx,
58 rowId: rowId
59 });
60 this.focusIndex(-1);
61 this.updateCurrIndex();
62};
63
64/***
65 * 数据行发生改变时更新focusindex
66 * @memberof DataTable
67 * @param {number} opIndex 发生改变的数据行位置
68 * @param {string} opType +表示新增行,-表示减少行
69 * @param {number} num 新增/减少的行数
70 *
71 */
72var updateFocusIndex = function updateFocusIndex(opIndex, opType, num) {
73 if (!isNumber(num)) {
74 num = 1;
75 }
76 if (opIndex <= this.focusIndex() && this.focusIndex() != -1) {
77 if (opType === '+') {
78 this.focusIndex(this.focusIndex() + num);
79 } else if (opType === '-') {
80 if (this.focusIndex() >= opIndex && this.focusIndex() <= opIndex + num - 1) {
81 this.focusIndex(-1);
82 } else if (this.focusIndex() > opIndex + num - 1) {
83 this.focusIndex(this.focusIndex() - num);
84 }
85 }
86 }
87};
88
89export var rowFocusFunObj = {
90 setRowFocus: setRowFocus,
91 setRowUnFocus: setRowUnFocus,
92 updateFocusIndex: updateFocusIndex
93};
\No newline at end of file