index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <!--
  2. 基础通用表格
  3. @Author: linqian
  4. @Date: 2021-07-07 17:11
  5. -->
  6. <template>
  7. <div>
  8. <!-- 列表 -->
  9. <dg-table
  10. border
  11. ref="baseTable"
  12. :auto-page="h=>h*0.6"
  13. :url="tableUrl"
  14. :condition="condition"
  15. :data="data"
  16. highlight-current-row
  17. :row-key="rowKey"
  18. :lazyLoad="lazyLoad"
  19. :load="load"
  20. :before-quest="handleBeforeQuest"
  21. :before-search="handleBeforeSearch"
  22. @selection-change="handleSelectionChange"
  23. @select="handleSelect"
  24. @select-all="handleCheckAll"
  25. @row-click="handleRowClick"
  26. >
  27. <dg-table-column v-if="selection" type="selection" reserve-selection width="55" align="center" />
  28. <dg-table-column type="index" label="序号" width="75" align="center" />
  29. <template v-for="item in tableHeader">
  30. <dg-table-column :key="item[rowKey]" v-bind="item" align="center">
  31. <template slot-scope="{ row }">
  32. <span v-if="item.dateFormat">{{ row[item.prop] | dateFormatter(item.dateFormat) }}</span>
  33. <slot v-if="item.custom" v-bind:row="row" :name="item.prop"></slot>
  34. </template>
  35. </dg-table-column>
  36. </template>
  37. <slot></slot>
  38. <!--操作栏-->
  39. <dg-table-column label="操作" align="center" v-if="tableOptList.length" :width="optColumnWidth">
  40. <template slot-scope="{ row }">
  41. <div class="u-table__operation">
  42. <el-tooltip
  43. v-for="(item, index) in tableOptList"
  44. :key="index"
  45. :content="item"
  46. effect="dark"
  47. placement="top-end"
  48. >
  49. <i :class="item | optIcon" @click="handleEvent(item, row)"></i>
  50. </el-tooltip>
  51. </div>
  52. </template>
  53. </dg-table-column>
  54. </dg-table>
  55. </div>
  56. </template>
  57. <script>
  58. import _ from 'lodash';
  59. import DgTable from '@srcPath/components/jz-base/table';
  60. export default {
  61. mixins: [DgTable],
  62. props: {
  63. tableUrl: String,
  64. tableHeader: Array,
  65. condition: {
  66. type: Object,
  67. default: () => {}
  68. },
  69. lazyLoad: {
  70. type: Boolean,
  71. default: false
  72. },
  73. load: {
  74. type: Function,
  75. default: (conditions) => conditions
  76. },
  77. rowKey: {
  78. type: String,
  79. default: 'id'
  80. },
  81. selection: {
  82. type: Boolean,
  83. default: false
  84. },
  85. // 列表操作
  86. tableOptList: {
  87. type: Array,
  88. default: () => []
  89. },
  90. // 操作列宽度
  91. optColumnWidth: {
  92. type: [String, Number],
  93. default: "120"
  94. }
  95. },
  96. components: {},
  97. data() {
  98. return {
  99. newChooseArr: []
  100. };
  101. },
  102. computed: {},
  103. methods: {
  104. /**
  105. * 查询
  106. */
  107. handleSearch() {
  108. this.$refs.baseTable.searchForm();
  109. },
  110. /**
  111. * 清除复选框选中的数据
  112. */
  113. handleClearSelection() {
  114. this.$refs.baseTable.clearAll();
  115. this.newChooseArr = [];
  116. },
  117. handleSelectionChange(data) {
  118. this.$emit('handleSelectionChange', data);
  119. },
  120. // getReqSearchCondition() {
  121. // return this.$refs.baseTable.getReqSearchCondition();
  122. // },
  123. handleBeforeSearch(conditions) {
  124. let searchConditionObj = JSON.parse(conditions.searchCondition);
  125. let result = [];
  126. for (let i = 0; i < searchConditionObj.length; i++) {
  127. const { op, value } = searchConditionObj[i];
  128. if (op == 'between' && value[0]) {
  129. result.push({ ...searchConditionObj[i], op: '>=', value: value[0] });
  130. result.push({ ...searchConditionObj[i], op: '<=', value: value[1] });
  131. } else {
  132. result.push(searchConditionObj[i]);
  133. }
  134. }
  135. conditions.searchCondition = JSON.stringify(result);
  136. conditions.sort = JSON.stringify(this.sortProps);
  137. return conditions;
  138. },
  139. /**
  140. * 进入页面默认选中设置
  141. */
  142. handleBeforeQuest(res) {
  143. const { content, totalElements } = res.data;
  144. this.currentPageContent = content;
  145. if (this.newChooseArr.length > 0) {
  146. let rowKeys = [];
  147. for (let j = 0; j < content.length; j++) {
  148. const cItem = content[j];
  149. const index = this.newChooseArr.findIndex((item) => (item[this.rowKey] || item) == cItem[this.rowKey]);
  150. if (index > -1) {
  151. rowKeys.push(this.newChooseArr[index][this.rowKey] || this.newChooseArr[index]);
  152. }
  153. }
  154. setTimeout(() => {
  155. this.$refs.baseTable.setCheck(rowKeys);
  156. }, 500);
  157. }
  158. const result = {
  159. data: {
  160. content,
  161. totalElements
  162. }
  163. };
  164. return result;
  165. },
  166. /**
  167. * 选中某条数据
  168. */
  169. handleSelect(selection, row) {
  170. let copyAttr = _.cloneDeep(this.newChooseArr);
  171. const index = this.newChooseArr.findIndex((item) => (item[this.rowKey] || item) == row[this.rowKey]);
  172. if (index > -1) {
  173. copyAttr.splice(index, 1);
  174. } else {
  175. copyAttr.push(row);
  176. }
  177. this.newChooseArr = copyAttr;
  178. console.log(this.newChooseArr);
  179. },
  180. /**
  181. * 选中/取消选中
  182. */
  183. handleCheckAll(selection) {
  184. const checked = selection.length == 0 ? false : true;
  185. this.currentPageContent.forEach((row) => {
  186. // this.handleSelect([], element);
  187. let copyAttr = _.cloneDeep(this.newChooseArr);
  188. const index = this.newChooseArr.findIndex((item) => (item[this.rowKey] || item) == row[this.rowKey]);
  189. if (checked) {
  190. if (index < 0) {
  191. copyAttr.push(row);
  192. }
  193. } else {
  194. if (index > -1) {
  195. copyAttr.splice(index, 1);
  196. }
  197. }
  198. this.newChooseArr = copyAttr;
  199. });
  200. console.log(this.newChooseArr);
  201. },
  202. // 发送操作事件类型
  203. handleEvent(type, row) {
  204. this.$emit('submitTableOpt', type, row);
  205. },
  206. /**
  207. * 表格某行被点击
  208. * */
  209. handleRowClick(row) {
  210. this.$refs.baseTable.setCurrentRow(row);
  211. this.$emit('handleRowClick', row);
  212. }
  213. },
  214. created() {},
  215. mounted() {}
  216. };
  217. </script>
  218. <style lang='scss'>
  219. </style>