当前位置:   article > 正文

element ui table 表格 二次封装——支持自定义列,render渲染表格数据,支持单选。_element table 二次封装 自定义render方法渲染内容

element table 二次封装 自定义render方法渲染内容

基于element table 表格二次封装,可以自定义表头信息,支持 render 函数渲染表格数据。

新建一个elTable文件,封装支持多选,单选,表尾合计行等

组件封装内容

  1. <template>
  2. <div class="tabletemplate">
  3. <el-table
  4. row-key="id"
  5. :size="size"
  6. :data="tableData"
  7. v-loading="tableLoading"
  8. :row-style="{ border: 'none' }"
  9. show-overflow-tooltip
  10. style="width: 100%; margin-top: 14px"
  11. :highlight-current-row="highlighCurrentRow"
  12. @selection-change="handleSelectionChange"
  13. @current-change="handleRadioChange"
  14. :show-summary="showSummary"
  15. :sum-text="sumText"
  16. :summary-method="getSummaries"
  17. :span-method="arraySpanMethod"
  18. ref="multipleTable"
  19. @row-click="tableRowClick"
  20. :row-class-name="tableRowClassName"
  21. >
  22. <!-- 选择 v-if表格是够显示多选框 -->
  23. <el-table-column
  24. v-if="selectionShow"
  25. type="selection"
  26. align="center"
  27. :width="selectionWith"
  28. ></el-table-column>
  29. <!-- // 单选框,多选单选只能选则显示一个或者都不显示 -->
  30. <el-table-column v-if="radioShow" type="index" :width="radioWidth" align="center">
  31. <template slot-scope="scope">
  32. <el-radio v-model="defaultRadio" :label="scope.row.index">{{scope.row.index}} </el-radio>
  33. </template>
  34. </el-table-column>
  35. <!-- 是否显示序列号 -->
  36. <el-table-column
  37. v-if="isSerialNumber"
  38. type="index"
  39. align="center"
  40. width="50"
  41. ></el-table-column>
  42. <el-table-column
  43. align="left"
  44. v-for="column in tableColumns"
  45. :key="column.dataIndex"
  46. :label="column.title"
  47. :prop="column.dataIndex"
  48. :width="column.width"
  49. show-overflow-tooltip
  50. :sortable="column.sortable"
  51. >
  52. <template slot-scope="scope">
  53. <expandDom
  54. v-if="column.render"
  55. :render="column.render"
  56. :index="scope.$index"
  57. :column="column"
  58. :row="scope.row"
  59. ></expandDom>
  60. <span v-else>
  61. {{ scope.row[column.dataIndex] }}
  62. </span>
  63. </template>
  64. </el-table-column>
  65. </el-table>
  66. </div>
  67. </template>
  68. <script>
  69. export default {
  70. components: {
  71. /** render函数渲染组件* */
  72. expandDom: {
  73. functional: true,
  74. props: {
  75. row: Object,
  76. render: Function,
  77. index: Number,
  78. column: {
  79. type: Object,
  80. default: null,
  81. },
  82. },
  83. render: (h, ctx) => {
  84. const params = {
  85. row: ctx.props.row,
  86. index: ctx.props.index,
  87. };
  88. if (ctx.props.column) params.column = ctx.props.column;
  89. return ctx.props.render(h, params);
  90. },
  91. },
  92. },
  93. props: {
  94. // 是否显示序列号
  95. isSerialNumber: {
  96. type: Boolean,
  97. default: false,
  98. },
  99. tableData: {
  100. // 表格数据
  101. type: Array,
  102. required: true,
  103. default: function () {
  104. return [];
  105. },
  106. },
  107. tableColumns: {
  108. // 表格列数据
  109. type: Array,
  110. required: true,
  111. default: function () {
  112. return [];
  113. },
  114. },
  115. selectionWith: {
  116. type: Number,
  117. required: false,
  118. default: 50,
  119. },
  120. radioWidth: {
  121. type: Number,
  122. required: false,
  123. default: 50,
  124. },
  125. selectionShow: {
  126. // 多选 默认显示多选
  127. type: Boolean,
  128. default: false,
  129. },
  130. radioShow: {
  131. // 单选 使用单选应将多选置为false
  132. type: Boolean,
  133. default: false,
  134. },
  135. highlighCurrentRow: {
  136. type: Boolean,
  137. default: false,
  138. },
  139. tableLoading: {
  140. type: Boolean,
  141. default: false,
  142. },
  143. size: {
  144. type: String,
  145. default: "medium ",
  146. },
  147. showSummary: {
  148. type: Boolean,
  149. default: false,
  150. },
  151. sumText: {
  152. type: String,
  153. default: "合计",
  154. },
  155. radioVal: {
  156. type: Number,
  157. default: -1,
  158. },
  159. },
  160. data() {
  161. return {
  162. defaultRadio: -1,
  163. };
  164. },
  165. watch: {
  166. radioVal(newVal) {
  167. this.defaultRadio = newVal;
  168. },
  169. },
  170. methods: {
  171. tableRowClassName({ row, rowIndex }) {
  172. row.index = rowIndex;
  173. },
  174. tableRowClick(row, event, column) {
  175. this.$emit("tableRowClick", row, event, column);
  176. },
  177. // 合并行或列的计算方法
  178. arraySpanMethod({ row, column, rowIndex, columnIndex }) {
  179. this.$emit("arraySpanMethod", { value: { row, column, rowIndex, columnIndex } });
  180. },
  181. // 自定义的合计计算方法
  182. getSummaries(params) {
  183. this.$emit("getSummaries", params);
  184. },
  185. handleSelectionChange(val) {
  186. this.$emit("handleSelectionChange", val);
  187. },
  188. handleRadioChange(val) {
  189. if (val) {
  190. this.defaultRadio = val.index;
  191. }
  192. this.$emit("handleRadioChange", val);
  193. },
  194. clearSelection() {
  195. this.$refs["multipleTable"].clearSelection();
  196. },
  197. },
  198. };
  199. </script>
  200. <style lang="less" scoped>
  201. ::v-deep .el-radio__label {
  202. display: none;
  203. }
  204. </style>

在index.js注册公共组件,方便以后的开发,

  1. import elTable from '@/components/ElementUI/elTable'
  2. Vue.component("j-el-table", elTable);

 项目的入口文件引入index.js

import '@/components/index';

最后就可以在我们要写的页面里面去使用这个全局组件了,上代码

  1. <template>
  2. <div class="page">
  3. <j-el-table
  4. :tableData="tableData"
  5. :tableColumns="tableColumns"
  6. :tableLoading="tableLoading"
  7. :selectionShow="true"
  8. @handleSelectionChange="handleSelectionChange"
  9. />
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. data() {
  15. return {
  16. tableData: [],
  17. tableColumns: [],
  18. tableLoading: false,
  19. };
  20. },
  21. methods: {
  22. initTableColumns() {
  23. this.tableColumns = [
  24. {
  25. dataIndex: "username",
  26. title: "姓名",
  27. },
  28. {
  29. dataIndex: "name",
  30. title: "账户",
  31. },
  32. {
  33. dataIndex: "rc",
  34. title: "性别",
  35. },
  36. {
  37. dataIndex: "n",
  38. title: "手机号",
  39. },
  40. {
  41. dataIndex: "r",
  42. title: "组织部门",
  43. },
  44. {
  45. dataIndex: "ros",
  46. title: "用户状态",
  47. },
  48. {
  49. dataIndex: "createTime",
  50. title: "创建时间",
  51. },
  52. {
  53. width: "300",
  54. dataIndex: "operation",
  55. title: "操作",
  56. render: (h, params) => {
  57. return h("div", [
  58. h("a", {
  59. domProps: {
  60. innerHTML: "查看",
  61. },
  62. style: {
  63. marginRight: "10px",
  64. },
  65. on: {
  66. click: () => {
  67. alert("我是查看页面");
  68. },
  69. },
  70. }),
  71. h("a", {
  72. domProps: {
  73. innerHTML: "编辑",
  74. },
  75. style: {
  76. marginRight: "10px",
  77. },
  78. on: {
  79. click: () => {
  80. alert("点击进行编辑")
  81. },
  82. },
  83. }),
  84. h("a", {
  85. domProps: {
  86. innerHTML: "修改密码",
  87. },
  88. style: {
  89. marginRight: "10px",
  90. },
  91. on: {
  92. click: () => {
  93. alert("修改密码");
  94. },
  95. },
  96. }),
  97. h("a", {
  98. domProps: {
  99. innerHTML: "禁用",
  100. },
  101. style: {
  102. color: "#559df9",
  103. cursor: "pointer",
  104. marginRight: "10px",
  105. },
  106. on: {
  107. click: () => {
  108. alert("禁用");
  109. },
  110. },
  111. }),
  112. ]);
  113. },
  114. },
  115. ];
  116. },
  117. },
  118. };
  119. </script>
  120. <style lang="less" scoped></style>

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

闽ICP备14008679号