index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. :url="tableUrl"
  13. :condition="condition"
  14. :data="data"
  15. highlight-current-row
  16. :row-key="rowKey"
  17. :lazyLoad="lazyLoad"
  18. :load="load"
  19. :before-quest="handleBeforeQuest"
  20. :before-search="handleBeforeSearch"
  21. @selection-change="handleSelectionChange"
  22. @select="handleSelect"
  23. @select-all="handleCheckAll"
  24. @row-click="handleRowClick"
  25. v-bind="$attrs"
  26. v-on="$listeners"
  27. >
  28. <dg-table-column v-if="selection" type="selection" reserve-selection width="55" align="center" />
  29. <dg-table-column type="index" label="序号" width="75" align="center" />
  30. <template v-for="item in tableHeader">
  31. <dg-table-column :key="item[rowKey]" v-bind="item" align="center">
  32. <template slot-scope="{ row }">
  33. <span v-if="item.dateFormat">{{ row[item.prop] | dateFormatter(item.dateFormat) }}</span>
  34. <slot v-if="item.custom" v-bind:row="row" :name="item.prop"></slot>
  35. </template>
  36. </dg-table-column>
  37. </template>
  38. <slot></slot>
  39. <!--操作栏-->
  40. <dg-table-column label="操作" align="center" v-if="tableOptList.length" :width="optColumnWidth">
  41. <template slot-scope="{ row }">
  42. <div class="u-table__operation">
  43. <el-button
  44. v-for="(item, index) in tableOptList"
  45. :key="index"
  46. type="text"
  47. @click="handleEvent(item, row)"
  48. >
  49. {{ item }}
  50. </el-button>
  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. defaultSelectRows: {
  97. type: Array,
  98. default: () => []
  99. },
  100. // 设置合并行根据的key
  101. rowSpanKey: String
  102. },
  103. components: {},
  104. data() {
  105. return {
  106. newChooseArr: []
  107. }
  108. },
  109. computed: {},
  110. watch: {
  111. defaultSelectRows: {
  112. immediate: true,
  113. deep: true,
  114. handler(val) {
  115. this.newChooseArr = this.$lodash.cloneDeep(val)
  116. }
  117. }
  118. },
  119. methods: {
  120. /**
  121. * 查询
  122. */
  123. handleSearch() {
  124. this.$refs.baseTable.searchForm()
  125. },
  126. /**
  127. * 清除复选框选中的数据
  128. */
  129. handleClearSelection() {
  130. this.$refs.baseTable.clearAll()
  131. // this.newChooseArr = [];
  132. },
  133. toggleRowSelection(row, selected) {
  134. this.$refs.baseTable.toggleRowSelection(row, selected)
  135. },
  136. handleSelectionChange(data) {
  137. this.$emit("handleSelectionChange", data)
  138. },
  139. // getReqSearchCondition() {
  140. // return this.$refs.baseTable.getReqSearchCondition();
  141. // },
  142. handleBeforeSearch(conditions) {
  143. let searchConditionObj = JSON.parse(conditions.searchCondition)
  144. let result = []
  145. for (let i = 0; i < searchConditionObj.length; i++) {
  146. const { op, value } = searchConditionObj[i]
  147. if (op == "between" && value[0]) {
  148. result.push({ ...searchConditionObj[i], op: ">=", value: value[0] })
  149. result.push({ ...searchConditionObj[i], op: "<=", value: value[1] })
  150. } else {
  151. result.push(searchConditionObj[i])
  152. }
  153. }
  154. conditions.searchCondition = JSON.stringify(result)
  155. conditions.sort = JSON.stringify(this.sortProps)
  156. return conditions
  157. },
  158. objectSpanMethod({ row, column, rowIndex, columnIndex }) {
  159. if (columnIndex === 0) {
  160. return {
  161. rowspan: row.rowspan,
  162. colspan: 1
  163. }
  164. }
  165. },
  166. setRowSpans(tableData) {
  167. // 先给所有的数据都加一个v.rowspan = 1
  168. tableData.forEach(v => {
  169. v.rowspan = 1
  170. })
  171. // 双层循环
  172. for (let i = 0; i < tableData.length; i++) {
  173. // 内层循环,上面已经给所有的行都加了v.rowspan = 1
  174. // 这里进行判断
  175. // 如果当前行的id和下一行的id相等
  176. // 就把当前v.rowspan + 1
  177. // 下一行的v.rowspan - 1
  178. for (let j = i + 1; j < tableData.length; j++) {
  179. // 此处可根据相同字段进行合并,此处是根据的id
  180. if (tableData[i][this.rowSpanKey] === tableData[i][this.rowSpanKey]) {
  181. tableData[i].rowspan++
  182. tableData[j].rowspan--
  183. }
  184. }
  185. // 这里跳过已经重复的数据
  186. i = i + tableData[i].rowspan - 1
  187. }
  188. return tableData
  189. },
  190. /**
  191. * 进入页面默认选中设置
  192. */
  193. handleBeforeQuest(res) {
  194. const { content, totalElements } = res.data
  195. this.currentPageContent = content
  196. if (this.newChooseArr.length > 0) {
  197. let rowKeys = []
  198. for (let j = 0; j < content.length; j++) {
  199. const cItem = content[j]
  200. const index = this.newChooseArr.findIndex(item => (item[this.rowKey] || item) == cItem[this.rowKey])
  201. if (index > -1) {
  202. rowKeys.push(this.newChooseArr[index][this.rowKey] || this.newChooseArr[index])
  203. }
  204. }
  205. setTimeout(() => {
  206. this.$refs.baseTable.setCheck(rowKeys)
  207. }, 500)
  208. }
  209. const result = {
  210. data: {
  211. content: this.rowSpanKey ? this.setRowSpans(content) : content,
  212. totalElements
  213. }
  214. }
  215. return result
  216. },
  217. /**
  218. * 选中某条数据
  219. */
  220. handleSelect(selection, row) {
  221. let copyAttr = _.cloneDeep(this.newChooseArr)
  222. const index = this.newChooseArr.findIndex(item => (item[this.rowKey] || item) == row[this.rowKey])
  223. if (index > -1) {
  224. copyAttr.splice(index, 1)
  225. } else {
  226. copyAttr.push(row)
  227. }
  228. this.newChooseArr = copyAttr
  229. console.log(this.newChooseArr)
  230. },
  231. /**
  232. * 选中/取消选中
  233. */
  234. handleCheckAll(selection) {
  235. const checked = selection.length == 0 ? false : true
  236. this.currentPageContent.forEach(row => {
  237. // this.handleSelect([], element);
  238. let copyAttr = _.cloneDeep(this.newChooseArr)
  239. const index = this.newChooseArr.findIndex(item => (item[this.rowKey] || item) == row[this.rowKey])
  240. if (checked) {
  241. if (index < 0) {
  242. copyAttr.push(row)
  243. }
  244. } else {
  245. if (index > -1) {
  246. copyAttr.splice(index, 1)
  247. }
  248. }
  249. this.newChooseArr = copyAttr
  250. })
  251. console.log(this.newChooseArr)
  252. },
  253. // 发送操作事件类型
  254. handleEvent(type, row) {
  255. this.$emit("submitTableOpt", type, row)
  256. },
  257. /**
  258. * 表格某行被点击
  259. * */
  260. handleRowClick(row) {
  261. this.$refs.baseTable.setCurrentRow(row)
  262. this.$emit("handleRowClick", row)
  263. }
  264. },
  265. created() {},
  266. mounted() {}
  267. }
  268. </script>
  269. <style lang="scss"></style>