| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- import { debounce } from 'lodash'
- export default {
- props: {
- isTree: {
- type: Boolean,
- default: false
- },
- /**
- * 尺寸
- */
- size: {
- type: String
- },
- /**
- * @description 工具栏
- */
- toolbars: {
- type: [Array, Boolean]
- },
- /**
- * @description 查询表单
- */
- searchForm: {
- type: Object
- },
- /**
- * @description 表格标题
- */
- title: {
- type: String,
- default: ''
- },
- /**
- * @description 主键
- */
- pkKey: {
- type: String
- },
- /**
- * @description 表头数据
- */
- columns: {
- type: Array,
- required: true
- },
- /**
- * @description 表格加载
- */
- loading: {
- type: Boolean,
- default: false
- },
- /**
- * @description 表格加载配置
- */
- loadingOptions: {
- type: Object,
- default: null
- },
- /**
- * @description 表格配置
- */
- options: {
- type: Object,
- default: () => {
- return {
- border: true,
- stripe: true
- }
- }
- },
- /**
- * @description 索引
- */
- indexRow: {
- type: [Object, Boolean],
- default: true
- },
- /**
- * @description 多选
- */
- selectionRow: {
- type: [Object, Boolean],
- default: true
- },
- /* 自定义表单名称*/
- formName:{
- type: String
- },
- /**
- * @description 类型
- * [可选值] radio/checkbox
- */
- selectionType: {
- type: String,
- default: 'checkbox'
- },
- /**
- * @description 所有列表已选择的数据id
- *
- */
- selectionAll: {
- type: Object,
- default: null
- }
- },
- methods: {
- /* 表格換颜色*/
- tableRowClassName({row, rowIndex}) {
- if (rowIndex % 2 === 1) return "warning-row"
- return 'success-row'
- },
- setSelectionRadio(row) {
- const radio = row ? this.getPkValue(row) : ''
- if (radio !== this.selectionRadio) {
- this.$set(this, 'selectionRadio', radio)
- }
- },
- clearSelection() {
- if (this.selectionType === 'radio') {
- this.setSelectionRadio()
- } else {
- this.$refs.elTable.clearSelection()
- }
- },
- toggleRowSelection(row, selected) {
- if (this.selectionType !== 'radio') {
- this.$refs.elTable.toggleRowSelection(row, selected)
- }
- },
- toggleAllSelection() {
- this.$refs.elTable.toggleAllSelection()
- },
- toggleSelectionRow(row, selected) {
- const pkValue = this.getPkValue(row)
- for (var i = 0; i < this.ibpsData.length; i++) {
- const data = this.ibpsData[i]
- if (this.getPkValue(data) === pkValue) {
- this.$refs.elTable.toggleRowSelection(data, selected)
- }
- }
- },
- /**
- * @description 处理工具栏收缩/展开
- */
- handleCollapseExpandToolbar() {
- this.showToolbar = !this.showToolbar
- this.handleTableHeight(true)
- this.$emit('collapse-expand-toolbar', this.showToolbar)
- },
- /**
- * @description 行选中状态
- */
- handleCurrentChange(currentRow, oldCurrentRow) {
- this.$emit('current-change', currentRow, oldCurrentRow)
- },
- /**
- * @description 勾选数据时触发的事件
- */
- handleSelect(selection, row) {
- this.$emit('select', selection, row)
- },
- /**
- * @description 勾选全选时触发的事件
- */
- handleSelectAll(selection) {
- this.$emit('select-all', selection)
- },
- /**
- * @description 复选框选择项发生变化时触发的事件
- */
- handleSelectionChange(selection) {
- this.$selections = selection
- this.$emit('selection-change', selection)
- },
- /**
- * @description 单元格 hover 进入时触发的事件
- */
- handleCellMouseEnter(row, column, cell, event) {
- this.$emit('cell-mouse-enter', row, column, cell, event)
- },
- /**
- * @description 单元格 hover 退出时触发的事件
- */
- handleCellMouseLeave(row, column, cell, event) {
- this.$emit('cell-mouse-leave', row, column, cell, event)
- },
- /**
- * @description 单元格点击时触发的事件
- */
- handleCellClick(row, column, cell, event) {
- this.$emit('cell-click', row, column, cell, event)
- },
- /**
- * @description 单元格双击时触发的事件
- */
- handleCellDblclick(row, column, cell, event) {
- this.$emit('cell-dblclick', row, column, cell, event)
- },
- /**
- * @description 行点击时触发的事件
- */
- handleRowClick(row, event, column) {
- if (this.selectionType === 'radio') {
- this.setSelectionRadio(row)
- this.$emit('selection-change', row)
- } else {
- this.$refs.elTable.toggleRowSelection(row)
- }
- this.$emit('row-click', row, event, column)
- },
- /**
- * @description 行右键点击时触发的事件
- */
- handleRowContextmenu(row, event) {
- this.$emit('row-contextmenu', row, event)
- },
- /**
- * @description 行双击时触发的事件
- */
- handleRowDblclick(row, event) {
- this.$emit('row-dblclick', row, event)
- },
- /**
- * @description 表头点击时触发的事件
- */
- handleHeaderClick(column, event) {
- this.$emit('header-click', column, event)
- },
- /**
- * @description 当拖动表头改变了列的宽度的时候会触发该事件
- */
- handleHeaderDragend(newWidth, oldWidth, column, event) {
- debounce(this.handleTableHeight, 100)()
- this.$emit('header-dragend', newWidth, oldWidth, column, event)
- },
- /**
- * @description 表头右键点击时触发的事件
- */
- handleHeaderContextmenu(column, event) {
- this.$emit('header-contextmenu', column, event)
- },
- /**
- * @description 处理按钮的事件 【包含增删改查】
- * @param {*} button
- * @param {*} position
- * @param {*} data
- */
- handleActionEvent(action, position, data, index) {
- if (position === 'toolbar') {
- data = this.$selections
- }
- if (action.emit) {
- this.$emit(action.emit, {
- key: action.key,
- index: index,
- row: data,
- position: position,
- action: action })
- }
- const buttonKey = action.key
- let selection = null// 选中数据
- if (position === 'toolbar') { // 工具栏
- if (this.selectionAll) {
- selection = this.selectionAll.map(i => i.id_) // 默认给所有已选择的数据id
- } else {
- selection = this.getSelectedIds()
- }
- } else { // 管理列
- selection = data ? this.getPkValue(data) : null
- }
- this.$emit('action-event', buttonKey, position, selection, data, index, action)
- },
- /**
- * 根据key获取对象的值
- * 用于解决key值大小写不同的问题
- * @param {Object} data 需要从中获取值的对象
- */
- getPkValue(data, defaultValue = '') {
- const pkKey = this.pkKey || 'id'
- // 创建一个忽略大小写的正则对象
- const regx = new RegExp(`^${pkKey}$`, 'gi')
- // 循环正则匹配
- for (const key in data) {
- // 匹配成功返回值
- if (regx.test(key)) {
- return data[key]
- }
- }
- return defaultValue
- },
- /**
- * @description 获取选中的值
- */
- getSelectedIds() {
- if (this.selectionType === 'radio') {
- if (this.selectionRadio === null || this.selectionRadio === undefined) {
- return
- }
- return this.selectionRadio
- } else {
- if (this.$selections === null || this.$selections === undefined) {
- return
- }
- const ids = []
- this.$selections.map((item) => {
- ids.push(this.getPkValue(item))
- })
- return ids
- }
- }
- }
- }
|