useSelection.ts 913 B

12345678910111213141516171819202122232425262728293031323334
  1. import { ref, computed } from "vue";
  2. /**
  3. * @description 表格多选数据操作
  4. * @param {String} rowKey 当表格可以多选时,所指定的 id
  5. * */
  6. export const useSelection = (rowKey: string = "id") => {
  7. const isSelected = ref<boolean>(false);
  8. const selectedList = ref<{ [key: string]: any }[]>([]);
  9. // 当前选中的所有 ids 数组
  10. const selectedListIds = computed((): string[] => {
  11. let ids: string[] = [];
  12. selectedList.value.forEach(item => ids.push(item[rowKey]));
  13. return ids;
  14. });
  15. /**
  16. * @description 多选操作
  17. * @param {Array} rowArr 当前选择的所有数据
  18. * @return void
  19. */
  20. const selectionChange = (rowArr: { [key: string]: any }[]) => {
  21. rowArr.length ? (isSelected.value = true) : (isSelected.value = false);
  22. selectedList.value = rowArr;
  23. };
  24. return {
  25. isSelected,
  26. selectedList,
  27. selectedListIds,
  28. selectionChange
  29. };
  30. };