Commit 8aea275c by Sendya

refactor: s-table

parent 00be0c86
import T from 'ant-design-vue/es/table/Table' import T from 'ant-design-vue/es/table/Table'
import get from 'lodash.get' import get from 'lodash.get'
export default { export default {
data() { data() {
return { return {
...@@ -38,8 +39,14 @@ export default { ...@@ -38,8 +39,14 @@ export default {
type: String, type: String,
default: 'default' default: 'default'
}, },
/**
* {
* show: true,
* clear: Function
* }
*/
alert: { alert: {
type: Object, type: [Object, Boolean],
default: null default: null
}, },
/** @Deprecated */ /** @Deprecated */
...@@ -57,7 +64,7 @@ export default { ...@@ -57,7 +64,7 @@ export default {
name: this.$route.name, name: this.$route.name,
params: Object.assign({}, this.$route.params, { params: Object.assign({}, this.$route.params, {
pageNo: val pageNo: val
}), })
}) })
}, },
pageNum(val) { pageNum(val) {
...@@ -66,13 +73,11 @@ export default { ...@@ -66,13 +73,11 @@ export default {
}) })
}, },
pageSize(val) { pageSize(val) {
console.log('pageSize:', val)
Object.assign(this.localPagination, { Object.assign(this.localPagination, {
pageSize: val pageSize: val
}) })
}, },
showSizeChanger(val) { showSizeChanger(val) {
console.log('showSizeChanger', val)
Object.assign(this.localPagination, { Object.assign(this.localPagination, {
showSizeChanger: val showSizeChanger: val
}) })
...@@ -112,7 +117,7 @@ export default { ...@@ -112,7 +117,7 @@ export default {
) )
) )
// 对接自己的通用数据接口需要修改下方代码中的 r.pageNo, r.totalCount, r.data // 对接自己的通用数据接口需要修改下方代码中的 r.pageNo, r.totalCount, r.data
if (result instanceof Promise) { if (result instanceof Promise) {
result.then(r => { result.then(r => {
this.localPagination = Object.assign({}, this.localPagination, { this.localPagination = Object.assign({}, this.localPagination, {
...@@ -122,7 +127,7 @@ export default { ...@@ -122,7 +127,7 @@ export default {
pageSize: (pagination && pagination.pageSize) || pageSize: (pagination && pagination.pageSize) ||
this.localPagination.pageSize this.localPagination.pageSize
}) })
// 为防止删除数据后导致页面当前页面数据长度为 0 ,自动翻页到上一页 // 为防止删除数据后导致页面当前页面数据长度为 0 ,自动翻页到上一页
if (r.data.length == 0 && this.localPagination.current != 1) { if (r.data.length == 0 && this.localPagination.current != 1) {
this.localPagination.current-- this.localPagination.current--
...@@ -130,7 +135,7 @@ export default { ...@@ -130,7 +135,7 @@ export default {
return return
} }
// 这里用于判断接口是否有返回 r.totalCount 或 this.showPagination = false // 这里用于判断接口是否有返回 r.totalCount 或 this.showPagination = false
// 当情况满足时,表示数据不满足分页大小,关闭 table 分页功能 // 当情况满足时,表示数据不满足分页大小,关闭 table 分页功能
!r.totalCount && ['auto', false].includes(this.showPagination) && (this.localPagination = false) !r.totalCount && ['auto', false].includes(this.showPagination) && (this.localPagination = false)
this.localDataSource = r.data // 返回结果中的数组数据 this.localDataSource = r.data // 返回结果中的数组数据
...@@ -142,15 +147,20 @@ export default { ...@@ -142,15 +147,20 @@ export default {
const totalList = [] const totalList = []
columns && columns instanceof Array && columns.forEach(column => { columns && columns instanceof Array && columns.forEach(column => {
if (column.needTotal) { if (column.needTotal) {
totalList.push({ ...column, totalList.push({
...column,
total: 0 total: 0
}) })
} }
}) })
return totalList return totalList
}, },
/**
* 用于更新已选中的列表数据 total 统计
* @param selectedRowKeys
* @param selectedRows
*/
updateSelect(selectedRowKeys, selectedRows) { updateSelect(selectedRowKeys, selectedRows) {
this.selectedRowKeys = selectedRowKeys
this.selectedRows = selectedRows this.selectedRows = selectedRows
const list = this.needTotalList const list = this.needTotalList
this.needTotalList = list.map(item => { this.needTotalList = list.map(item => {
...@@ -162,123 +172,91 @@ export default { ...@@ -162,123 +172,91 @@ export default {
}, 0) }, 0)
} }
}) })
// this.$emit('change', selectedRowKeys, selectedRows)
},
updateEdit() {
this.selectedRows = []
}, },
onClearSelected() { /**
this.selectedRowKeys = [] * 清空 table 已选中项
this.updateSelect([], []) */
clearSelected () {
if (this.rowSelection) {
this.rowSelection.onChange([], [])
this.updateSelect([], [])
}
}, },
renderMsg(h) { /**
const _vm = this * 处理交给 table 使用者去处理 clear 事件时,内部选中统计同时调用
const d = [] * @param callback
// 构建 已选择 * @returns {*}
d.push( */
h('span', { renderClear (callback) {
style: { return (
marginRight: '12px' <a style="margin-left: 24px" onClick={() => {
} callback()
}, ['已选择 ', h('a', { this.clearSelected()
style: { }}>清空</a>
fontWeight: 600
}
}, this.selectedRows.length)])
) )
},
// 构建 列统计 renderAlert () {
this.needTotalList.map(item => { // 绘制统计列数据
d.push(h('span', { const needTotalItems = this.needTotalList.map((item) => {
style: { return (<span style="margin-right: 12px">
marginRight: '12px' {item.title}总计 <a style="font-weight: 600">{!item.customRender ? item.total : item.customRender(item.total)}</a>
} </span>)
},
[
`${ item.title }总计 `,
h('a', {
style: {
fontWeight: 600
}
}, `${ !item.customRender ? item.total : item.customRender(item.total) }`)
]))
}) })
// 构建 清空选择 // 绘制 清空 按钮
d.push(h('a', { const clearItem = (typeof this.alert.clear === 'boolean' && this.alert.clear) ? (
style: { this.renderClear(this.clearSelected)
marginLeft: '24px' ) : (this.alert !== null && typeof this.alert.clear === 'function') ? (
}, this.renderClear(this.alert.clear)
on: { ) : null
click: _vm.onClearSelected
}
}, '清空'))
return d // 绘制 alert 组件
}, return (
renderAlert(h) { <a-alert showIcon={true} style="margin-bottom: 16px">
return h('span', { <template slot="message">
slot: 'message' <span style="margin-right: 12px">已选择: <a style="font-weight: 600">{this.selectedRows.length}</a></span>
}, this.renderMsg(h)) {needTotalItems}
}, {clearItem}
</template>
</a-alert>
)
}
}, },
render(h) { render() {
const _vm = this const props = {}
const localKeys = Object.keys(this.$data)
const props = {}, const showAlert = (typeof this.alert === 'object' && this.alert !== null && this.alert.show) || this.alert
localKeys = Object.keys(this.$data)
Object.keys(T.props).forEach(k => { Object.keys(T.props).forEach(k => {
const localKey = `local${k.substring(0,1).toUpperCase()}${k.substring(1)}` const localKey = `local${k.substring(0, 1).toUpperCase()}${k.substring(1)}`
if (localKeys.includes(localKey)) { if (localKeys.includes(localKey)) {
return props[k] = _vm[localKey] return props[k] = this[localKey]
} }
return props[k] = _vm[k] if (showAlert && k === 'rowSelection') {
}) // 重新绑定 rowSelection 事件
return props[k] = {
selectedRowKeys: this[k].selectedRowKeys,
// 显示信息提示 onChange: (selectedRowKeys, selectedRows) => {
if (this.showAlertInfo) { this.updateSelect(selectedRowKeys, selectedRows)
this[k].onChange(selectedRowKeys, selectedRows)
props.rowSelection = { }
selectedRowKeys: this.selectedRowKeys,
onChange: (selectedRowKeys, selectedRows) => {
_vm.updateSelect(selectedRowKeys, selectedRows)
_vm.$emit('onSelect', { selectedRowKeys: selectedRowKeys, selectedRows: selectedRows })
} }
} }
return props[k] = this[k]
})
return h('div', {}, [ const table = (
h('a-alert', { <a-table {...{ props, scopedSlots: {...this.$scopedSlots}}} onChange={this.loadData}>
style: { {this.$slots.default}
marginBottom: '16px' </a-table>
}, )
props: {
type: 'info',
showIcon: true
}
}, [_vm.renderAlert(h)]),
h('a-table', {
tag: 'component',
attrs: props,
on: {
change: _vm.loadData
},
scopedSlots: this.$scopedSlots
}, this.$slots.default)
])
}
return h('a-table', {
tag: 'component',
attrs: props,
on: {
change: _vm.loadData
},
scopedSlots: this.$scopedSlots
}, this.$slots.default)
return (
<div class="table-wrapper">
{ showAlert ? this.renderAlert() : null }
{ table }
</div>
)
} }
} }
\ No newline at end of file
...@@ -80,8 +80,8 @@ ...@@ -80,8 +80,8 @@
size="default" size="default"
:columns="columns" :columns="columns"
:data="loadData" :data="loadData"
:showAlertInfo="true" :alert="{ show: true, clear: () => { this.selectedRowKeys = [] } }"
@onSelect="onChange" :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
> >
<span slot="action" slot-scope="text, record"> <span slot="action" slot-scope="text, record">
<template v-if="$auth('table.update')"> <template v-if="$auth('table.update')">
...@@ -271,11 +271,10 @@ ...@@ -271,11 +271,10 @@
handleOk () { handleOk () {
}, },
onChange (row) {
this.selectedRowKeys = row.selectedRowKeys
this.selectedRows = row.selectedRows
console.log(this.$refs.table) onSelectChange (selectedRowKeys, selectedRows) {
this.selectedRowKeys = selectedRowKeys
this.selectedRows = selectedRows
}, },
toggleAdvanced () { toggleAdvanced () {
this.advanced = !this.advanced this.advanced = !this.advanced
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment