base.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. indexMethod (index) {
  109. if (this.pagination) {
  110. return (this.currentPage - 1) * this.pageSize + index + 1
  111. } else {
  112. return index + 1
  113. }
  114. },
  115. /* 表格換颜色*/
  116. tableRowClassName ({ row, rowIndex }) {
  117. if (rowIndex % 2 === 1) return 'warning-row'
  118. return 'success-row'
  119. },
  120. setSelectionRadio (row) {
  121. const radio = row ? this.getPkValue(row) : ''
  122. if (radio !== this.selectionRadio) {
  123. this.$set(this, 'selectionRadio', radio)
  124. }
  125. },
  126. clearSelection () {
  127. if (this.selectionType === 'radio') {
  128. this.setSelectionRadio()
  129. } else {
  130. this.$refs.elTable.clearSelection()
  131. }
  132. },
  133. toggleRowSelection (row, selected) {
  134. if (this.selectionType !== 'radio') {
  135. this.$refs.elTable.toggleRowSelection(row, selected)
  136. }
  137. },
  138. toggleAllSelection () {
  139. this.$refs.elTable.toggleAllSelection()
  140. },
  141. toggleSelectionRow (row, selected) {
  142. const pkValue = this.getPkValue(row)
  143. for (var i = 0; i < this.ibpsData.length; i++) {
  144. const data = this.ibpsData[i]
  145. if (this.getPkValue(data) === pkValue) {
  146. this.$refs.elTable.toggleRowSelection(data, selected)
  147. }
  148. }
  149. },
  150. /**
  151. * @description 处理工具栏收缩/展开
  152. */
  153. handleCollapseExpandToolbar () {
  154. this.showToolbar = !this.showToolbar
  155. this.handleTableHeight(true)
  156. this.$emit('collapse-expand-toolbar', this.showToolbar)
  157. },
  158. /**
  159. * @description 行选中状态
  160. */
  161. handleCurrentChange (currentRow, oldCurrentRow) {
  162. this.$emit('current-change', currentRow, oldCurrentRow)
  163. },
  164. /**
  165. * @description 勾选数据时触发的事件
  166. */
  167. handleSelect (selection, row) {
  168. this.$emit('select', selection, row)
  169. },
  170. /**
  171. * @description 勾选全选时触发的事件
  172. */
  173. handleSelectAll (selection) {
  174. this.$emit('select-all', selection)
  175. },
  176. /**
  177. * @description 复选框选择项发生变化时触发的事件
  178. */
  179. handleSelectionChange (selection) {
  180. this.$selections = selection
  181. this.$emit('selection-change', selection)
  182. },
  183. /**
  184. * @description 单元格 hover 进入时触发的事件
  185. */
  186. handleCellMouseEnter (row, column, cell, event) {
  187. this.$emit('cell-mouse-enter', row, column, cell, event)
  188. },
  189. /**
  190. * @description 单元格 hover 退出时触发的事件
  191. */
  192. handleCellMouseLeave (row, column, cell, event) {
  193. this.$emit('cell-mouse-leave', row, column, cell, event)
  194. },
  195. /**
  196. * @description 单元格点击时触发的事件
  197. */
  198. handleCellClick (row, column, cell, event) {
  199. this.$emit('cell-click', row, column, cell, event)
  200. },
  201. /**
  202. * @description 单元格双击时触发的事件
  203. */
  204. handleCellDblclick (row, column, cell, event) {
  205. this.$emit('cell-dblclick', row, column, cell, event)
  206. },
  207. /**
  208. * @description 行点击时触发的事件
  209. */
  210. handleRowClick (row, event, column) {
  211. if (this.selectionType === 'radio') {
  212. this.setSelectionRadio(row)
  213. this.$emit('selection-change', row)
  214. } else {
  215. this.$refs.elTable.toggleRowSelection(row)
  216. }
  217. this.$emit('row-click', row, event, column)
  218. },
  219. /**
  220. * @description 行右键点击时触发的事件
  221. */
  222. handleRowContextmenu (row, event) {
  223. this.$emit('row-contextmenu', row, event)
  224. },
  225. /**
  226. * @description 行双击时触发的事件
  227. */
  228. handleRowDblclick (row, event) {
  229. this.$emit('row-dblclick', row, event)
  230. },
  231. /**
  232. * @description 表头点击时触发的事件
  233. */
  234. handleHeaderClick (column, event) {
  235. this.$emit('header-click', column, event)
  236. },
  237. /**
  238. * @description 当拖动表头改变了列的宽度的时候会触发该事件
  239. */
  240. handleHeaderDragend (newWidth, oldWidth, column, event) {
  241. debounce(this.handleTableHeight, 100)()
  242. this.$emit('header-dragend', newWidth, oldWidth, column, event)
  243. },
  244. /**
  245. * @description 表头右键点击时触发的事件
  246. */
  247. handleHeaderContextmenu (column, event) {
  248. this.$emit('header-contextmenu', column, event)
  249. },
  250. /**
  251. * @description 处理按钮的事件 【包含增删改查】
  252. * @param {*} button
  253. * @param {*} position
  254. * @param {*} data
  255. */
  256. handleActionEvent (action, position, data, index) {
  257. if (position === 'toolbar') {
  258. data = this.$selections
  259. }
  260. if (action.emit) {
  261. this.$emit(action.emit, {
  262. key: action.key,
  263. index: index,
  264. row: data,
  265. position: position,
  266. action: action
  267. })
  268. }
  269. const buttonKey = action.key
  270. let selection = null// 选中数据
  271. if (position === 'toolbar') { // 工具栏
  272. if (this.selectionAll) {
  273. selection = this.selectionAll.map(i => i.id_) // 默认给所有已选择的数据id
  274. } else {
  275. selection = this.getSelectedIds()
  276. }
  277. } else { // 管理列
  278. selection = data ? this.getPkValue(data) : null
  279. }
  280. this.$emit('action-event', buttonKey, position, selection, data, index, action)
  281. },
  282. /**
  283. * 根据key获取对象的值
  284. * 用于解决key值大小写不同的问题
  285. * @param {Object} data 需要从中获取值的对象
  286. */
  287. getPkValue (data, defaultValue = '') {
  288. const pkKey = this.pkKey || 'id'
  289. // 创建一个忽略大小写的正则对象
  290. const regx = new RegExp(`^${pkKey}$`, 'gi')
  291. // 循环正则匹配
  292. for (const key in data) {
  293. // 匹配成功返回值
  294. if (regx.test(key)) {
  295. return data[key]
  296. }
  297. }
  298. return defaultValue
  299. },
  300. /**
  301. * @description 获取选中的值
  302. */
  303. getSelectedIds () {
  304. if (this.selectionType === 'radio') {
  305. if (this.selectionRadio === null || this.selectionRadio === undefined) {
  306. return
  307. }
  308. return this.selectionRadio
  309. } else {
  310. if (this.$selections === null || this.$selections === undefined) {
  311. return
  312. }
  313. const ids = []
  314. this.$selections.map((item) => {
  315. ids.push(this.getPkValue(item))
  316. })
  317. return ids
  318. }
  319. }
  320. }
  321. }