base.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import { debounce } from 'lodash'
  2. export default {
  3. props: {
  4. isTree: {
  5. type: Boolean,
  6. default: false
  7. },
  8. /**
  9. * 尺寸
  10. */
  11. size: {
  12. type: String
  13. },
  14. /**
  15. * @description 工具栏
  16. */
  17. toolbars: {
  18. type: [Array, Boolean]
  19. },
  20. /**
  21. * @description 查询表单
  22. */
  23. searchForm: {
  24. type: Object
  25. },
  26. /**
  27. * @description 表格标题
  28. */
  29. title: {
  30. type: String,
  31. default: ''
  32. },
  33. /**
  34. * @description 主键
  35. */
  36. pkKey: {
  37. type: String
  38. },
  39. /**
  40. * @description 表头数据
  41. */
  42. columns: {
  43. type: Array,
  44. required: true
  45. },
  46. /**
  47. * @description 表格加载
  48. */
  49. loading: {
  50. type: Boolean,
  51. default: false
  52. },
  53. /**
  54. * @description 表格加载配置
  55. */
  56. loadingOptions: {
  57. type: Object,
  58. default: null
  59. },
  60. /**
  61. * @description 表格配置
  62. */
  63. options: {
  64. type: Object,
  65. default: () => {
  66. return {
  67. border: true,
  68. stripe: true
  69. }
  70. }
  71. },
  72. /**
  73. * @description 索引
  74. */
  75. indexRow: {
  76. type: [Object, Boolean],
  77. default: true
  78. },
  79. /**
  80. * @description 多选
  81. */
  82. selectionRow: {
  83. type: [Object, Boolean],
  84. default: true
  85. },
  86. /* 自定义表单名称*/
  87. formName:{
  88. type: String
  89. },
  90. /**
  91. * @description 类型
  92. * [可选值] radio/checkbox
  93. */
  94. selectionType: {
  95. type: String,
  96. default: 'checkbox'
  97. },
  98. /**
  99. * @description 所有列表已选择的数据id
  100. *
  101. */
  102. selectionAll: {
  103. type: Object,
  104. default: null
  105. }
  106. },
  107. methods: {
  108. /* 表格換颜色*/
  109. tableRowClassName({row, rowIndex}) {
  110. if (rowIndex % 2 === 1) return "warning-row"
  111. return 'success-row'
  112. },
  113. setSelectionRadio(row) {
  114. const radio = row ? this.getPkValue(row) : ''
  115. if (radio !== this.selectionRadio) {
  116. this.$set(this, 'selectionRadio', radio)
  117. }
  118. },
  119. clearSelection() {
  120. if (this.selectionType === 'radio') {
  121. this.setSelectionRadio()
  122. } else {
  123. this.$refs.elTable.clearSelection()
  124. }
  125. },
  126. toggleRowSelection(row, selected) {
  127. if (this.selectionType !== 'radio') {
  128. this.$refs.elTable.toggleRowSelection(row, selected)
  129. }
  130. },
  131. toggleAllSelection() {
  132. this.$refs.elTable.toggleAllSelection()
  133. },
  134. toggleSelectionRow(row, selected) {
  135. const pkValue = this.getPkValue(row)
  136. for (var i = 0; i < this.ibpsData.length; i++) {
  137. const data = this.ibpsData[i]
  138. if (this.getPkValue(data) === pkValue) {
  139. this.$refs.elTable.toggleRowSelection(data, selected)
  140. }
  141. }
  142. },
  143. /**
  144. * @description 处理工具栏收缩/展开
  145. */
  146. handleCollapseExpandToolbar() {
  147. this.showToolbar = !this.showToolbar
  148. this.handleTableHeight(true)
  149. this.$emit('collapse-expand-toolbar', this.showToolbar)
  150. },
  151. /**
  152. * @description 行选中状态
  153. */
  154. handleCurrentChange(currentRow, oldCurrentRow) {
  155. this.$emit('current-change', currentRow, oldCurrentRow)
  156. },
  157. /**
  158. * @description 勾选数据时触发的事件
  159. */
  160. handleSelect(selection, row) {
  161. this.$emit('select', selection, row)
  162. },
  163. /**
  164. * @description 勾选全选时触发的事件
  165. */
  166. handleSelectAll(selection) {
  167. this.$emit('select-all', selection)
  168. },
  169. /**
  170. * @description 复选框选择项发生变化时触发的事件
  171. */
  172. handleSelectionChange(selection) {
  173. this.$selections = selection
  174. this.$emit('selection-change', selection)
  175. },
  176. /**
  177. * @description 单元格 hover 进入时触发的事件
  178. */
  179. handleCellMouseEnter(row, column, cell, event) {
  180. this.$emit('cell-mouse-enter', row, column, cell, event)
  181. },
  182. /**
  183. * @description 单元格 hover 退出时触发的事件
  184. */
  185. handleCellMouseLeave(row, column, cell, event) {
  186. this.$emit('cell-mouse-leave', row, column, cell, event)
  187. },
  188. /**
  189. * @description 单元格点击时触发的事件
  190. */
  191. handleCellClick(row, column, cell, event) {
  192. this.$emit('cell-click', row, column, cell, event)
  193. },
  194. /**
  195. * @description 单元格双击时触发的事件
  196. */
  197. handleCellDblclick(row, column, cell, event) {
  198. this.$emit('cell-dblclick', row, column, cell, event)
  199. },
  200. /**
  201. * @description 行点击时触发的事件
  202. */
  203. handleRowClick(row, event, column) {
  204. if (this.selectionType === 'radio') {
  205. this.setSelectionRadio(row)
  206. this.$emit('selection-change', row)
  207. } else {
  208. this.$refs.elTable.toggleRowSelection(row)
  209. }
  210. this.$emit('row-click', row, event, column)
  211. },
  212. /**
  213. * @description 行右键点击时触发的事件
  214. */
  215. handleRowContextmenu(row, event) {
  216. this.$emit('row-contextmenu', row, event)
  217. },
  218. /**
  219. * @description 行双击时触发的事件
  220. */
  221. handleRowDblclick(row, event) {
  222. this.$emit('row-dblclick', row, event)
  223. },
  224. /**
  225. * @description 表头点击时触发的事件
  226. */
  227. handleHeaderClick(column, event) {
  228. this.$emit('header-click', column, event)
  229. },
  230. /**
  231. * @description 当拖动表头改变了列的宽度的时候会触发该事件
  232. */
  233. handleHeaderDragend(newWidth, oldWidth, column, event) {
  234. debounce(this.handleTableHeight, 100)()
  235. this.$emit('header-dragend', newWidth, oldWidth, column, event)
  236. },
  237. /**
  238. * @description 表头右键点击时触发的事件
  239. */
  240. handleHeaderContextmenu(column, event) {
  241. this.$emit('header-contextmenu', column, event)
  242. },
  243. /**
  244. * @description 处理按钮的事件 【包含增删改查】
  245. * @param {*} button
  246. * @param {*} position
  247. * @param {*} data
  248. */
  249. handleActionEvent(action, position, data, index) {
  250. if (position === 'toolbar') {
  251. data = this.$selections
  252. }
  253. if (action.emit) {
  254. this.$emit(action.emit, {
  255. key: action.key,
  256. index: index,
  257. row: data,
  258. position: position,
  259. action: action })
  260. }
  261. const buttonKey = action.key
  262. let selection = null// 选中数据
  263. if (position === 'toolbar') { // 工具栏
  264. if (this.selectionAll) {
  265. selection = this.selectionAll.map(i => i.id_) // 默认给所有已选择的数据id
  266. } else {
  267. selection = this.getSelectedIds()
  268. }
  269. } else { // 管理列
  270. selection = data ? this.getPkValue(data) : null
  271. }
  272. this.$emit('action-event', buttonKey, position, selection, data, index, action)
  273. },
  274. /**
  275. * 根据key获取对象的值
  276. * 用于解决key值大小写不同的问题
  277. * @param {Object} data 需要从中获取值的对象
  278. */
  279. getPkValue(data, defaultValue = '') {
  280. const pkKey = this.pkKey || 'id'
  281. // 创建一个忽略大小写的正则对象
  282. const regx = new RegExp(`^${pkKey}$`, 'gi')
  283. // 循环正则匹配
  284. for (const key in data) {
  285. // 匹配成功返回值
  286. if (regx.test(key)) {
  287. return data[key]
  288. }
  289. }
  290. return defaultValue
  291. },
  292. /**
  293. * @description 获取选中的值
  294. */
  295. getSelectedIds() {
  296. if (this.selectionType === 'radio') {
  297. if (this.selectionRadio === null || this.selectionRadio === undefined) {
  298. return
  299. }
  300. return this.selectionRadio
  301. } else {
  302. if (this.$selections === null || this.$selections === undefined) {
  303. return
  304. }
  305. const ids = []
  306. this.$selections.map((item) => {
  307. ids.push(this.getPkValue(item))
  308. })
  309. return ids
  310. }
  311. }
  312. }
  313. }