table.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // table 纯前端示例
  2. <template>
  3. <div>
  4. <el-table
  5. :data="tableData"
  6. @selection-change="handleSelectionChange"
  7. border
  8. ref="multipleTable"
  9. stripe
  10. style="width: 100%"
  11. tooltip-effect="dark"
  12. >
  13. <el-table-column type="selection" width="55"></el-table-column>
  14. <el-table-column label="日期" width="120">
  15. <template slot-scope="scope">{{ scope.row.date }}</template>
  16. </el-table-column>
  17. <el-table-column label="姓名" prop="name" width="120"></el-table-column>
  18. <el-table-column label="年龄" prop="age" width="120"></el-table-column>
  19. <el-table-column label="住址" prop="address" min-width="200" show-overflow-tooltip></el-table-column>
  20. <el-table-column label="是否禁用" prop="switch" width="180">
  21. <template slot-scope="scope">
  22. <el-switch active-text="开启" inactive-text="禁用" v-model="scope.row.switch"></el-switch>
  23. </template>
  24. </el-table-column>
  25. <el-table-column label="按钮组" width="200">
  26. <template slot-scope="scope" >
  27. <el-button type="text" size="small" @click="toggleSelection([scope.row])">按钮1</el-button>
  28. <el-button type="text" size="small" @click="toggleSelection([scope.row])">按钮2</el-button>
  29. <el-button type="text" size="small" @click="toggleSelection([scope.row])">按钮3</el-button>
  30. </template>
  31. </el-table-column>
  32. </el-table>
  33. <div style="margin-top: 20px">
  34. <el-button @click="toggleSelection([tableData[1], tableData[2]])">切换第二、第三行的选中状态</el-button>
  35. <el-button @click="toggleSelection()">取消选择</el-button>
  36. </div>
  37. </div>
  38. </template>
  39. <script>
  40. export default {
  41. name: 'Table',
  42. data() {
  43. return {
  44. tableData: [
  45. {
  46. date: '2016-05-03',
  47. name: '王小虎',
  48. age: 12,
  49. address: '上海市普陀区金沙江路 1518 弄',
  50. switch:true
  51. },
  52. {
  53. date: '2016-05-02',
  54. name: '王小虎',
  55. age: 12,
  56. address: '上海市普陀区金沙江路 1518 弄',
  57. switch:true
  58. },
  59. {
  60. date: '2016-05-04',
  61. name: '王小虎',
  62. age: 12,
  63. address: '上海市普陀区金沙江路 1518 弄',
  64. switch:true
  65. },
  66. {
  67. date: '2016-05-01',
  68. name: '王小虎',
  69. age: 12,
  70. address: '上海市普陀区金沙江路 1518 弄',
  71. switch:false
  72. },
  73. {
  74. date: '2016-05-08',
  75. name: '王小虎',
  76. age: 12,
  77. address: '上海市普陀区金沙江路 1518 弄',
  78. switch:true
  79. },
  80. {
  81. date: '2016-05-06',
  82. name: '王小虎',
  83. age: 12,
  84. address: '上海市普陀区金沙江路 1518 弄',
  85. switch:true
  86. },
  87. {
  88. date: '2016-05-07',
  89. name: '王小虎',
  90. age: 12,
  91. address: '上海市普陀区金沙江路 1518 弄',
  92. switch:false
  93. }
  94. ],
  95. multipleSelection: []
  96. }
  97. },
  98. methods: {
  99. toggleSelection(rows) {
  100. if (rows) {
  101. rows.forEach(row => {
  102. this.$refs.multipleTable.toggleRowSelection(row)
  103. })
  104. } else {
  105. this.$refs.multipleTable.clearSelection()
  106. }
  107. },
  108. handleSelectionChange(val) {
  109. this.multipleSelection = val
  110. }
  111. }
  112. }
  113. </script>