[Update] 修改table 渲染formatter

This commit is contained in:
ibuler
2020-04-02 13:36:25 +08:00
parent 1ea349b460
commit 1693e11dcc
7 changed files with 91 additions and 75 deletions

View File

@@ -74,9 +74,7 @@
class="tree-ctrl" class="tree-ctrl"
@click="toggleExpanded(scope.$index)" @click="toggleExpanded(scope.$index)"
> >
<i <i :class="`el-icon-${scope.row._expanded ? 'minus' : 'plus'}`"/>
:class="`el-icon-${scope.row._expanded ? 'minus' : 'plus'}`"
/>
</span> </span>
{{ scope.row[columns[0].prop] }} {{ scope.row[columns[0].prop] }}
</template> </template>
@@ -92,11 +90,25 @@
<!--非树--> <!--非树-->
<template v-else> <template v-else>
<el-data-table-column v-if="hasSelection" type="selection" :align="columnsAlign"></el-data-table-column>
<el-data-table-column <el-data-table-column
v-for="col in columns" v-for="col in columns"
:key="col.prop" :key="col.prop"
:formatter="typeof col.formatter === 'function' ? col.formatter : null"
v-bind="{align: columnsAlign, ...col}" v-bind="{align: columnsAlign, ...col}"
> >
<template slot-scope="{row}">
<div
v-if="col.formatter && typeof col.formatter !== 'function'"
:is="col.formatter"
:row="row"
:col="col"
>
</div>
<template v-else>
{{ row[col.prop] }}
</template>
</template>
</el-data-table-column> </el-data-table-column>
</template> </template>
@@ -119,6 +131,7 @@
<self-loading-button <self-loading-button
v-if="hasEdit" v-if="hasEdit"
type="primary" type="primary"
:disable="!canEdit(scope.row)"
:size="operationButtonType === 'text' ? '' : buttonSize" :size="operationButtonType === 'text' ? '' : buttonSize"
:is-text="operationButtonType === 'text'" :is-text="operationButtonType === 'text'"
@click="onDefaultEdit(scope.row)" @click="onDefaultEdit(scope.row)"
@@ -153,8 +166,9 @@
</self-loading-button> </self-loading-button>
</template> </template>
<self-loading-button <self-loading-button
v-if="!hasSelect && hasDelete && canDelete(scope.row)" v-if="hasDelete"
type="danger" type="danger"
:disable="!canDelete(scope.row)"
:size="operationButtonType === 'text' ? '' : buttonSize" :size="operationButtonType === 'text' ? '' : buttonSize"
:is-text="operationButtonType === 'text'" :is-text="operationButtonType === 'text'"
@click="onDefaultDelete(scope.row)" @click="onDefaultDelete(scope.row)"
@@ -450,6 +464,12 @@ export default {
return true return true
} }
}, },
canEdit: {
type: Function,
default() {
return true
}
},
/** /**
* 点击新增按钮时的方法, 当默认新增方法不满足需求时使用, 需要返回promise * 点击新增按钮时的方法, 当默认新增方法不满足需求时使用, 需要返回promise
* 参数(data, row) data 是form表单的数据, row 是当前行的数据, 只有isTree为true时, 点击操作列的新增按钮才会有值 * 参数(data, row) data 是form表单的数据, row 是当前行的数据, 只有isTree为true时, 点击操作列的新增按钮才会有值
@@ -484,7 +504,7 @@ export default {
const ids = Array.isArray(data) const ids = Array.isArray(data)
? data.map(v => v[this.id]).join(',') ? data.map(v => v[this.id]).join(',')
: data[this.id] : data[this.id]
return this.$axios.delete(this.url + '/' + ids, this.axiosConfig) return this.$axios.delete(this.url + '/' + ids + '/', this.axiosConfig)
} }
}, },
/** /**
@@ -750,6 +770,14 @@ export default {
extraPaginationAttrs: { extraPaginationAttrs: {
type: Object, type: Object,
default: () => {} default: () => {}
},
hasSelection: {
type: Boolean,
default: true
},
hasDetail: {
type: Boolean,
default: true
} }
}, },
data() { data() {
@@ -775,6 +803,7 @@ export default {
}, },
computed: { computed: {
hasSelect() { hasSelect() {
console.log(this.columns.length && this.columns[0].type === 'selection')
return this.columns.length && this.columns[0].type === 'selection' return this.columns.length && this.columns[0].type === 'selection'
}, },
selectable() { selectable() {

View File

@@ -0,0 +1,34 @@
<template>
<el-link :type="col.type || 'success'" @click="goDetail">{{ cellValue }}</el-link>
</template>
<script>
export default {
name: 'DetailFormatter',
props: {
row: {
type: Object,
default: null
},
col: {
type: Object,
default: null
}
},
computed: {
cellValue() {
return this.row[this.col.prop]
}
},
methods: {
goDetail() {
const routeName = this.col.route || ''
this.$router.push({ name: routeName, params: { id: this.row.id }})
}
}
}
</script>
<style scoped>
</style>

View File

@@ -30,12 +30,14 @@ export default {
totalPath: 'count', totalPath: 'count',
saveQuery: false, // 关闭路径保存查询参数 saveQuery: false, // 关闭路径保存查询参数
persistSelection: true, // 切换页面 已勾选项不会丢失 persistSelection: true, // 切换页面 已勾选项不会丢失
hasEdit: false, // 有编辑按钮 hasEdit: true, // 有编辑按钮
hasDelete: false, hasDelete: true,
hasAction: false, // 是否有更多操作
hasUpload: false,
hasNew: false, hasNew: false,
// editText: this.$t('action.update'), // 编辑按钮文案 // editText: this.$t('action.update'), // 编辑按钮文案
operationAttrs: {
align: 'center',
width: '150px'
},
tableAttrs: { tableAttrs: {
stripe: true, // 斑马纹表格 stripe: true, // 斑马纹表格
border: true, // 表格边框 border: true, // 表格边框

View File

@@ -90,7 +90,7 @@ export default {
console.log('Search: ', keyword) console.log('Search: ', keyword)
}, },
handleActionClick(item) { handleActionClick(item) {
console.log('Action: ', item) this.$emit('clickAction', item)
} }
} }
} }

View File

@@ -1,8 +1,8 @@
<template> <template>
<div> <div>
<TableAction></TableAction> <TableAction v-bind="actionConfig" @clickAction="handleActionClick"></TableAction>
<el-card class="table-content"> <el-card class="table-content">
<DataTable :config="tableConfig" @selection-change="handleSelectionChange" ></DataTable> <DataTable :config="tableConfig" @selection-change="handleSelectionChange"></DataTable>
</el-card> </el-card>
</div> </div>
</template> </template>
@@ -24,30 +24,9 @@ export default {
default: () => {} default: () => {}
}, },
// 是否显示table左侧的action // 是否显示table左侧的action
actionConfig: {
createTitle: {
type: String,
default() {
return this.$tc('Create')
}
},
createAction: {
type: Object, type: Object,
default() { default: () => ({ })
return {
type: 'primary',
name: 'create',
title: this.createTitle
}
}
},
moreActions: {
type: Array,
default: () => []
},
actions: {
type: Array,
default: () => []
} }
}, },
data() { data() {
@@ -56,46 +35,16 @@ export default {
} }
}, },
computed: { computed: {
totalActions() {
let actions = this.actions
if (this.hasCreate) {
actions = [
this.createAction,
... actions
]
}
return actions
}
}, },
methods: { methods: {
handleSelectionChange(val) { handleSelectionChange(val) {
console.log('lIst table', val)
this.selectRows = val this.selectRows = val
this.multipleSelection = val; this.multipleSelection = val;
(val.length > 0) ? (this.selectDisable = false) : (this.selectDisable = true) (val.length > 0) ? (this.selectDisable = false) : (this.selectDisable = true)
}, },
handleEdit: function (index, row) { handleActionClick(item) {
try { console.log('Handle ', item, this.selectRows)
this.$router.push({name: this.action.hasEdit, params: {id: row.id}})
} catch (error) {
console.log(error)
}
},
handleHeaderActionClick(item) {
this.$emit('headerActionClick', item, this.selectRows)
},
handleDelete: (index, row) => {
},
get(draw, limit, offset) {
this.loading = true
// this.getData({ draw, limit, offset }, { row: 1 }).then(response => {
// console.log(response)
// this.tabledata = response.results
// this.total = response.count
// this.loading = false
// })
} }
} }
} }
</script> </script>

View File

@@ -53,9 +53,9 @@ service.interceptors.response.use(
const res = response.data const res = response.data
// if the custom code is not 20000, it is judged as an error. // if the custom code is not 20000, it is judged as an error.
if (response.status !== 200 && response.status !== 201) { if (response.status < 200 || response.status > 300) {
Message({ Message({
message: res.message || 'Error', message: res.message || res.error || 'Error',
type: 'error', type: 'error',
duration: 5 * 1000 duration: 5 * 1000
}) })

View File

@@ -1,13 +1,15 @@
<template> <template>
<Page> <Page>
<el-alert type="success"> 这里是一个成功的文案 </el-alert> <el-alert type="success"> 这里是一个成功的文案 </el-alert>
<ListTable :table-config="tableConfig" /> <ListTable :table-config="tableConfig" :action-config="actionConfig" />
</Page> </Page>
</template> </template>
<script> <script>
import { Page } from '@/layout/components' import { Page } from '@/layout/components'
import ListTable from '@/components/ListTable' import ListTable from '@/components/ListTable'
import DetailFormatter from '@/components/DataTable/formatters/DetailFormatter'
export default { export default {
components: { components: {
Page, Page,
@@ -18,15 +20,13 @@ export default {
tableConfig: { tableConfig: {
url: '/api/v1/users/groups/', url: '/api/v1/users/groups/',
columns: [ columns: [
{
type: 'selection'
},
{ {
prop: 'name', prop: 'name',
label: this.$tc('Name'), label: this.$tc('Name'),
key: 'name', key: 'name',
link: 'UserGroupDetail', formatter: DetailFormatter,
sortable: true sortable: true,
route: 'UserDetail'
}, },
{ {
prop: 'users_amount', prop: 'users_amount',
@@ -46,6 +46,8 @@ export default {
newClick: 'UserGroupEdit' newClick: 'UserGroupEdit'
} }
}, },
actionConfig: {
}
} }
} }