blob: 48f50730d02de3c0ec291493b9210cd846a172fc [file] [log] [blame]
SurenNewareba91c492020-10-27 14:18:54 +05301export const selectedRows = [];
2export const tableHeaderCheckboxModel = false;
3export const tableHeaderCheckboxIndeterminate = false;
4
Yoshie Muranaka183c2752020-02-12 11:30:49 -08005const BVTableSelectableMixin = {
jason westoverd36ac8a2025-11-03 20:58:59 -06006 data() {
7 return {
8 selectedRows: [],
9 tableHeaderCheckboxModel: false,
10 tableHeaderCheckboxIndeterminate: false,
11 };
12 },
13 watch: {
14 currentPage() {
15 // Bootstrap Vue 2 behavior: Clear selections when page changes
16 // This prevents confusion with checkboxes appearing checked on the new page
17 const table = this.$refs.table;
18 if (table) {
19 table.clearSelected();
20 this.selectedRows = [];
21 this.tableHeaderCheckboxModel = false;
22 this.tableHeaderCheckboxIndeterminate = false;
23 }
24 },
25 },
Yoshie Muranaka183c2752020-02-12 11:30:49 -080026 methods: {
27 clearSelectedRows(tableRef) {
jason westoverd36ac8a2025-11-03 20:58:59 -060028 if (tableRef) {
29 tableRef.clearSelected();
30 this.selectedRows = [];
31 this.tableHeaderCheckboxModel = false;
32 this.tableHeaderCheckboxIndeterminate = false;
33 }
Yoshie Muranaka183c2752020-02-12 11:30:49 -080034 },
35 toggleSelectRow(tableRef, rowIndex) {
36 if (tableRef && rowIndex !== undefined) {
jason westoverd36ac8a2025-11-03 20:58:59 -060037 const wasSelected = tableRef.isRowSelected(rowIndex);
38
39 if (wasSelected) {
40 tableRef.unselectRow(rowIndex);
Yoshie Muranaka183c2752020-02-12 11:30:49 -080041 } else {
jason westoverd36ac8a2025-11-03 20:58:59 -060042 tableRef.selectRow(rowIndex);
Yoshie Muranaka183c2752020-02-12 11:30:49 -080043 }
jason westoverd36ac8a2025-11-03 20:58:59 -060044
45 // Manually trigger onRowSelected after toggle since unselectRow might not fire event
46 this.$nextTick(() => {
47 this.onRowSelected();
48 });
Yoshie Muranaka183c2752020-02-12 11:30:49 -080049 }
50 },
jason westoverd36ac8a2025-11-03 20:58:59 -060051 onRowSelected() {
52 /*
53 * Bootstrap Vue Next fires @row-selected for each individual row change.
54 * Query the table's internal state to get ALL currently selected rows.
55 */
56 const table = this.$refs.table;
57 if (!table) return;
58
59 const allItems = table.filteredItems || table.items || [];
60 const selectedItems = allItems.filter((item, index) => {
61 return table.isRowSelected(index);
62 });
63
64 this.selectedRows = selectedItems;
65
66 // Update header checkbox state
67 const currentPage = this.currentPage || 1;
68 const perPage = this.perPage || 10;
69 const startIndex = (currentPage - 1) * perPage;
70 const endIndex = Math.min(startIndex + perPage, allItems.length);
71 const pageItemsCount = endIndex - startIndex;
72
73 const selectedOnPageCount = selectedItems.filter((item) =>
74 allItems
75 .slice(startIndex, endIndex)
76 .some((pageItem) => pageItem === item),
77 ).length;
78
79 if (selectedOnPageCount === 0) {
80 this.tableHeaderCheckboxIndeterminate = false;
81 this.tableHeaderCheckboxModel = false;
82 } else if (selectedOnPageCount === pageItemsCount) {
83 this.tableHeaderCheckboxIndeterminate = false;
84 this.tableHeaderCheckboxModel = true;
85 } else {
86 this.tableHeaderCheckboxIndeterminate = true;
87 this.tableHeaderCheckboxModel = true;
Yoshie Muranaka183c2752020-02-12 11:30:49 -080088 }
Derick Montague602e98a2020-10-21 16:20:00 -050089 },
jason westoverd36ac8a2025-11-03 20:58:59 -060090 onChangeHeaderCheckbox(tableRef, event) {
91 /*
92 * Bootstrap Vue Next Migration:
93 * Handle header checkbox to select/deselect all rows on current page.
94 */
95 if (!tableRef) return;
96
97 // Extract checked state from event (could be boolean or Event object)
98 const isChecked =
99 typeof event === 'boolean' ? event : event?.target?.checked;
100
101 if (isChecked) {
102 // Select all rows on the current page
103 const currentPage = this.currentPage || 1;
104 const perPage = this.perPage || 10;
105 const startIndex = (currentPage - 1) * perPage;
106 const allItems = tableRef.filteredItems || tableRef.items || [];
107 const endIndex = Math.min(startIndex + perPage, allItems.length);
108
109 for (let i = startIndex; i < endIndex; i++) {
110 tableRef.selectRow(i);
111 }
112 } else {
113 // Deselect all rows
114 tableRef.clearSelected();
115 // Manually trigger update since clearSelected might not fire @row-selected
116 this.selectedRows = [];
117 this.tableHeaderCheckboxModel = false;
118 this.tableHeaderCheckboxIndeterminate = false;
119 }
120
121 // onRowSelected will be triggered automatically for selections
122 },
Derick Montague602e98a2020-10-21 16:20:00 -0500123 },
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800124};
125
126export default BVTableSelectableMixin;