使用vue antd 体系实现一个树和表格的渲染
使用antd体系开发,这里是index.vue
<template>
<div>
<a-row :gutter="24">
<a-col :md="5" :sm="24">
<a-card :bordered="false" :loading="treeLoading">
<div v-if="this.categoryTree != ''">
<a-tree
style="scroll:true"
:treeData="categoryTree"
v-if="categoryTree.length"
@select="handleClick"
:defaultExpandAll="true"
:defaultExpandedKeys="defaultExpandedKeys"
:replaceFields="replaceFields" />
</div>
<div v-else>
<a-empty :image="simpleImage" />
</div>
</a-card>
</a-col>
<a-col :md="19" :sm="24">
<a-card :bordered="false" :bodyStyle="tstyle">
<div class="table-page-search-wrapper" v-if="hasPerm('category:page')">
<a-form layout="inline">
<a-row :gutter="48">
<a-col :md="6" :sm="24">
<a-form-item label="门类名">
<a-input v-model="queryParam.categoryName" allow-clear placeholder="请输入门类名" />
</a-form-item>
</a-col>
<a-col :md="6" :sm="24">
<a-form-item label="门类编码">
<a-input v-model="queryParam.code" allow-clear placeholder="请输入唯一编码" />
</a-form-item>
</a-col>
<a-col :md="6" :sm="24">
<a-form-item label="门类类型">
<a-select style="width: 100%" v-model="queryParam.categoryType" placeholder="请选择门类类型">
<a-select-option v-for="(item, index) in categoryTypeData" :key="index" :value="item.code">{{
item.name
}}</a-select-option>
</a-select>
</a-form-item>
</a-col>
<a-col :md="6" :sm="24">
<span class="table-page-search-submitButtons">
<a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
<a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
</span>
</a-col>
</a-row>
</a-form>
</div>
</a-card>
<a-card :bordered="false">
<s-table
ref="table"
:columns="columns"
:data="loadData"
:alert="options.alert"
:rowKey="record => record.id"
:rowSelection="options.rowSelection">
<template class="table-operator" slot="operator" v-if="hasPerm('category:add')">
<a-button type="primary" v-if="hasPerm('category:add')" icon="plus" @click="$refs.addForm.add()">新增门类
</a-button>
<a-button
type="danger"
:disabled="selectedRowKeys.length < 1"
v-if="hasPerm('category:delete')"
@click="batchDelete">
<a-icon type="delete" />批量删除
</a-button>
<x-down v-if="hasPerm('category:export')" ref="batchExport" @batchExport="batchExport" />
</template>
<span slot="categoryTypeScopedSlots" slot-scope="text">
{{ 'category_type' | dictType(text) }}
</span>
<span slot="action" slot-scope="text, record">
<a v-if="hasPerm('category:edit')" @click="$refs.editForm.edit(record)">编辑</a>
<a-divider type="vertical" v-if="hasPerm('category:edit') & hasPerm('category:delete')" />
<a-popconfirm
v-if="hasPerm('category:delete')"
placement="topRight"
title="确认删除?"
@confirm="() => singleDelete(record)">
<a>删除</a>
</a-popconfirm>
</span>
</s-table>
<add-form ref="addForm" :id="this.queryParam.pid" @ok="handleOk" />
<edit-form ref="editForm" @ok="handleOk" />
</a-card>
</a-col>
</a-row>
</div>
</template>
<script>
import { STable, XCard, XDown } from '@/components'
import { Empty } from 'ant-design-vue'
import {
getCategoryTree,
categoryPage,
categoryDelete,
categoryExport
} from '@/api/modular/main/category/categoryManage'
import addForm from './addForm.vue'
import editForm from './editForm.vue'
export default {
components: {
STable,
addForm,
editForm,
XDown,
XCard
},
data() {
return {
// 查询参数
queryParam: {},
// 表头
columns: [
{
title: '门类名',
align: 'center',
dataIndex: 'categoryName'
},
{
title: '唯一编码',
align: 'center',
dataIndex: 'code'
},
{
title: '门类类型',
align: 'center',
dataIndex: 'categoryType',
scopedSlots: { customRender: 'categoryTypeScopedSlots' }
},
{
title: '排序号',
align: 'center',
dataIndex: 'sort'
},
{
title: '备注',
align: 'center',
dataIndex: 'remaks'
}
],
tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
// 加载数据方法 必须为 Promise 对象
loadData: parameter => {
return categoryPage(Object.assign(parameter, this.queryParam)).then(res => {
return res.data
})
},
List: [],
categoryTree: [],
defaultExpandedKeys: [],
categoryTypeData: [],
selectedRowKeys: [],
selectedRows: [],
treeLoading: true,
simpleImage: Empty.PRESENTED_IMAGE_SIMPLE,
replaceFields: {
key: 'id'
},
options: {
alert: {
show: true,
clear: () => {
this.selectedRowKeys = []
}
},
rowSelection: {
selectedRowKeys: this.selectedRowKeys,
onChange: this.onSelectChange
}
}
}
},
created() {
this.getCategoryTree()
if (this.hasPerm('category:edit') || this.hasPerm('category:delete')) {
this.columns.push({
title: '操作',
width: '150px',
dataIndex: 'action',
scopedSlots: { customRender: 'action' }
})
}
const categoryTypeOption = this.$options
this.categoryTypeData = categoryTypeOption.filters['dictData']('category_type')
},
methods: {
// 获取树
getCategoryTree() {
getCategoryTree(Object.assign(this.queryParam)).then(res => {
this.treeLoading = false
if (!res.success) {
return
}
this.categoryTree = res.data
if (this.categoryTree.length > 0) {
this.queryParam.parentId = this.categoryTree[0].id
} else {
this.queryParam.parentId = 0
}
// 全部展开,上面api方法提供的不生效,先用此方法
for (var item of res.data) {
// eslint-disable-next-line eqeqeq
if (item.parentId == 0) {
this.defaultExpandedKeys.push(item.id)
}
}
this.$refs.table.refresh()
})
},
/**
* 单个删除
*/
singleDelete(record) {
const param = [{ id: record.id }]
this.categoryDelete(param)
},
/**
* 批量删除
*/
batchDelete() {
const paramIds = this.selectedRowKeys.map(d => {
return { id: d }
})
this.categoryDelete(paramIds)
},
categoryDelete(record) {
categoryDelete(record).then(res => {
if (res.success) {
this.$message.success('删除成功')
this.$refs.table.clearRefreshSelected()
this.getCategoryTree()
} else {
this.$message.error('删除失败') // + res.message
}
})
},
/**
* 批量导出
*/
batchExport() {
const paramIds = this.selectedRowKeys.map(d => {
return { id: d }
})
categoryExport(paramIds).then(res => {
this.$refs.batchExport.downloadfile(res)
})
},
handleClick(e) {
this.queryParam = {
pid: e.toString()
}
this.$refs.table.refresh(true)
},
handleOk() {
this.getCategoryTree()
this.$refs.table.refresh()
},
onSelectChange(selectedRowKeys, selectedRows) {
this.selectedRowKeys = selectedRowKeys
this.selectedRows = selectedRows
}
}
}
使用
tree组件,使用handleclick()响应树节点点击,传递参数pid(树节点的id)父组件向子组件传值:在父组件定义一个参数,然后把他绑定到要回填的组件上,在这里使用的是:id="this.queryParam.pid"在子组件使用props,在data中定义props:[绑定的key],这样就收到了,然后使用setFieldsValue回填到子组件表单中需要的位置。
setFieldsValue
在ant 中通过setFieldsValue 存储的类似是一种键值 对的形式,你可以通过对应的key 值来展示例如:
this.form.setFieldsValue(
{
pid: this.id
}
)
一般这种情况会报错,说这个this.form.setFieldsValue不是一个methods,所以可以使用一个定时器
setTimeout()=>{
this.form.setFieldsValue(
{
pid : this.id
}
),100}
除此之外,还应用了下拉树,在这里可以使用一个树的空间,叫做<tree-select>这一简单模块的前端大概就是这个样子。谢谢
转载自:https://segmentfault.com/a/1190000042188740