data-table.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <div id="data-table">
  3. <el-table :data="data" border stripe size="small">
  4. <template v-for="(item, index) in fields">
  5. <el-table-column :key="index" align="center" :label="item.label" :prop="item.prop" :formatter="toFormatter"></el-table-column>
  6. </template>
  7. <template v-if="opera.length > 0">
  8. <el-table-column label="操作" align="center">
  9. <template v-slot="{ row, $index }">
  10. <template v-for="(item, index) in opera">
  11. <template v-if="display(item, row)">
  12. <el-tooltip :key="index" effect="dark" :content="item.label" placement="bottom">
  13. <el-button
  14. :key="index"
  15. type="text"
  16. :icon="item.icon || ''"
  17. size="mini"
  18. @click="handleOpera(row, item.method, item.confirm, item.methodZh, item.label)"
  19. ></el-button>
  20. </el-tooltip>
  21. </template>
  22. </template>
  23. </template>
  24. </el-table-column>
  25. </template>
  26. </el-table>
  27. </div>
  28. </template>
  29. <script>
  30. import _ from 'lodash';
  31. export default {
  32. name: 'data-table',
  33. props: {
  34. fields: { type: Array, required: true },
  35. data: { type: Array, required: true },
  36. opera: { type: Array, default: () => [] },
  37. toFormat: null,
  38. },
  39. components: {},
  40. data: () => ({}),
  41. created() {},
  42. computed: {},
  43. methods: {
  44. toFormatter(row, column, cellValue, index) {
  45. let this_fields = this.fields.filter(fil => fil.prop === column.property);
  46. if (this_fields.length > 0) {
  47. let format = _.get(this_fields[0], `format`, false);
  48. if (format) {
  49. let res;
  50. if (_.isFunction(format)) {
  51. res = format(cellValue);
  52. } else {
  53. res = this.toFormat({
  54. model: this_fields[0].prop,
  55. value: cellValue,
  56. });
  57. }
  58. return res;
  59. } else return cellValue;
  60. }
  61. },
  62. handleOpera(data, method, confirm = false, methodZh, label) {
  63. if (!methodZh) {
  64. methodZh = label;
  65. }
  66. if (confirm) {
  67. this.$confirm(`您确认${methodZh}该数据?`, '提示', {
  68. confirmButtonText: '确定',
  69. cancelButtonText: '取消',
  70. type: 'warning',
  71. })
  72. .then(() => {
  73. this.$emit(method, data);
  74. })
  75. .catch(() => {});
  76. } else {
  77. this.$emit(method, data);
  78. }
  79. },
  80. display(item, row) {
  81. let display = _.get(item, `display`, true);
  82. if (display === true) return true;
  83. else {
  84. let res = display(row);
  85. return res;
  86. }
  87. },
  88. },
  89. };
  90. </script>
  91. <style lang="less" scoped></style>