mirror of
https://github.com/iczer/vue-antd-admin
synced 2025-04-06 04:00:06 +08:00
chore: optimize the code of StandardTable.vue; 🌟
This commit is contained in:
parent
a964274d63
commit
8688661c8c
@ -2,17 +2,14 @@
|
|||||||
<div class="standard-table">
|
<div class="standard-table">
|
||||||
<div class="alert">
|
<div class="alert">
|
||||||
<a-alert type="info" :show-icon="true">
|
<a-alert type="info" :show-icon="true">
|
||||||
<div slot="message">
|
<div class="message" slot="message">
|
||||||
已选择 <a style="font-weight: 600">{{selectedRows.length}}</a> 项
|
已选择 <a>{{selectedRows.length}}</a> 项 <a class="clear" @click="onClear">清空</a>
|
||||||
<div v-for="(item, index) in needTotalList" :key="index">
|
<template v-for="(item, index) in needTotalList" >
|
||||||
<div v-if="item.needTotal">
|
<div v-if="item.needTotal" :key="index">
|
||||||
{{item.title}}总计
|
{{item.title}}总计
|
||||||
<a :key="index" style="font-weight: 600">
|
<a>{{item.customRender ? item.customRender(item.total) : item.total}}</a>
|
||||||
{{item.customRender ? item.customRender(item.total) : item.total}}
|
|
||||||
</a>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</template>
|
||||||
<a style="margin-left: 24px">清空</a>
|
|
||||||
</div>
|
</div>
|
||||||
</a-alert>
|
</a-alert>
|
||||||
</div>
|
</div>
|
||||||
@ -39,22 +36,12 @@ export default {
|
|||||||
data () {
|
data () {
|
||||||
return {
|
return {
|
||||||
needTotalList: [],
|
needTotalList: [],
|
||||||
selectedRowKeys: [],
|
|
||||||
scopedSlots: []
|
scopedSlots: []
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
updateSelect (selectedRowKeys, selectedRows) {
|
updateSelect (selectedRowKeys, selectedRows) {
|
||||||
this.selectedRowKeys = selectedRowKeys
|
this.$emit('update:selectedRows', selectedRows)
|
||||||
let list = this.needTotalList
|
|
||||||
this.needTotalList = list.map(item => {
|
|
||||||
return {
|
|
||||||
...item,
|
|
||||||
total: selectedRows.reduce((sum, val) => {
|
|
||||||
return sum + val[item.dataIndex]
|
|
||||||
}, 0)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
this.$emit('change', selectedRowKeys, selectedRows)
|
this.$emit('change', selectedRowKeys, selectedRows)
|
||||||
},
|
},
|
||||||
initTotalList (columns) {
|
initTotalList (columns) {
|
||||||
@ -69,6 +56,10 @@ export default {
|
|||||||
getScopedSlots(columns) {
|
getScopedSlots(columns) {
|
||||||
return columns.filter(item => item.scopedSlots && item.scopedSlots.customRender)
|
return columns.filter(item => item.scopedSlots && item.scopedSlots.customRender)
|
||||||
.map(item => item.scopedSlots.customRender)
|
.map(item => item.scopedSlots.customRender)
|
||||||
|
},
|
||||||
|
onClear() {
|
||||||
|
this.updateSelect([], [])
|
||||||
|
this.$emit('clear')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
created () {
|
created () {
|
||||||
@ -76,7 +67,7 @@ export default {
|
|||||||
this.needTotalList = this.initTotalList(this.columns)
|
this.needTotalList = this.initTotalList(this.columns)
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
'selectedRows': function (selectedRows) {
|
selectedRows (selectedRows) {
|
||||||
this.needTotalList = this.needTotalList.map(item => {
|
this.needTotalList = this.needTotalList.map(item => {
|
||||||
return {
|
return {
|
||||||
...item,
|
...item,
|
||||||
@ -86,12 +77,27 @@ export default {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
computed: {
|
||||||
|
selectedRowKeys() {
|
||||||
|
return this.selectedRows.map(row => row.key)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped lang="less">
|
||||||
|
.standard-table{
|
||||||
.alert{
|
.alert{
|
||||||
margin-bottom: 16px;
|
margin-bottom: 16px;
|
||||||
|
.message{
|
||||||
|
a{
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.clear{
|
||||||
|
float: right;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
@ -95,8 +95,8 @@
|
|||||||
<standard-table
|
<standard-table
|
||||||
:columns="columns"
|
:columns="columns"
|
||||||
:dataSource="dataSource"
|
:dataSource="dataSource"
|
||||||
:selectedRows="selectedRows"
|
:selectedRows.sync="selectedRows"
|
||||||
@change="onchange"
|
@clear="onClear"
|
||||||
>
|
>
|
||||||
<div slot="description" slot-scope="{text}">
|
<div slot="description" slot-scope="{text}">
|
||||||
{{text}}
|
{{text}}
|
||||||
@ -110,7 +110,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import StandardTable from '../../components/table/StandardTable'
|
import StandardTable from '@/components/table/StandardTable'
|
||||||
const columns = [
|
const columns = [
|
||||||
{
|
{
|
||||||
title: '规则编号',
|
title: '规则编号',
|
||||||
@ -165,7 +165,6 @@ export default {
|
|||||||
advanced: true,
|
advanced: true,
|
||||||
columns: columns,
|
columns: columns,
|
||||||
dataSource: dataSource,
|
dataSource: dataSource,
|
||||||
selectedRowKeys: [],
|
|
||||||
selectedRows: []
|
selectedRows: []
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -173,13 +172,12 @@ export default {
|
|||||||
toggleAdvanced () {
|
toggleAdvanced () {
|
||||||
this.advanced = !this.advanced
|
this.advanced = !this.advanced
|
||||||
},
|
},
|
||||||
onchange (selectedRowKeys, selectedRows) {
|
|
||||||
this.selectedRowKeys = selectedRowKeys
|
|
||||||
this.selectedRows = selectedRows
|
|
||||||
},
|
|
||||||
remove () {
|
remove () {
|
||||||
this.dataSource = this.dataSource.filter(item => this.selectedRowKeys.indexOf(item.key) < 0)
|
this.dataSource = this.dataSource.filter(item => this.selectedRows.findIndex(row => row.key === item.key) === -1)
|
||||||
this.selectedRows = this.selectedRows.filter(item => this.selectedRowKeys.indexOf(item.key) < 0)
|
this.selectedRows = []
|
||||||
|
},
|
||||||
|
onClear() {
|
||||||
|
this.$message.info('您清空了勾选的所有行')
|
||||||
},
|
},
|
||||||
addNew () {
|
addNew () {
|
||||||
this.dataSource.unshift({
|
this.dataSource.unshift({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user