index.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <div>
  3. <el-alert
  4. effect="dark"
  5. :closable="false"
  6. title="基于 Element 的 Table 组件进行二次封装,实现数据驱动,支持所有 Table 参数 -- 多选"
  7. type="info"
  8. style="margin-bottom: 20px;"
  9. />
  10. <com-table
  11. ref="multipleTable"
  12. v-loading="loading"
  13. selection
  14. :columns="columns"
  15. :data="tableData"
  16. @selection-change="handleSelectionChange"
  17. />
  18. <div style="margin-top: 20px">
  19. <el-button @click="toggleSelection([tableData[1], tableData[2]])">切换第二、第三行的选中状态</el-button>
  20. <el-button @click="toggleSelection()">取消选择</el-button>
  21. </div>
  22. </div>
  23. </template>
  24. <script lang="ts">
  25. import { defineComponent, ref, unref } from 'vue'
  26. const columns = [
  27. {
  28. field: 'date',
  29. label: '日期'
  30. },
  31. {
  32. field: 'name',
  33. label: '姓名'
  34. },
  35. {
  36. field: 'address',
  37. label: '地址'
  38. }
  39. ]
  40. const tableData = [
  41. {
  42. date: '2016-05-02',
  43. name: '王小虎',
  44. address: '上海市普陀区金沙江路 1518 弄'
  45. }, {
  46. date: '2016-05-04',
  47. name: '王小虎',
  48. address: '上海市普陀区金沙江路 1517 弄'
  49. }, {
  50. date: '2016-05-01',
  51. name: '王小虎',
  52. address: '上海市普陀区金沙江路 1519 弄'
  53. }, {
  54. date: '2016-05-03',
  55. name: '王小虎',
  56. address: '上海市普陀区金沙江路 1516 弄'
  57. }
  58. ]
  59. export default defineComponent({
  60. // name: 'MultipleChoice',
  61. setup() {
  62. const loading = ref<boolean>(true)
  63. setTimeout(() => {
  64. loading.value = false
  65. }, 1000)
  66. const multipleTable = ref<HTMLElement | null>(null)
  67. function toggleSelection(rows: any[]) {
  68. const multipleTableRef = unref(multipleTable as any).getTableRef()
  69. if (rows) {
  70. rows.forEach(row => {
  71. multipleTableRef.toggleRowSelection(row)
  72. })
  73. } else {
  74. multipleTableRef.clearSelection()
  75. }
  76. }
  77. function handleSelectionChange(val: any) {
  78. console.log(val)
  79. }
  80. return {
  81. columns,
  82. tableData,
  83. loading,
  84. multipleTable, toggleSelection, handleSelectionChange
  85. }
  86. }
  87. })
  88. </script>
  89. <style>
  90. </style>