123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <template>
- <div class="standard-table">
- <div class="alert">
- <a-alert type="info" :show-icon="true">
- <div slot="message">
- 已选择 <a style="font-weight: 600">{{ selectedRows.length }}</a>
- <template v-for="(item, index) in needTotalList" v-if="item.needTotal">
- {{ item.title }} 总计
- <a :key="index" style="font-weight: 600">
- {{ item.customRender ? item.customRender(item.total) : item.total }}
- </a>
- </template>
- <a style="margin-left: 24px" @click="onClearSelected">清空</a>
- </div>
- </a-alert>
- </div>
- <a-table
- :size="size"
- :bordered="bordered"
- :loading="loading"
- :columns="columns"
- :dataSource="current"
- :rowKey="rowKey"
- :pagination="pagination"
- :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: updateSelect }"
- >
- </a-table>
- </div>
- </template>
- <script>
- export default {
- name: "StandardTable",
-
- props: {
-
- data: {
- type: Function,
- required: true
- },
- dataSource: {
- type: Array,
- default () {
- return []
- }
- },
- columns: {
- type: Array,
- required: true
- },
- pageSize: {
- type: Number,
- default: 10
- },
- pageNum: {
- type: Number,
- default: 1
- },
- pageSizeOptions: {
- type: Array,
- default () {
- return ['10', '20', '30', '40', '50']
- }
- },
- responseParamsName: {
- type: Object,
- default () {
- return {}
- }
- },
- bordered: {
- type: Boolean,
- default: false
- },
-
- size: {
- type: String,
- default: 'default'
- },
- rowKey: {
- type: String
- },
- selectedRows: {
- type: Array
- }
- },
- data () {
- return {
- needTotalList: [],
- selectedRowKeys: [],
- loading: true,
- total: 0,
- pageNumber: this.pageNum,
- currentPageSize: this.pageSize,
- defaultCurrent: 1,
- sortParams: {},
- current: [],
- pagination: {},
- paramsName: {},
- }
- },
- created () {
-
- this.paramsName = Object.assign(
- {},
- {
- pageNumber: "pageNo",
- pageSize: "pageSize",
- total: "totalCount",
- results: "data",
- sortColumns: "sortColumns"
- },
- this.responseParamsName
- );
- this.needTotalList = this.initTotalList(this.columns)
-
- this.loadData( { pageNum: this.pageNumber } )
- },
- methods: {
- updateSelect (selectedRowKeys, selectedRows) {
- this.selectedRowKeys = selectedRowKeys
- 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)
- },
- initTotalList (columns) {
- const totalList = []
- columns.forEach(column => {
- if (column.needTotal) {
- totalList.push({ ...column, total: 0 })
- }
- })
- return totalList
- },
- loadData (params) {
- let that = this
- that.loading = true
- params = Object.assign({}, params)
- const remoteParams = Object.assign({}, that.sortParams)
- remoteParams[that.paramsName.pageNumber] = params.pageNum || that.pageNumber
- remoteParams[that.paramsName.pageSize] = params.pageSize || that.currentPageSize
- if (params.pageNum) {
- that.pageNumber = params.pageNum
- }
- if (params.pageSize) {
- that.currentPageSize = params.pageSize
- }
- let dataPromise = that.data(remoteParams)
- dataPromise.then( response => {
- if (!response) {
- that.loading = false
- return
- }
- let results = response[that.paramsName.results]
- results = (results instanceof Array && results) || []
- that.current = results
- that.$emit("update:currentData", that.current.slice())
- that.$emit("dataloaded", that.current.slice())
- that.total = response[that.paramsName.total] * 1
- that.pagination = that.pager()
- that.loading = false
- }, () => {
-
- that.loading = false
- })
- },
-
- onPagerChange (page, pageSize) {
- this.pageNumber = page
- this.loadData({ pageNum: page })
- },
- onPagerSizeChange (current, size) {
- this.currentPageSize = size
-
- },
- onClearSelected () {
- this.selectedRowKeys = []
- this.updateSelect([], [])
- },
- pager () {
- return {
- total: this.total,
- showTotal: total => `共有 ${total} 条`,
- showSizeChanger: true,
- pageSizeOptions: this.pageSizeOptions,
- pageSize: this.pageSize,
- defaultCurrent: this.defaultCurrent,
- onChange: this.onPagerChange,
- onShowSizeChange: this.onPagerSizeChange
- }
- }
- },
- watch: {
- 'selectedRows': function (selectedRows) {
- this.needTotalList = this.needTotalList.map(item => {
- return {
- ...item,
- total: selectedRows.reduce( (sum, val) => {
- return sum + val[item.dataIndex]
- }, 0)
- }
- })
- }
- }
- }
- </script>
- <style scoped>
- .alert {
- margin-bottom: 16px;
- }
- </style>
|