123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- import T from 'ant-design-vue/es/table/Table'
- import get from 'lodash.get'
- import BaseTool from '@/utils/tool'
- export default {
- data () {
- return {
- needTotalList: [],
- selectedRows: [],
- selectedRowKeys: [],
- checkCustomMap: {},
- localLoading: false,
- localDataSource: [],
- // localPagination: Object.assign({}, this.pagination)
- localPagination: Object.assign({}, {
- ...this.pagination,
- showTotal: total => `共 ${total} 条数据`,
- showSizeChanger: true,
- pageSizeOptions: ['10', '20', '50', '100', '150']
- })
- }
- },
- props: Object.assign({}, T.props, {
- widthSpace: {
- type: Boolean,
- default: false
- },
- rowKey: {
- type: [String, Function],
- default: 'key'
- },
- data: {
- type: Function,
- required: true
- },
- pageNum: {
- type: Number,
- default: 1
- },
- pageSize: {
- type: Number,
- default: 50
- },
- showSizeChanger: {
- type: Boolean,
- default: true
- },
- size: {
- type: String,
- default: 'default'
- },
- /**
- * alert: {
- * show: true,
- * clear: Function
- * }
- */
- alert: {
- type: [Object, Boolean],
- default: null
- },
- rowSelection: {
- type: Object,
- default: null
- },
- /** @Deprecated */
- showAlertInfo: {
- type: Boolean,
- default: false
- },
- showPagination: {
- type: String | Boolean,
- default: 'auto'
- },
- /**
- * enable page URI mode
- *
- * e.g:
- * /users/1
- * /users/2
- * /users/3?queryParam=test
- * ...
- */
- pageURI: {
- type: Boolean,
- default: false
- }
- }),
- watch: {
- columns (val, oldVal) { // 深度监听,可监听到对象、数组的变化
- let isChecked = 0
- val.forEach((item, index) => {
- if (item['checked'] == null) {
- item['checked'] = false
- }
- if (item['checked']) {
- isChecked++
- }
- this.checkCustomMap[item.title] = item
- })
- if (isChecked === 0) {
- val.forEach((item, index) => {
- if (index < 8) {
- item['checked'] = true
- this.checkCustomMap[item.title] = item
- }
- if (item.title === '操作') {
- item['checked'] = true
- this.checkCustomMap[item.title] = item
- }
- })
- }
- },
- widthSpace (val) {
- Object.assign(this.localPagination, {
- widthSpace: val
- })
- },
- 'localPagination.current' (val) {
- this.pageURI && this.$router.push({
- ...this.$route,
- name: this.$route.name,
- params: Object.assign({}, this.$route.params, {
- pageNum: val
- })
- })
- },
- pageNum (val) {
- Object.assign(this.localPagination, {
- current: val
- })
- },
- pageSize (val) {
- Object.assign(this.localPagination, {
- pageSize: val
- })
- },
- showSizeChanger (val) {
- Object.assign(this.localPagination, {
- showSizeChanger: val
- })
- }
- },
- created () {
- const { pageNum } = this.$route.params
- const localPageNum = this.pageURI && (pageNum && parseInt(pageNum)) || this.pageNum
- this.localPagination = ['auto', true].includes(this.showPagination) && Object.assign({}, this.localPagination, {
- current: localPageNum,
- pageSize: this.pageSize,
- showSizeChanger: this.showSizeChanger
- }) || false
- this.needTotalList = this.initTotalList(this.columns)
- let isChecked = 0
- this.columns.forEach((item, index) => {
- if (item['checked'] == null) {
- item['checked'] = false
- }
- if (item['checked']) {
- isChecked++
- }
- this.checkCustomMap[item.title] = item
- })
- if (isChecked === 0) {
- this.columns.forEach((item, index) => {
- if (index < 8) {
- item['checked'] = true
- this.checkCustomMap[item.title] = item
- }
- if (item.title === '操作') {
- item['checked'] = true
- this.checkCustomMap[item.title] = item
- }
- })
- }
- this.loadData()
- },
- methods: {
- /**
- * 表格重新加载方法
- * 如果参数为 true, 则强制刷新到第一页
- * @param Boolean bool
- */
- refresh (bool = false) {
- bool && (this.localPagination = Object.assign({}, {
- current: 1, pageSize: this.pageSize
- }))
- this.loadData()
- },
- /**
- * 加载数据方法
- * @param {Object} pagination 分页选项器
- * @param {Object} filters 过滤条件
- * @param {Object} sorter 排序条件
- */
- loadData (pagination, filters, sorter) {
- this.localLoading = true
- const dataScope = {
- }
- sorter && sorter.field && (dataScope.sortName = BaseTool.String.underlineToHump(sorter.field))
- sorter && sorter.order && (dataScope.sortBy = sorter.order.slice(0, -3))
- const parameter = Object.assign({
- pageNum: (pagination && pagination.current) ||
- this.showPagination && this.localPagination.current || this.pageNum,
- pageSize: (pagination && pagination.pageSize) ||
- this.showPagination && this.localPagination.pageSize || this.pageSize
- },
- {
- dataScope: dataScope
- },
- {
- ...filters
- }
- )
- const result = this.data(parameter)
- // 对接自己的通用数据接口需要修改下方代码中的 r.pageNo, r.totalCount, r.data
- // eslint-disable-next-line
- if ((typeof result === 'object' || typeof result === 'function') && typeof result.then === 'function') {
- result.then(r => {
- this.localPagination = this.showPagination && Object.assign({}, this.localPagination, {
- current: r.pageNum, // 返回结果中的当前分页数
- total: r.total, // 返回结果中的总记录数
- showSizeChanger: this.showSizeChanger,
- pageSize: (pagination && pagination.pageSize) ||
- this.localPagination.pageSize
- }) || false
- // 为防止删除数据后导致页面当前页面数据长度为 0 ,自动翻页到上一页
- if (r.rows.length === 0 && this.showPagination && this.localPagination.current > 1) {
- this.localPagination.current--
- this.loadData()
- return
- }
- // 这里用于判断接口是否有返回 r.totalCount 且 this.showPagination = true 且 pageNo 和 pageSize 存在 且 totalCount 小于等于 pageNo * pageSize 的大小
- // 当情况满足时,表示数据不满足分页大小,关闭 table 分页功能
- try {
- if ((['auto', true].includes(this.showPagination) && r.totalCount <= (r.pageNum * this.localPagination.pageSize))) {
- this.localPagination.hideOnSinglePage = true
- }
- } catch (e) {
- this.localPagination = false
- }
- this.localDataSource = r.rows // 返回结果中的数组数据
- this.localLoading = false
- })
- }
- },
- initTotalList (columns) {
- const totalList = []
- columns && columns instanceof Array && columns.forEach(column => {
- if (column.needTotal) {
- totalList.push({
- ...column,
- total: 0
- })
- }
- })
- return totalList
- },
- /**
- * 用于更新已选中的列表数据 total 统计
- * @param selectedRowKeys
- * @param selectedRows
- */
- updateSelect (selectedRowKeys, selectedRows) {
- this.selectedRows = selectedRows
- this.selectedRowKeys = selectedRowKeys
- const list = this.needTotalList
- this.needTotalList = list.map(item => {
- return {
- ...item,
- total: selectedRows.reduce((sum, val) => {
- const total = sum + parseInt(get(val, item.dataIndex))
- return isNaN(total) ? 0 : total
- }, 0)
- }
- })
- },
- /**
- * 清空 table 已选中项
- */
- clearSelected () {
- if (this.rowSelection) {
- this.rowSelection.onChange([], [])
- this.updateSelect([], [])
- }
- },
- /**
- * 处理交给 table 使用者去处理 clear 事件时,内部选中统计同时调用
- * @param callback
- * @returns {*}
- */
- renderClear (callback) {
- if (this.selectedRowKeys.length <= 0) return null
- return (
- <a style="margin-left: 24px" onClick={() => {
- callback()
- this.clearSelected()
- }}>清空</a>
- )
- },
- /** myColumns
- * 处理交给 table 使用者去处理 clear 事件时,内部选中统计同时调用
- * @returns {*}
- */
- renderColumns () {
- // this.columns
- // if (this.selectedRowKeys.length <= 0) { return null }
- const checkedKeys = []
- const treeData = []
- Object.keys(this.checkCustomMap).forEach((key) => {
- const item = this.checkCustomMap[key]
- if (item.checked) {
- checkedKeys.push(item.title)
- }
- treeData.push({ title: item.title,
- key: item.title })
- })
- // const choose=this.$t('m.common.choose_show_column')
- return (
- <a style="margin-left: 24px" onClick={() => {
- }}>
- <a-popover placement="bottom">
- <template slot="content">
- <a-tree
- checkable={true}
- onCheck={(checkedKeys) => {
- Object.keys(this.checkCustomMap).forEach((key) => {
- const item = this.checkCustomMap[key]
- item.checked = false
- })
- checkedKeys.forEach((item) => {
- this.checkCustomMap[item].checked = true
- })
- // 重新渲染render ()函数
- this.$forceUpdate()
- }}
- onSelect={(selectedKeys, info) => {
- }}
- checkedKeys={checkedKeys}
- treeData={treeData} />
- </template>
- <template slot="title">
- <span>选择显示列</span>
- </template>
- <a-icon type="appstore" />
- </a-popover>
- </a>
- )
- },
- renderAlert () {
- // 绘制统计列数据
- const needTotalItems = this.needTotalList.map((item) => {
- return (<span style="margin-right: 12px">
- {item.title}总计 <a style="font-weight: 600">{!item.customRender ? item.total : item.customRender(item.total)}</a>
- </span>)
- })
- // 绘制 清空 按钮
- const clearItem = (typeof this.alert.clear === 'boolean' && this.alert.clear) ? (
- this.renderClear(this.clearSelected)
- ) : (this.alert !== null && typeof this.alert.clear === 'function') ? (
- this.renderClear(this.alert.clear)
- ) : null
- const renderColumns = this.renderColumns()
- // const ss = this.$t('m.common.choose_total')
- // 绘制 alert 组件
- return (
- <a-alert showIcon={true} style="margin-bottom: 16px">
- <template slot="message">
- <span style="margin-right: 12px">已选择:<a style="font-weight: 600">{this.selectedRows.length}</a></span>
- {needTotalItems}
- {clearItem}
- {renderColumns}
- </template>
- </a-alert>
- )
- }
- },
- render () {
- const props = {}
- const localKeys = Object.keys(this.$data)
- const showAlert = (typeof this.alert === 'object' && this.alert !== null && this.alert.show) && typeof this.rowSelection.selectedRowKeys !== 'undefined' || this.alert
- Object.keys(T.props).forEach(k => {
- const localKey = `local${k.substring(0, 1).toUpperCase()}${k.substring(1)}`
- if (localKeys.includes(localKey)) {
- props[k] = this[localKey]
- return props[k]
- }
- if (k === 'rowSelection') {
- if (showAlert && this.rowSelection) {
- // 如果需要使用alert,则重新绑定 rowSelection 事件
- props[k] = {
- ...this.rowSelection,
- selectedRows: this.selectedRows,
- selectedRowKeys: this.selectedRowKeys,
- onChange: (selectedRowKeys, selectedRows) => {
- this.updateSelect(selectedRowKeys, selectedRows)
- typeof this[k].onChange !== 'undefined' && this[k].onChange(selectedRowKeys, selectedRows)
- }
- }
- return props[k]
- } else if (!this.rowSelection) {
- // 如果没打算开启 rowSelection 则清空默认的选择项
- props[k] = null
- return props[k]
- }
- }
- this[k] && (props[k] = this[k])
- return props[k]
- })
- const myColumns = []
- let totalWidth = 0
- Object.keys(this.checkCustomMap).forEach((key) => {
- const item = this.checkCustomMap[key]
- if (item.checked) {
- myColumns.push(item)
- const width = /^\d+/.exec(item.width)
- if (width && width.length > 0) {
- totalWidth += parseInt(width[0])
- }
- }
- })
- const x = props.scroll ? props.scroll.x : 0
- if (x && x > 0) {
- props.scroll.x = totalWidth
- }
- props.columns = myColumns
- const myClass = this.widthSpace ? 'future-table-width-space' : ''
- const table = (
- <a-table class={myClass} {...{ props, scopedSlots: { ...this.$scopedSlots } }} onChange={this.loadData}>
- { Object.keys(this.$slots).map(name => (<template slot={name}>{this.$slots[name]}</template>)) }
- </a-table>
- )
- return (
- <div class="table-wrapper">
- { showAlert ? this.renderAlert() : null }
- { table }
- </div>
- )
- }
- }
|