table.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <!--
  2. * @Author : yuanrunwei
  3. * @Date : 2021-11-01 18:02:58
  4. * @LastEditors: Please set LastEditors
  5. * @LastEditTime: 2022-01-11 18:42:02
  6. * @FilePath : /spfm-market-front/src/pages/main/performance/components/table.vue
  7. -->
  8. <template>
  9. <el-table
  10. class="simple-table"
  11. :data="computed_list"
  12. v-loading="loading"
  13. @selection-change="handleSelectionChange"
  14. >
  15. <el-table-column
  16. type="selection"
  17. width="55"
  18. :selectable="selectable"
  19. v-if="multiple"
  20. />
  21. <el-table-column
  22. v-for="(
  23. { props, label, type, width, align, children, dictionary, variable },
  24. index
  25. ) in config"
  26. :key="index"
  27. :prop="props"
  28. :width="width"
  29. :label="label"
  30. :align="align || 'center'"
  31. >
  32. <template #default="scope">
  33. <div v-if="type === 'edit'">
  34. <el-input
  35. v-if="scope.row[`edit`]"
  36. v-model="scope.row[props]"
  37. autosize
  38. type="textarea"
  39. @change="handleModify"
  40. />
  41. </div>
  42. <div v-if="type === 'number'">{{ scope.$index + 1 }}</div>
  43. <div v-else-if="type === 'textarea'">
  44. <pre class="simple-table-break">{{ scope.row[props] }}</pre>
  45. </div>
  46. <div v-else-if="type === 'date'">
  47. <div>{{ $formatDate(scope.row[props], "YYYY-MM-DD") }}</div>
  48. </div>
  49. <div v-else-if="type === 'time'">
  50. <div>
  51. {{ $formatDate(scope.row[props], "YYYY-MM-DD HH:00:00") }}
  52. </div>
  53. </div>
  54. <div v-else-if="type === 'click'">
  55. <div
  56. class="simple-table-click cursor-pointer"
  57. @click="handleClick(props, scope.row)"
  58. >
  59. {{ scope.row[props] }}
  60. </div>
  61. </div>
  62. <div v-else-if="type === 'dictionary'">
  63. {{ dictionary[scope.row[props]] }}
  64. </div>
  65. <div v-else>{{ scope.row[props] }}</div>
  66. </template>
  67. <template v-if="children">
  68. <el-table-column
  69. v-for="({ props, label, width, align, type }, index) in children"
  70. :key="index"
  71. :prop="props"
  72. :width="width"
  73. :label="label"
  74. :align="align || 'center'"
  75. >
  76. <template #default="scope">
  77. <div v-if="type === 'edit'">
  78. <el-input
  79. v-model="scope.row[props]"
  80. @change="handleModify"
  81. autosize
  82. type="textarea"
  83. />
  84. </div>
  85. <div v-else>{{ scope.row[props] }}</div>
  86. </template>
  87. <template v-if="variable">
  88. <div>{{ scope.row[props] }}</div>
  89. </template>
  90. </el-table-column>
  91. </template>
  92. </el-table-column>
  93. <el-table-column
  94. v-if="handleRow.length"
  95. label="操作"
  96. :align="'center'"
  97. :width="handleRow.length * 50"
  98. >
  99. <template slot-scope="scope">
  100. <span
  101. v-for="({ label, props, popconfirm, visible }, index) in handleRow"
  102. :key="index"
  103. class="padding-right-5"
  104. >
  105. <span v-if="handleFormat(visible, scope.row)">
  106. <el-popconfirm
  107. v-if="popconfirm"
  108. :title="`确定要${label}吗?`"
  109. @onConfirm="handleClick(props, scope.row)"
  110. @cancel="handleCancel"
  111. >
  112. <el-button slot="reference" type="text" size="small">{{
  113. label
  114. }}</el-button>
  115. </el-popconfirm>
  116. <el-button
  117. v-else
  118. @click="handleClick(props, scope.row)"
  119. type="text"
  120. size="small"
  121. >{{ label }}</el-button
  122. >
  123. </span>
  124. </span>
  125. </template>
  126. </el-table-column>
  127. </el-table>
  128. </template>
  129. <script>
  130. export default {
  131. props: {
  132. list: {
  133. type: Array,
  134. default: () => [],
  135. },
  136. config: {
  137. type: Array,
  138. default: () => [],
  139. },
  140. multiple: {
  141. type: Boolean,
  142. default: false,
  143. },
  144. selectable: {
  145. type: Function,
  146. },
  147. loading: {
  148. type: Boolean,
  149. default: false,
  150. },
  151. handleRow: {
  152. type: Array,
  153. default: () => [],
  154. },
  155. },
  156. computed: {
  157. computed_list() {
  158. return this.list.map((element) => ({
  159. ...element,
  160. }));
  161. },
  162. },
  163. methods: {
  164. handleFormat(params, data) {
  165. let visible = true;
  166. if (params) {
  167. visible = false;
  168. Object.keys(params).forEach((element) => {
  169. if (params[element].includes(data[element])) {
  170. visible = true;
  171. }
  172. });
  173. }
  174. return visible;
  175. },
  176. handleClick(props, row) {
  177. this.$emit(props, row);
  178. },
  179. handleCancel() {
  180. console.log("我被关闭了");
  181. },
  182. handleSelectionChange(val) {
  183. this.$emit("selection", val);
  184. },
  185. handleModify() {
  186. this.$emit("modify", this.computed_list);
  187. },
  188. },
  189. };
  190. </script>