1、视图模板
<template>
<div class="container">
<a-card class="form-search" :bordered="false" :style="{ marginBottom: '20px' }">
<a-form layout="inline" :form="form" @submit="handleSubmit">
<a-row class="form-row" :gutter="16">
<a-form-item label="项目名称">
<a-input v-model="formData.name" maxlength="100" placeholder="项目名称" :allowClear="true"></a-input>
</a-form-item>
<a-form-item label="项目状态">
<a-select default-value="" v-model="formData.status" style="width:80px;">
<a-select-option value="">
全部
</a-select-option>
<a-select-option value="doing">
进行中
</a-select-option>
<a-select-option value="wait">
已延期
</a-select-option>
<a-select-option value="done">
已完成
</a-select-option>
</a-select>
</a-form-item>
<a-form-item>
<a-button type="primary" html-type="submit">
查询
</a-button>
</a-form-item>
</a-row>
</a-form>
</a-card>
<a-card class="datatable" :bordered="false">
<a-table
:columns="columns"
:rowKey="row => row.id"
:data-source="data"
:pagination="pagination"
:loading="loading"
@change="handleTableChange">
<span slot="action" slot-scope="record">
<a-button class="btn-action" type="link" @click="detal(record)">详情</a-button>
<!--
<a-divider type="vertical" />
<a-button class="btn-action" type="link" @click="clickTask(record)">任务统计</a-button>
<a-divider type="vertical" />
<a-button class="btn-action" type="link" @click="clickBug(record)">BUG统计</a-button>
-->
</span>
</a-table>
</a-card>
</div>
</template>
2、JS脚本
<script>
import { findPage } from '@/api/project'
function renderStatus (text, record, index) {
if(text == 'doing') {
return '进行中'
} else if (text == 'done') {
return '已完成'
} else if (text == 'wait') {
return '已延期'
}
}
function renderProjectType (text, record, index) {
if(text == 'sprint') {
return '短期项目'
} else if (text == 'waterfall') {
return '长期项目'
} else {
return '维护项目'
}
}
function formatDate (text, record, index) {
return text ? text.substr(0, 10) : text;
}
// 表格字段属性
const columns = [
{
title: 'ID', // 表头文字
dataIndex: 'id', // 字段索引,列数据在数据项中对应的key
key: 'id' // Vue需要的 key,如果已经设置了唯一的dataIndex,可以忽略这个属性
},
{
title: '项目名称',
dataIndex: 'name',
key: 'name'
},
{
title: '项目代号',
dataIndex: 'code',
key: 'code'
},
{
title: '项目类型',
dataIndex: 'type',
key: 'type',
customRender: renderProjectType
},
{
title: '开始时间',
key: 'begin',
dataIndex: 'begin',
customRender: formatDate
},
{
title: '结束日期',
key: 'end',
dataIndex: 'end',
customRender: formatDate
},
{
title: '项目状态',
key: 'status',
dataIndex: 'status',
customRender: renderStatus
},
{
title: '操作',
key: 'action',
scopedSlots: { customRender: 'action' }
}
];
export default {
name: 'Project',
data () {
return {
formData: {
name: '',
owner: '',
status: ''
},
form: this.$form.createForm(this),
pagination: {
pageSize: 20,
current: 1
},
loading: false,
columns,
data: []
}
},
mounted () {
this.fetch(this.formData)
},
methods: {
handleSubmit (e) {
e.preventDefault()
this.pagination.current = 1
this.pagination.pageNo = 1
this.fetch(this.formData)
},
fetch (param = {}) {
this.loading = true;
const postData = { pageNo: this.pagination.current, ...param, ...this.pagination }
console.log('postData:', postData);
findPage(postData).then(res => {
const pagination = { ...this.pagination };
// pagination.total = data.totalCount;
pagination.total = res.data.total;
this.loading = false;
this.data = res.data.data;
this.pagination = pagination;
});
},
detal (row) {
console.log('row', row)
this.$router.push({
name: 'detail',
params: { projectId: row.id }
})
},
handleTableChange (pagination, filters, sorter) {
console.log(pagination);
const pager = { ...this.pagination };
pager.current = pagination.current;
this.pagination = pager;
this.fetch({
results: pagination.pageSize,
page: pagination.current,
sortField: sorter.field,
sortOrder: sorter.order,
...filters,
});
}
}
}
</script>
3、CSS样式
<style lang="less" scoped>
.btn-action {
padding: 0;
}
</style>