Skip to content

Commit f032243

Browse files
committed
add setCellSelection instance method
1 parent 7309b3c commit f032243

File tree

3 files changed

+58
-19
lines changed

3 files changed

+58
-19
lines changed

packages/ve-table/src/index.jsx

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
getContextmenuBodyOptionCollection,
88
createEmptyRowData,
99
isContextmenuPanelClicked,
10+
getRowKey,
11+
getColumnByColkey,
1012
} from "./util";
1113
import {
1214
getValByUnit,
@@ -1585,27 +1587,15 @@ export default {
15851587

15861588
// cell selection by click
15871589
cellSelectionByClick({ rowData, column }) {
1588-
const { rowKeyFieldName, cellSelectionOption } = this;
1590+
const { rowKeyFieldName } = this;
15891591

1590-
const rowKey = rowData[rowKeyFieldName];
1592+
const rowKey = getRowKey(rowData, rowKeyFieldName);
15911593

1592-
// update cell selection
1593-
if (
1594-
!(
1595-
cellSelectionOption &&
1596-
isBoolean(cellSelectionOption.enable) &&
1597-
cellSelectionOption.enable === false
1598-
)
1599-
) {
1600-
if (!isEmptyValue(rowKey) && !isEmptyValue(column.key)) {
1601-
this.cellSelectionKeyChange({
1602-
rowKey,
1603-
colKey: column.key,
1604-
});
1605-
1606-
this.columnToVisible(column);
1607-
}
1608-
}
1594+
// set cell selection and column to visible
1595+
this[INSTANCE_METHODS.SET_CELL_SELECTION]({
1596+
rowKey,
1597+
colKey: column.key,
1598+
});
16091599
},
16101600

16111601
/*
@@ -1802,6 +1792,31 @@ export default {
18021792
}
18031793
},
18041794

1795+
/*
1796+
set cell selection and column to visible
1797+
*/
1798+
[INSTANCE_METHODS.SET_CELL_SELECTION]({ rowKey, colKey }) {
1799+
const { cellSelectionOption } = this;
1800+
1801+
if (
1802+
cellSelectionOption &&
1803+
isBoolean(cellSelectionOption.enable) &&
1804+
cellSelectionOption.enable === false
1805+
) {
1806+
return false;
1807+
}
1808+
1809+
if (!isEmptyValue(rowKey) && !isEmptyValue(colKey)) {
1810+
this.cellSelectionKeyChange({
1811+
rowKey,
1812+
colKey,
1813+
});
1814+
1815+
const column = getColumnByColkey(colKey, this.colgroups);
1816+
this.columnToVisible(column);
1817+
}
1818+
},
1819+
18051820
// hide columns by keys
18061821
[INSTANCE_METHODS.HIDE_COLUMNS_BY_KEYS](keys) {
18071822
if (!isEmptyArray(keys)) {

packages/ve-table/src/util/constant.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ export const INSTANCE_METHODS = {
134134
STOP_EDITING_CELL: "stopEditingCell",
135135
// set highlight row
136136
SET_HIGHLIGHT_ROW: "setHighlightRow",
137+
// set cell selection
138+
SET_CELL_SELECTION: "setCellSelection",
137139
// hide columns by keys
138140
HIDE_COLUMNS_BY_KEYS: "hideColumnsByKeys",
139141
// show columns by keys

tests/unit/specs/ve-table-cell-selection.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,28 @@ describe("veTable cell selection", () => {
573573
expect(firstCell.find(".ve-table-cell-selection").exists()).toBe(false);
574574
});
575575

576+
it("table instance: setCellSelection method", async () => {
577+
const wrapper = mount(veTable, {
578+
propsData: {
579+
columns: COLUMNS,
580+
tableData: TABLE_DATA,
581+
rowKeyFieldName: "rowKey",
582+
},
583+
});
584+
585+
wrapper.vm.setCellSelection({ rowKey: "2", colKey: "a" });
586+
587+
await later();
588+
589+
const selectionTd = wrapper
590+
.findAll(".ve-table-body-tr")
591+
.at(1)
592+
.findAll(".ve-table-body-td")
593+
.at(0);
594+
595+
expect(selectionTd.classes()).toContain("ve-table-cell-selection");
596+
});
597+
576598
/* it("virtual scroll keyboard events", async () => {
577599
const mockFn = jest.fn();
578600

0 commit comments

Comments
 (0)