columns.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { message } from "@/utils/message";
  2. import { tableData } from "../data";
  3. import { ref, computed } from "vue";
  4. // 需是hooks写法(函数中有return),避免失去响应性
  5. export function useColumns() {
  6. const search = ref("");
  7. const filterTableData = computed(() =>
  8. tableData.filter(
  9. data =>
  10. !search.value ||
  11. data.name.toLowerCase().includes(search.value.toLowerCase())
  12. )
  13. );
  14. const handleEdit = (index: number, row) => {
  15. message(`您修改了第 ${index} 行,数据为:${JSON.stringify(row)}`, {
  16. type: "success"
  17. });
  18. };
  19. const handleDelete = (index: number, row) => {
  20. message(`您删除了第 ${index} 行,数据为:${JSON.stringify(row)}`);
  21. };
  22. const columns: TableColumnList = [
  23. {
  24. prop: "date",
  25. // 自定义表头,slot用法 #nameHeader="{ column, $index }"
  26. headerSlot: "nameHeader"
  27. },
  28. {
  29. label: "姓名",
  30. prop: "name"
  31. },
  32. {
  33. label: "地址",
  34. prop: "address"
  35. },
  36. {
  37. align: "right",
  38. // 自定义表头,tsx用法
  39. headerRenderer: () => (
  40. <el-input
  41. v-model={search.value}
  42. size="small"
  43. clearable
  44. placeholder="Type to search"
  45. />
  46. ),
  47. cellRenderer: ({ index, row }) => (
  48. <>
  49. <el-button size="small" onClick={() => handleEdit(index + 1, row)}>
  50. Edit
  51. </el-button>
  52. <el-button
  53. size="small"
  54. type="danger"
  55. onClick={() => handleDelete(index + 1, row)}
  56. >
  57. Delete
  58. </el-button>
  59. </>
  60. )
  61. }
  62. ];
  63. return {
  64. columns,
  65. filterTableData
  66. };
  67. }