<el-table
ref="tableRef"
:height="440"
:data="orgList"
v-loading="loading"
@row-click="handleClickRow"
@selection-change="handleSelectionChange"
@select="handleSelectChange"
@select-all="handleSelectAll"
>
<el-table-column type="selection"></el-table-column>
</el-table>
props:
initEmpArray: {
type: Array<any>,
default: [],
},
conversionKey: {
type: String,
default: "",
},
checkKey: {
type: String,
default: "id",
},
const handleSelectAll = (selection: any) => {
handleAddOrDelete(selection, orgList.value, selectedEmpList.value);
};
function handleSelectionChange(selection: any) {}
const handleSelectChange = (selection: any, row: any) => {
let isCheck = false;
if (
selection.filter((item: any) => item[props.checkKey] == row[props.checkKey])
.length != 0
) {
isCheck = true;
} else {
isCheck = false;
}
handleRowAddOrDelete(row, isCheck, selectedEmpList.value);
};
const handleRowAddOrDelete = (
row: any,
isCheck: boolean,
allSelectedData: any
) => {
if (isCheck) {
allSelectedData.push(row);
} else {
allSelectedData.forEach((item: any, index: number) => {
if (item[props.checkKey] == row[props.checkKey]) {
allSelectedData.splice(index, 1);
}
});
}
return allSelectedData;
};
const handleAddOrDelete = (
selection: any,
tableData: any,
allSelectedData: any
) => {
const noSelectedEmp = tableData.filter(
(item: any) =>
selection.findIndex(
(oItem: any) => oItem[props.checkKey] == item[props.checkKey]
) == -1
);
noSelectedEmp.map((noItem: any) => {
const index = allSelectedData.findIndex(
(sItem: any) => sItem[props.checkKey] == noItem[props.checkKey]
);
if (index != -1) {
allSelectedData.splice(index, 1);
}
});
selection.map((item: any) => {
const index = allSelectedData.findIndex(
(sItem: any) => sItem[props.checkKey] == item[props.checkKey]
);
if (index == -1) {
allSelectedData.push(item);
}
});
return allSelectedData;
};
const handleCheckTableData = (tableData: any, allSelectedData: any) => {
tableData.forEach((item: any, index: number) => {
const selectIndex = allSelectedData.findIndex(
(sItem: any) => sItem[props.checkKey] == item[props.checkKey]
);
if (selectIndex != -1) {
tableRef.value.toggleRowSelection(item, true);
}
});
};
getTableData(queryParams)
.then((res: any) => {
const data = res.data;
if (props.conversionKey && props.conversionKey != "") {
data.list.forEach((item: any) => {
item[props.checkKey] = item[props.conversionKey];
});
}
nextTick(() => {
handleCheckTableData(data.list, selectedEmpList.value);
});
})
.finally(() => {
loading.value = false;
});