0


vue element ui table行点击添加自定义行背景色

1.在table添加cell-style属性

  1. :cell-style="TableCellStyle"

2.在methods中添加TableCellStyle

  1. TableCellStyle(row) {
  2. if (this.row === row.row) {
  3. return "background-color:#214F81;color: #ffffff !important;";
  4. } else {
  5. return "background-color:transparent;";
  6. }
  7. },

3.在table添加row-click

  1. @row-click="RowClickFun"

4.在methods中添加RowClickFun

  1. RowClickFun(row, column, event) {
  2. console.log("row, column, event", row, column, event);
  3. debounce(() => {
  4. this.row = row;
  5. }, 500);
  6. },

5.代码

tablecpt组件,tablecpt/index.vue

  1. <template>
  2. <div class="TableComponent">
  3. <el-table
  4. :data="TableData"
  5. style="width: 100%; height: 100%"
  6. :row-class-name="tableRowClassName"
  7. :header-cell-style="tableHeaderColor"
  8. @row-click="RowClickFun"
  9. :cell-style="TableCellStyle"
  10. >
  11. <el-table-column
  12. prop="chargeMessage"
  13. label="时间"
  14. show-overflow-tooltip
  15. align="center"
  16. >
  17. <template slot-scope="scope">
  18. <div class="chargeMessageDiv">
  19. {{ scope.row.chargeMessage }}
  20. </div>
  21. </template>
  22. </el-table-column>
  23. <el-table-column prop="chargeUrl" label="名字" align="center">
  24. <template slot-scope="scope">
  25. <div class="chargeUrlDiv">
  26. {{ scope.row.chargeUrl }}
  27. </div>
  28. </template>
  29. </el-table-column>
  30. <el-table-column prop="rate" label="速率" align="center">
  31. <template slot-scope="scope">
  32. <div class="rateDiv">
  33. {{ scope.row.rate }}
  34. </div>
  35. </template>
  36. </el-table-column>
  37. <el-table-column prop="chargeType" label="类型" align="center">
  38. <template slot-scope="scope">
  39. <div class="chargeTypeDiv">
  40. {{ scope.row.chargeType }}
  41. </div>
  42. </template>
  43. </el-table-column>
  44. <el-table-column label="点击" align="center" width="90">
  45. <template slot-scope="scope">
  46. <div class="SetLocationDivOutbox">
  47. <div
  48. class="SetLocationDiv iconfont icon-shuichang"
  49. @click.stop="DWFun(scope.row, scope.$index)"
  50. :class="
  51. ActiveIconStyleStatus && ActiveIconNowIndex == scope.$index
  52. ? 'ActiveIconStyle'
  53. : 'NormalIconStyle'
  54. "
  55. ></div>
  56. </div>
  57. </template>
  58. </el-table-column>
  59. </el-table>
  60. </div>
  61. </template>
  62. <script>
  63. import debounce from "@/utils/debounce";
  64. import Bus from "@/utils/bus.js";
  65. import { mapGetters } from "vuex";
  66. import AxiosUrl from "@/config/AxiosUrl";
  67. import mixin from "@/pages/HomePage/mixin";
  68. export default {
  69. name: "TableComponent",
  70. mixins: [mixin],
  71. props: ["TableData"],
  72. computed: {
  73. ...mapGetters({}),
  74. },
  75. data() {
  76. return {
  77. row: {},
  78. ActiveIconStyleStatus: false,
  79. ActiveIconNowIndex: null,
  80. };
  81. },
  82. mounted() {
  83. this.$nextTick(() => {
  84. this.row = this.TableData[0];
  85. });
  86. },
  87. methods: {
  88. // 修改table header的背景色
  89. tableHeaderColor({ row, column, rowIndex, columnIndex }) {
  90. if (rowIndex === 0) {
  91. return this.themeName == "default"
  92. ? "background-color: #102C51"
  93. : "background-color: #102C51";
  94. }
  95. },
  96. // 修改table row的背景色
  97. tableRowClassName({ row, rowIndex }) {
  98. if (rowIndex % 2 == 0) {
  99. if (row.IsDispatch == 1) {
  100. return "DispatchRowStyle";
  101. }
  102. return "evenRowStyle";
  103. } else {
  104. if (row.IsDispatch == 1) {
  105. return "DispatchRowStyle";
  106. }
  107. return "oddRowStyle";
  108. }
  109. },
  110. RowClickFun(row, column, event) {
  111. console.log("row, column, event", row, column, event);
  112. debounce(() => {
  113. this.row = row;
  114. }, 500);
  115. },
  116. TableCellStyle(row) {
  117. if (this.row === row.row) {
  118. return "background-color:#214F81;color: #ffffff !important;";
  119. } else {
  120. return "background-color:transparent;";
  121. }
  122. },
  123. DWFun(row, index) {
  124. debounce(() => {
  125. if (this.ActiveIconNowIndex == index) {
  126. this.ActiveIconStyleStatus = !this.ActiveIconStyleStatus;
  127. this.ActiveIconNowIndex = this.ActiveIconStyleStatus ? index : null;
  128. } else {
  129. this.ActiveIconNowIndex = index;
  130. this.ActiveIconStyleStatus = true;
  131. }
  132. if (this.ActiveIconStyleStatus) {
  133. console.log("%c可以执行", "color:green", row);
  134. } else {
  135. console.log("%c不可以执行", "color:red");
  136. }
  137. }, 500);
  138. },
  139. },
  140. };
  141. </script>
  142. <style lang="scss">
  143. .el-table td.el-table__cell,
  144. .el-table th.el-table__cell.is-leaf {
  145. border-bottom: none;
  146. }
  147. .el-table--border::after,
  148. .el-table--group::after,
  149. .el-table::before {
  150. content: "";
  151. position: absolute;
  152. background-color: transparent;
  153. z-index: 1;
  154. }
  155. .el-table,
  156. .el-table__expanded-cell {
  157. background-color: transparent;
  158. }
  159. </style>
  160. <style lang="scss" scoped>
  161. .TableComponent {
  162. width: 100%;
  163. height: 100%;
  164. background: transparent;
  165. ::v-deep .el-table {
  166. width: 100%;
  167. height: 100% !important;
  168. tr {
  169. background-color: transparent;
  170. }
  171. .cell {
  172. line-height: 48px;
  173. }
  174. tbody tr:hover > td {
  175. background: inherit;
  176. cursor: pointer;
  177. }
  178. .el-table__header-wrapper {
  179. background: #102c51;
  180. th {
  181. font-size: 28px;
  182. font-family: Microsoft YaHei !important;
  183. font-weight: 500 !important;
  184. color: #fff;
  185. }
  186. .el-table__cell {
  187. height: 48px;
  188. padding: 0;
  189. }
  190. }
  191. .el-table__body-wrapper {
  192. background: transparent;
  193. height: calc(100% - 48px) !important;
  194. overflow-x: hidden;
  195. overflow-y: auto;
  196. &::-webkit-scrollbar {
  197. width: 5px;
  198. height: 5px;
  199. background-color: #07192f;
  200. background-color: transparent;
  201. }
  202. /*定义滚动条轨道 内阴影+圆角*/
  203. &::-webkit-scrollbar-track {
  204. // -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
  205. border-radius: 2px;
  206. background-color: #07192f;
  207. background-color: transparent;
  208. }
  209. /*定义滑块 内阴影+圆角*/
  210. &::-webkit-scrollbar-thumb {
  211. border-radius: 20px;
  212. // -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, .3);
  213. background-color: #07192f;
  214. //background-color: transparent;
  215. }
  216. .el-table__row {
  217. .el-table__cell {
  218. height: 48px;
  219. font-size: 28px;
  220. font-family: Microsoft YaHei !important;
  221. font-weight: 400 !important;
  222. color: #fff;
  223. padding: 0;
  224. }
  225. }
  226. }
  227. .oddRowStyle {
  228. background: #102c51;
  229. }
  230. .evenRowStyle {
  231. background: transparent;
  232. }
  233. .DispatchRowStyle {
  234. background: red;
  235. div {
  236. color: #ffffff !important;
  237. }
  238. }
  239. .chargeMessageDiv {
  240. overflow: hidden;
  241. text-overflow: ellipsis;
  242. white-space: nowrap;
  243. }
  244. .DepositionDivOutbox {
  245. display: flex;
  246. flex-flow: row nowrap;
  247. justify-content: center;
  248. align-items: center;
  249. width: 100%;
  250. height: 100%;
  251. .DepositionDiv {
  252. display: flex;
  253. flex-flow: row nowrap;
  254. justify-content: center;
  255. align-items: center;
  256. width: 80%;
  257. height: 38px;
  258. border-radius: 4px;
  259. }
  260. .LH_activeColor {
  261. background: #0aa0de;
  262. }
  263. .QW_activeColor {
  264. background: #2fa652;
  265. }
  266. .ZD_activeColor {
  267. background: #de910a;
  268. }
  269. .YZ_activeColor {
  270. background: #cb563d;
  271. }
  272. }
  273. .PlanDivOutbox {
  274. display: flex;
  275. flex-flow: row nowrap;
  276. justify-content: center;
  277. align-items: center;
  278. width: 100%;
  279. height: 100%;
  280. .PlanDiv {
  281. display: flex;
  282. flex-flow: row nowrap;
  283. justify-content: center;
  284. align-items: center;
  285. width: 100%;
  286. height: 38px;
  287. border-radius: 4px;
  288. }
  289. .activeColor {
  290. background-color: #da2f2f;
  291. }
  292. }
  293. .SetLocationDivOutbox {
  294. display: flex;
  295. flex-flow: row nowrap;
  296. justify-content: center;
  297. align-items: center;
  298. width: 100%;
  299. height: 100%;
  300. .SetLocationDiv {
  301. font-size: 28px;
  302. }
  303. .ActiveIconStyle {
  304. color: #00eaff;
  305. }
  306. .NormalIconStyle {
  307. color: #fff;
  308. }
  309. }
  310. }
  311. }
  312. </style>

引入tablecpt组件

  1. <template>
  2. <div class="HomePage">
  3. <div class="HomePageOutbox">
  4. <component :is="'tablecpt'" :TableData="chargeInfoList"></component>
  5. </div>
  6. </div>
  7. </template>
  8. <script>
  9. import debounce from "@/utils/debounce";
  10. import Bus from "@/utils/bus.js";
  11. import { mapGetters } from "vuex";
  12. import AxiosUrl from "@/config/AxiosUrl";
  13. import { setTheme, themeList } from "@/themeConfig/setTheme.js";
  14. import mixin from "@/pages/HomePage/mixin";
  15. import tablecpt from "./tablecpt/index.vue";
  16. export default {
  17. name: "HomePage",
  18. mixins: [mixin],
  19. components: {
  20. tablecpt,
  21. },
  22. computed: {
  23. ...mapGetters({
  24. }),
  25. },
  26. data() {
  27. return {
  28. themeList: themeList,
  29. setTheme: setTheme,
  30. chargeInfoList: [
  31. {
  32. rate: 1280,
  33. chargeUrl: "热狗",
  34. chargeMessage: "2022-05-02",
  35. chargeType:2,
  36. },
  37. {
  38. rate: 1920,
  39. chargeUrl: "姚中仁",
  40. chargeMessage: "2021-05-02",
  41. chargeType: 3,
  42. },
  43. {
  44. rate: 3200,
  45. chargeUrl: "哈狗帮",
  46. chargeMessage: "2019-05-02",
  47. chargeType: 0,
  48. },
  49. {
  50. rate: 9990,
  51. chargeUrl: "遥远的梦",
  52. chargeMessage: "2016-05-02",
  53. chargeType: 1,
  54. },
  55. ],
  56. };
  57. },
  58. mounted() {
  59. //this.getTimeNowStausfun()
  60. // this.$store.commit('HomePageModule/set_TestData', "data")
  61. // this.$store.dispatch('HomePageModule/TestDatafun', data)
  62. },
  63. methods: {
  64. },
  65. };
  66. </script>
  67. <style lang="scss" scoped>
  68. .HomePage {
  69. display: flex;
  70. flex-flow: row nowrap;
  71. justify-content: center;
  72. align-items: center;
  73. width: 100%;
  74. height: 100%;
  75. .HomePageOutbox {
  76. width: calc(100% - 60px);
  77. height: calc(100% - 60px);
  78. background: rgba(0, 0, 71, 0.825);
  79. }
  80. }
  81. </style>

mixin文件

  1. import debounce from "@/utils/debounce";
  2. import Bus from "@/utils/bus.js";
  3. import {
  4. mapGetters
  5. } from "vuex";
  6. import AxiosUrl from "@/config/AxiosUrl";
  7. const mixin = {
  8. filters: {
  9. formatLongText(value) {
  10. if (value === undefined || value === null || value === "") {
  11. return "暂无";
  12. } else if (value.length > 8) {
  13. return value.substr(0, 8) + "...";
  14. } else {
  15. return value;
  16. }
  17. },
  18. ellipsis(value, limit) {
  19. if (!value) return "";
  20. if (value.length > limit) {
  21. return value.slice(0, limit) + "...";
  22. }
  23. return value;
  24. },
  25. },
  26. data() {
  27. return {
  28. publicPath: process.env.BASE_URL,
  29. }
  30. },
  31. computed: {
  32. ...mapGetters({
  33. GetTestData: "HomePageModule/GetTestData",
  34. }),
  35. },
  36. created() {},
  37. mounted() {},
  38. methods: {
  39. //初始化、定时1分钟刷新数据
  40. getTimeNowStausfun() {
  41. if (this.NowStaustimerH) {
  42. window.clearInterval(this.NowStaustimerH);
  43. this.NowStaustimerH = null;
  44. }
  45. this.GetData();
  46. this.NowStaustimerH = setInterval(() => {
  47. }, 60000);
  48. this.$once("hook:beforeDestroy", () => {
  49. console.log("清除定时器NowStaustimerH");
  50. window.clearInterval(this.NowStaustimerH);
  51. this.NowStaustimerH = null;
  52. });
  53. },
  54. GetData() {
  55. this.$axios
  56. .post(AxiosUrl.HomePageUrlPath + "GetPredictData")
  57. .then((res) => {
  58. console.log("res", res);
  59. });
  60. },
  61. },
  62. }
  63. export default mixin
标签: css vue.js css3

本文转载自: https://blog.csdn.net/qq_37312180/article/details/128168351
版权归原作者 volodyan 所有, 如有侵权,请联系我们删除。

“vue element ui table行点击添加自定义行背景色”的评论:

还没有评论