lite-grid.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <el-table border style="width: 100%;overflow: auto;" :size="options.size || 'mini'" v-bind="options" :data="data">
  3. <slot name="pre"> </slot>
  4. <slot>
  5. <el-table-column
  6. v-for="(item, index) in listFields"
  7. :key="'field' + index"
  8. :label="item.label"
  9. :prop="item.name"
  10. :formatter="item.formatter"
  11. v-bind="item.options"
  12. show-overflow-tooltip
  13. />
  14. </slot>
  15. <slot name="ext"> </slot>
  16. <slot name="oper">
  17. <el-table-column label="操作" :width="options.operWidth || '100'" v-if="!readonly">
  18. <template slot-scope="scope">
  19. <el-button
  20. v-for="(item, index) in operItems"
  21. :key="'field' + index"
  22. @click="handleOper(item, scope.row, scope.$index)"
  23. type="text"
  24. :size="options.size || 'mini'"
  25. >
  26. <el-tooltip v-if="item.icon" :content="item.label"><i :class="item.icon"></i></el-tooltip>
  27. <span v-else>{{ item.label }}</span>
  28. </el-button>
  29. </template>
  30. </el-table-column>
  31. </slot>
  32. <slot name="post"> </slot>
  33. </el-table>
  34. </template>
  35. <script>
  36. import _ from 'lodash';
  37. import { FieldMeta, Operation, Formatter, MergeFilters } from './meta-util';
  38. export default {
  39. name: 'lite-grid',
  40. props: {
  41. meta: { type: Array, required: true },
  42. readonly: Boolean /* 是否显示操作列 */,
  43. options: {
  44. type: Object,
  45. default: () => ({ size: 'mini' }),
  46. } /* 表格扩展属性 */,
  47. operation: {
  48. default: () => [['edit', '编辑', 'el-icon-edit'], ['delete', '删除', 'el-icon-delete', true]],
  49. } /* 操作类型 */,
  50. data: Array,
  51. },
  52. methods: {
  53. async handleOper({ event, label, confirm }, data, index) {
  54. try {
  55. if (confirm) {
  56. const msg = _.isString(confirm) ? confirm : `是否${label}此数据?`;
  57. await this.$confirm(msg, '请确认', {
  58. confirmButtonText: '确定',
  59. cancelButtonText: '取消',
  60. type: 'warning',
  61. });
  62. }
  63. this.$emit(event, data, index);
  64. this.$emit('oper', { event, data, index });
  65. } catch (err) {
  66. if (err == 'cancel') {
  67. this.$message({
  68. type: 'info',
  69. message: `已取消${label}`,
  70. });
  71. }
  72. }
  73. },
  74. },
  75. computed: {
  76. listFields() {
  77. const res = this.meta
  78. .map(FieldMeta)
  79. .filter(p => p.slots.list)
  80. // .sort((a, b) => b.order - a.order)
  81. .map(p => ({
  82. ...p.field,
  83. formatter: Formatter(p, this),
  84. options: MergeFilters(p, this),
  85. }));
  86. // console.log('listFields: ', res);
  87. return res;
  88. },
  89. operItems() {
  90. return Operation(this.operation);
  91. },
  92. },
  93. };
  94. </script>
  95. <style lang="less" scoped>
  96. .el-table {
  97. height: 100%;
  98. overflow: auto;
  99. }
  100. .el-button + .el-button {
  101. margin-left: 5px;
  102. }
  103. </style>