当前位置:   article > 正文

element table实现选中改变背景颜色_

 1.重现代码如下: 

  1. <el-table
  2. id="self-table"
  3. :data="sdfslist"
  4. height="400"
  5. border
  6. :row-key="getRowKeys"
  7. @selection-change="handleSelectionChange"
  8. @select="selectOne"
  9. style="width: 100%"
  10. ref="multipleTable"
  11. v-loading="listLoading"
  12. :row-style="tableRowClassName">
  13. <el-table-column
  14. type="selection"
  15. :reserve-selection="true"
  16. width="55"
  17. ></el-table-column>
  18. ......
  19. ......
  20. ......
  21. <el-table-column
  22. fixed
  23. style="background: #ffcc99"
  24. label="姓名"
  25. min-width="230px"
  26. align="center"
  27. >
  28. <template slot-scope="{ row }">
  29. <span>{{ row.closeno }}</span>
  30. </template>
  31. </el-table-column>
  32. <el-table-column
  33. fixed
  34. style="background: #ffcc99"
  35. label="标号"
  36. min-width="200px"
  37. align="center"
  38. >
  39. <template slot-scope="{ row }">
  40. <span>{{ row.clsa}}</span>
  41. </template>
  42. </el-table-column>
  43. </el-table>
  44. <el-checkbox v-model="allCheck" @change="allCheckEvent">全选</el-checkbox>
  45. data() {
  46. return {
  47. sdfslist: [],
  48. tableKey: 0,
  49. multipleSelection: [],
  50. allCheck: false,
  51. };
  52. },
  53. watch: {
  54. sdfslist: {
  55. handler(value) {
  56. if (this.allCheck) {
  57. let that = this;
  58. let len = that.multipleSelection.length;
  59. value.forEach((row) => {
  60. for (let i = 0; i < len; i++) {
  61. if (row.autoid === that.multipleSelection[i].autoid) {
  62. that.$refs.multipleTable.toggleRowSelection(row, false);
  63. break;
  64. } else {
  65. that.$refs.multipleTable.toggleRowSelection(row, true);
  66. }
  67. }
  68. });
  69. }
  70. },
  71. deep: true,
  72. },
  73. },
  74. methods:{
  75. selectOne() {
  76. if (this.allCheck) {
  77. this.allCheck = false;
  78. }
  79. },
  80. getRowKeys(row) {
  81. return row.autoid; //唯一标识例如id...等
  82. },
  83. allCheckEvent() {
  84. if (this.allCheck) {
  85. this.sdfslist.forEach((row) => {
  86. this.$refs.multipleTable.toggleRowSelection(row, true);
  87. });
  88. } else {
  89. this.$refs.multipleTable.clearSelection();
  90. }
  91. },
  92. tableRowClassName({ row, rowIndex }){
  93. //判断是否相等,相同时改变背景颜色
  94. let list = this.multipleSelection.find((item) => item.autoid === row.autoid);
  95. if (list !== undefined) {
  96. rowBackground.background = "#9FBBFF"
  97. //rowBackground.color = "red" //字体颜色
  98. return rowBackground
  99. } else {
  100. return "";
  101. }
  102. },
  103. },

2.效果图:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/107865
推荐阅读
相关标签