columns.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { ref, computed } from "vue";
  2. import { tableDataEdit } from "../data";
  3. import EditPen from "~icons/ep/edit-pen";
  4. import Check from "~icons/ep/check";
  5. export function useColumns() {
  6. const editMap = ref({});
  7. const activeIndex = ref(-1);
  8. const dataList = ref(tableDataEdit);
  9. const editing = computed(() => {
  10. return index => {
  11. return editMap.value[index]?.editing;
  12. };
  13. });
  14. const iconClass = computed(() => {
  15. return (index, other = false) => {
  16. return [
  17. "cursor-pointer",
  18. "ml-2",
  19. "transition",
  20. "delay-100",
  21. other
  22. ? ["hover:scale-110", "hover:text-red-500"]
  23. : editing.value(index) && ["scale-150", "text-red-500"]
  24. ];
  25. };
  26. });
  27. const columns: TableColumnList = [
  28. {
  29. label: "姓名(可修改)",
  30. prop: "name",
  31. cellRenderer: ({ row, index }) => (
  32. <div
  33. class="flex-bc w-full h-[32px]"
  34. onMouseenter={() => (activeIndex.value = index)}
  35. onMouseleave={() => onMouseleave(index)}
  36. >
  37. {!editing.value(index) ? (
  38. <p>{row.name}</p>
  39. ) : (
  40. <>
  41. <el-input v-model={row.name} />
  42. <iconify-icon-offline
  43. icon={Check}
  44. class={iconClass.value(index)}
  45. onClick={() => onSave(index)}
  46. />
  47. </>
  48. )}
  49. <iconify-icon-offline
  50. v-show={activeIndex.value === index && !editing.value(index)}
  51. icon={EditPen}
  52. class={iconClass.value(index, true)}
  53. onClick={() => onEdit(row, index)}
  54. />
  55. </div>
  56. )
  57. },
  58. {
  59. label: "地址",
  60. prop: "address"
  61. }
  62. ];
  63. function onMouseleave(index) {
  64. editing.value[index]
  65. ? (activeIndex.value = index)
  66. : (activeIndex.value = -1);
  67. }
  68. function onEdit(row, index) {
  69. editMap.value[index] = Object.assign({ ...row, editing: true });
  70. }
  71. function onSave(index) {
  72. editMap.value[index].editing = false;
  73. }
  74. return {
  75. columns,
  76. dataList
  77. };
  78. }