Skip to content

Commit 1d70c0c

Browse files
expose selectRowsByIndex and selectRowsByIds methods with table comp
1 parent 16f4105 commit 1d70c0c

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

client/packages/lowcoder/src/comps/comps/tableComp/tableComp.tsx

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,76 @@ TableTmpComp = withMethodExposing(TableTmpComp, [
704704
comp.children.selection.children.selectedRowKey.dispatchChangeValueAction(allKeys[0] || "0");
705705
comp.children.selection.children.selectedRowKeys.dispatchChangeValueAction(allKeys);
706706
},
707-
},
707+
},
708+
{
709+
method: {
710+
name: "selectRowsByIndex",
711+
description: "Select rows by index",
712+
params: [
713+
{ name: "rowIndexes", type: "arrayNumberString"},
714+
]
715+
},
716+
execute: (comp, values) => {
717+
const rowIndexes = values[0];
718+
if (!isArray(rowIndexes)) {
719+
return Promise.reject("selectRowsByIndex function only accepts array of string or number i.e. ['1', '2', '3'] or [1, 2, 3]")
720+
}
721+
const displayData = comp.filterData ?? [];
722+
const selectedKeys: string[] = rowIndexes
723+
.map((index) => {
724+
const numIndex = Number(index);
725+
if (isNaN(numIndex) || numIndex < 0 || numIndex >= displayData.length) {
726+
return null;
727+
}
728+
return displayData[numIndex][OB_ROW_ORI_INDEX];
729+
})
730+
.filter((key): key is string => key !== null);
731+
732+
comp.children.selection.children.selectedRowKey.dispatchChangeValueAction(selectedKeys[0] || "0");
733+
comp.children.selection.children.selectedRowKeys.dispatchChangeValueAction(selectedKeys);
734+
},
735+
},
736+
{
737+
method: {
738+
name: "selectRowsByIds",
739+
description: "Select rows by ids",
740+
params: [
741+
{ name: "rowIds", type: "arrayNumberString"},
742+
]
743+
},
744+
execute: (comp, values) => {
745+
const rowIds = values[0];
746+
if (!isArray(rowIds)) {
747+
return Promise.reject("selectRowsByIds function only accepts array of string or number i.e. ['1', '2', '3'] or [1, 2, 3]")
748+
}
749+
const displayData = comp.filterData ?? [];
750+
751+
// Common ID field names to check
752+
const idFields = ['id', 'ID', 'Id', 'key', 'Key', 'KEY'];
753+
754+
const selectedKeys: string[] = rowIds
755+
.map((id) => {
756+
// First try to find by common ID fields
757+
for (const field of idFields) {
758+
const foundRow = displayData.find((row) => {
759+
const fieldValue = row[field];
760+
return fieldValue !== undefined && String(fieldValue) === String(id);
761+
});
762+
if (foundRow) {
763+
return foundRow[OB_ROW_ORI_INDEX];
764+
}
765+
}
766+
767+
// If no ID field found, fall back to comparing with OB_ROW_ORI_INDEX
768+
const foundRow = displayData.find((row) => row[OB_ROW_ORI_INDEX] === String(id));
769+
return foundRow ? foundRow[OB_ROW_ORI_INDEX] : null;
770+
})
771+
.filter((key): key is string => key !== null);
772+
773+
comp.children.selection.children.selectedRowKey.dispatchChangeValueAction(selectedKeys[0] || "0");
774+
comp.children.selection.children.selectedRowKeys.dispatchChangeValueAction(selectedKeys);
775+
},
776+
},
708777
{
709778
method: {
710779
name: "cancelChanges",

0 commit comments

Comments
 (0)