FixedThead.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <div class="app-container">
  3. <div class="filter-container">
  4. <el-checkbox-group v-model="checkboxVal">
  5. <el-checkbox label="apple">
  6. apple
  7. </el-checkbox>
  8. <el-checkbox label="banana">
  9. banana
  10. </el-checkbox>
  11. <el-checkbox label="orange">
  12. orange
  13. </el-checkbox>
  14. </el-checkbox-group>
  15. </div>
  16. <el-table :key="key" :data="tableData" border fit highlight-current-row style="width: 100%">
  17. <el-table-column prop="name" label="fruitName" width="180" />
  18. <el-table-column v-for="fruit in formThead" :key="fruit" :label="fruit">
  19. <template slot-scope="scope">
  20. {{ scope.row[fruit] }}
  21. </template>
  22. </el-table-column>
  23. </el-table>
  24. </div>
  25. </template>
  26. <script>
  27. const defaultFormThead = ['apple', 'banana']
  28. export default {
  29. data() {
  30. return {
  31. tableData: [
  32. {
  33. name: 'fruit-1',
  34. apple: 'apple-10',
  35. banana: 'banana-10',
  36. orange: 'orange-10'
  37. },
  38. {
  39. name: 'fruit-2',
  40. apple: 'apple-20',
  41. banana: 'banana-20',
  42. orange: 'orange-20'
  43. }
  44. ],
  45. key: 1, // table key
  46. formTheadOptions: ['apple', 'banana', 'orange'],
  47. checkboxVal: defaultFormThead, // checkboxVal
  48. formThead: defaultFormThead // 默认表头 Default header
  49. }
  50. },
  51. watch: {
  52. checkboxVal(valArr) {
  53. this.formThead = this.formTheadOptions.filter(i => valArr.indexOf(i) >= 0)
  54. this.key = this.key + 1// 为了保证table 每次都会重渲 In order to ensure the table will be re-rendered each time
  55. }
  56. }
  57. }
  58. </script>