table.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <!--
  2. * @Author : yuanrunwei
  3. * @Date : 2021-11-01 18:02:58
  4. * @LastEditors: Please set LastEditors
  5. * @LastEditTime: 2022-01-12 13:19:40
  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 flex"
  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-if="type === 'file'">
  66. <div v-if="scope.row[props] && scope.row[props].length">
  67. <span
  68. v-for="(item, index) in scope.row[props].split(',')"
  69. :key="index"
  70. @click="downloadFile(item)"
  71. class="simple-table-click cursor-pointer margin-left-10"
  72. >
  73. {{ item }}
  74. </span>
  75. </div>
  76. <div v-else>{{ scope.row[props] }}</div>
  77. </div>
  78. <div v-else>{{ scope.row[props] }}</div>
  79. </template>
  80. <template v-if="children">
  81. <el-table-column
  82. v-for="({ props, label, width, align, type }, index) in children"
  83. :key="index"
  84. :prop="props"
  85. :width="width"
  86. :label="label"
  87. :align="align || 'center'"
  88. >
  89. <template #default="scope">
  90. <div v-if="type === 'edit'">
  91. <el-input
  92. v-model="scope.row[props]"
  93. @change="handleModify"
  94. autosize
  95. type="textarea"
  96. />
  97. </div>
  98. <div v-else>{{ scope.row[props] }}</div>
  99. </template>
  100. <template v-if="variable">
  101. <div>{{ scope.row[props] }}</div>
  102. </template>
  103. </el-table-column>
  104. </template>
  105. </el-table-column>
  106. <el-table-column
  107. v-if="handleRow.length"
  108. label="操作"
  109. :align="'center'"
  110. :width="handleRow.length * 50"
  111. >
  112. <template slot-scope="scope">
  113. <span
  114. v-for="({ label, props, popconfirm, visible }, index) in handleRow"
  115. :key="index"
  116. class="padding-right-5"
  117. >
  118. <span v-if="handleFormat(visible, scope.row)">
  119. <el-popconfirm
  120. v-if="popconfirm"
  121. :title="`确定要${label}吗?`"
  122. @onConfirm="handleClick(props, scope.row)"
  123. @cancel="handleCancel"
  124. >
  125. <el-button slot="reference" type="text" size="small">{{
  126. label
  127. }}</el-button>
  128. </el-popconfirm>
  129. <el-button
  130. v-else
  131. @click="handleClick(props, scope.row)"
  132. type="text"
  133. size="small"
  134. >{{ label }}</el-button
  135. >
  136. </span>
  137. </span>
  138. </template>
  139. </el-table-column>
  140. </el-table>
  141. </template>
  142. <script>
  143. export default {
  144. props: {
  145. list: {
  146. type: Array,
  147. default: () => [],
  148. },
  149. config: {
  150. type: Array,
  151. default: () => [],
  152. },
  153. multiple: {
  154. type: Boolean,
  155. default: false,
  156. },
  157. selectable: {
  158. type: Function,
  159. },
  160. loading: {
  161. type: Boolean,
  162. default: false,
  163. },
  164. handleRow: {
  165. type: Array,
  166. default: () => [],
  167. },
  168. },
  169. computed: {
  170. computed_list() {
  171. return this.list.map((element) => ({
  172. ...element,
  173. }));
  174. },
  175. },
  176. methods: {
  177. handleFormat(params, data) {
  178. let visible = true;
  179. if (params) {
  180. visible = false;
  181. Object.keys(params).forEach((element) => {
  182. if (params[element].includes(data[element])) {
  183. visible = true;
  184. }
  185. });
  186. }
  187. return visible;
  188. },
  189. handleClick(props, row) {
  190. this.$emit(props, row);
  191. },
  192. handleCancel() {
  193. console.log("我被关闭了");
  194. },
  195. handleSelectionChange(val) {
  196. this.$emit("selection", val);
  197. },
  198. downloadFile(val) {
  199. this.$emit("download", val);
  200. },
  201. handleModify() {
  202. this.$emit("modify", this.computed_list);
  203. },
  204. },
  205. };
  206. </script>