data-table.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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)"
  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. },
  38. components: {},
  39. data: () => ({}),
  40. created() {},
  41. computed: {},
  42. methods: {
  43. toFormatter(row, column, cellValue, index) {
  44. let this_fields = this.fields.filter(fil => fil.prop === column.property);
  45. if (this_fields.length > 0) {
  46. let format = _.get(this_fields[0], `format`, false);
  47. if (format) {
  48. let res = format(cellValue);
  49. return res;
  50. } else return cellValue;
  51. }
  52. },
  53. handleOpera(data, method, confirm = false, methodZh = '操作') {
  54. if (confirm) {
  55. this.$confirm(`您确认${methodZh}该数据?`, '提示', {
  56. confirmButtonText: '确定',
  57. cancelButtonText: '取消',
  58. type: 'warning',
  59. })
  60. .then(() => {
  61. this.$emit(method, data);
  62. })
  63. .catch(() => {});
  64. } else {
  65. this.$emit(method, data);
  66. }
  67. },
  68. display(item, row) {
  69. let display = _.get(item, `display`, true);
  70. if (display === true) return true;
  71. else {
  72. let res = display(row);
  73. return res;
  74. }
  75. },
  76. },
  77. };
  78. </script>
  79. <style lang="less" scoped></style>