前言
在产线实习期间我们都是用PDCA高效思维法来进行任务书编写,受益颇多,因此开始尝试用此方法来记笔记。
Plan
往资产查询页面添加如下功能:
- 按状态搜索
- 导出数据
- 批量删除
Do
1. 按状态搜索
第一步:在搜索资产功能中添加资产状态组件
<el-form-item label="资产状态" prop="status">
<el-select v-model="queryParams.status" placeholder="请选择资产状态" clearable size="small">
<el-option
v-for="dict in statusOptions"
:key="dict.dictValue"
:label="dict.dictLabel"
:value="dict.dictValue"
/>
</el-select>
</el-form-item>
第二步:在查询参数中添加status
queryParams: {
title: null,
type: null,
pageNum: 1,
pageSize: 10,
status: null,
},
效果如下:
2. 导出数据
第一步:在新增资产中添加导出组件
<el-col :span="1.5">
<el-button
type="warning"
icon="el-icon-download"
size="mini"
@click="handleExport"
v-hasPermi="['asset:category:export']"
>导出</el-button>
</el-col>
第二步:js中导入exportCategory
import { listCategory,getCategory, delCategory, addCategory, updateCategory,exportCategory} from "@/api/asset/category";
第三步:添加导出按钮操作方法
/** 导出按钮操作 */
handleExport() {
const queryParams = this.queryParams;
this.$confirm('是否确认导出所有资产数据项?', "警告", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}).then(function() {
return exportCategory(queryParams);
}).then(response => {
this.download(response.msg);
}).catch(function() {});
}
效果如下:
3. 批量删除
第一步:在新增资产中添加删除组件
注意:disabled只有是multiple时,表示开启多选模式,才可以使用批量删除按钮
<el-col :span="1.5">
<el-button
type="danger"
icon="el-icon-delete"
size="mini"
:disabled="multiple"
@click="handleDelete"
v-hasPermi="['asset:category:remove']"
>删除</el-button>
</el-col>
第二步:在资产查询中添加好多选框并绑定好方法
代码如下:
<!-- 资产查询 -->
<el-table
v-loading="loading"
:data="categoryList"
row-key="id"
default-expand-all
:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
@selection-change="handleSelectionChange"
>
<el-table-column type="selection" width="55" align="center" />
<el-table-column label="分类名称" align="center" prop="title" />
<el-table-column label="分类类别" align="center" prop="type" :formatter="typeFormat" />
<el-table-column label="排序" align="center" prop="listSort" />
<el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<template slot-scope="scope">
<el-button
size="mini"
type="text"
icon="el-icon-edit"
@click="handleUpdate(scope.row)"
v-hasPermi="['asset:category:edit']"
>修改</el-button>
<el-button
size="mini"
type="text"
icon="el-icon-delete"
@click="handleDelete(scope.row)"
v-hasPermi="['asset:category:remove']"
>删除</el-button>
</template>
</el-table-column>
</el-table>
效果如下:
第三步:在data() return中添加好如下数据
// 选中数组
ids: [],
// 非单个禁用
single: true,
// 非多个禁用
multiple: true,
第四步:添加多选框选中数据方法
// 多选框选中数据
handleSelectionChange(selection) {
this.ids = selection.map(item => item.id)
this.single = selection.length!=1
this.multiple = !selection.length
},
注意:下图框中的数据必须和你从后端传给前端的数据编码号一样
第五步:之前的文章中我们已经实现了单个删除操作,现需修改删除按钮操作 handleDelete()方法
原来的代码:
/** 删除按钮操作 */
handleDelete(row) {
this.$confirm('是否确认删除资产分类编号为"' + row.id + '"的数据项?', "警告", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}).then(function() {
return delCategory(row.id);
}).then(() => {
this.getList();
this.msgSuccess("删除成功");
})
},
修改后的代码:
/** 删除按钮操作 */
handleDelete(row) {
const ids = row.id || this.ids;
this.$confirm('是否确认删除资产分类编号为"' + ids+ '"的数据项?', "警告", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}).then(function() {
return delCategory(ids);
}).then(() => {
this.getList();
this.msgSuccess("删除成功");
})
},
效果如下:
打算把C和A的部分放到下一篇,此篇只附上D的代码,不然篇幅太长!!!
资产查询页面总代码如下:
<template>
<div class="app-container">
<!-- 搜索资产功能 -->
<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<el-form-item label="分类名称" prop="title">
<el-input
v-model="queryParams.title"
placeholder="请输入分类名称"
clearable
size="small"
@keyup.enter.native="handleQuery"
/>
</el-form-item>
<el-form-item label="分类类别" prop="type">
<el-select v-model="queryParams.type" placeholder="请选择分类类别" clearable size="small">
<el-option
v-for="dict in typeOptions"
:key="dict.dictValue"
:label="dict.dictLabel"
:value="dict.dictValue"
/>
</el-select>
</el-form-item>
<el-form-item label="资产状态" prop="status">
<el-select v-model="queryParams.status" placeholder="请选择资产状态" clearable size="small">
<el-option
v-for="dict in statusOptions"
:key="dict.dictValue"
:label="dict.dictLabel"
:value="dict.dictValue"
/>
</el-select>
</el-form-item>
<el-form-item>
<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
</el-form-item>
</el-form>
<!-- 新增资产 -->
<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
<el-button
type="primary"
plain
icon="el-icon-plus"
size="mini"
@click="handleAdd"
v-hasPermi="['asset:category:add']"
>新增</el-button>
</el-col>
<!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
<el-col :span="1.5">
<el-button
type="warning"
icon="el-icon-download"
size="mini"
@click="handleExport"
v-hasPermi="['asset:category:export']"
>导出</el-button>
</el-col>
<el-col :span="1.5">
<el-button
type="danger"
icon="el-icon-delete"
size="mini"
:disabled="multiple"
@click="handleDelete"
v-hasPermi="['asset:category:remove']"
>删除</el-button>
</el-col>
</el-row>
<!-- 添加或修改资产分类对话框 -->
<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
<el-form ref="form" :model="form" :rules="rules" label-width="80px">
<el-form-item label="上级" prop="pid">
<treeselect v-model="form.pid" :options="categoryOptions" :normalizer="normalizer" placeholder="请选择上级" />
</el-form-item>
<el-form-item label="分类名称" prop="title">
<el-input v-model="form.title" placeholder="请输入分类名称" />
</el-form-item>
<el-form-item label="分类类别" prop="type">
<el-select v-model="form.type" placeholder="请选择分类类别">
<el-option
v-for="dict in typeOptions"
:key="dict.dictValue"
:label="dict.dictLabel"
:value="dict.dictValue"
></el-option>
</el-select>
</el-form-item>
<el-form-item label="排序" prop="listSort">
<el-input v-model="form.listSort" placeholder="请输入排序" />
</el-form-item>
<el-form-item label="状态">
<el-radio-group v-model="form.status">
<el-radio
v-for="dict in statusOptions"
:key="dict.dictValue"
:label="dict.dictValue"
>{{dict.dictLabel}}</el-radio>
</el-radio-group>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="primary" @click="submitForm">确 定</el-button>
<el-button @click="cancel">取 消</el-button>
</div>
</el-dialog>
<!-- 资产查询 -->
<el-table
v-loading="loading"
:data="categoryList"
row-key="id"
default-expand-all
:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
@selection-change="handleSelectionChange"
>
<el-table-column type="selection" width="55" align="center" />
<el-table-column label="分类名称" align="center" prop="title" />
<el-table-column label="分类类别" align="center" prop="type" :formatter="typeFormat" />
<el-table-column label="排序" align="center" prop="listSort" />
<el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<template slot-scope="scope">
<el-button
size="mini"
type="text"
icon="el-icon-edit"
@click="handleUpdate(scope.row)"
v-hasPermi="['asset:category:edit']"
>修改</el-button>
<el-button
size="mini"
type="text"
icon="el-icon-delete"
@click="handleDelete(scope.row)"
v-hasPermi="['asset:category:remove']"
>删除</el-button>
</template>
</el-table-column>
</el-table>
<!-- 分页功能 -->
<pagination
v-show="total>0"
:total="total"
:page.sync="queryParams.pageNum"
:limit.sync="queryParams.pageSize"
@pagination="getList"
/>
</div>
</template>
<script>
import { listCategory,getCategory, delCategory, addCategory, updateCategory,exportCategory,} from "@/api/asset/category";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
name: "demo",
components: {
Treeselect
},
data() {
return {
// 遮罩层
loading: true,
// 资产分类表格数据
categoryList: [],
// 分类类别字典
typeOptions: [],
// 状态字典
statusOptions: [],
// 弹出层标题
title: "",
// 资产分类树选项
categoryOptions: [],
// 是否显示弹出层
open: false,
// 表单参数
form: {},
// 表单校验
rules: {
title: [
{ required: true, message: "分类名称不能为空", trigger: "blur" }
],
type: [
{ required: true, message: "分类类别不能为空", trigger: "blur" }
],
status: [
{ required: true, message: "状态不能为空", trigger: "blur" }
]
},
// 显示搜索条件
showSearch: true,
// 查询参数
queryParams: {
title: null,
type: null,
pageNum: 1,
pageSize: 10,
status: null,
},
// 总条数
total: 0,
// 选中数组
ids: [],
// 非单个禁用
single: true,
// 非多个禁用
multiple: true,
};
},
created() {
this.getList();
this.getDicts("cate_type").then((response) => {
this.typeOptions = response.data;
});
this.getDicts("ext_status").then((response) => {
this.statusOptions = response.data;
});
},
methods: {
/** 查询资产分类列表 */
getList() {
this.loading = true;
listCategory(this.queryParams).then((response) => {
this.categoryList = this.handleTree(response.rows, "id", "pid");
// this.categoryList = response.rows;
this.total = response.total;
this.loading = false;
});
},
// 分类类别字典翻译
typeFormat(row, column) {
return this.selectDictLabel(this.typeOptions, row.type);
},
// 状态字典翻译
statusFormat(row, column) {
return this.selectDictLabel(this.statusOptions, row.status);
},
/** 查询部门下拉树结构 */
getTreeselect() {
listCategory().then(response => {
this.categoryOptions = [];
const data = { id: 0, title: '顶级节点', children: [] };
data.children = this.handleTree(response.rows, "id", "pid");
this.categoryOptions.push(data);
});
},
/** 转换资产分类数据结构 */
normalizer(node) {
if (node.children && !node.children.length) {
delete node.children;
}
return {
id: node.id,
label: node.title,
children: node.children
};
},
// 取消按钮
cancel() {
this.open = false;
this.reset();
},
// 表单重置
reset() {
this.form = {
id: null,
title: null,
pid: 0,
type: '1',
listSort: null,
status: "1"
};
this.resetForm("form");
},
/** 新增按钮操作 */
handleAdd() {
this.reset();
this.getTreeselect();
this.open = true;
this.title = "添加资产分类";
},
/** 确认按钮 */
submitForm() {
this.$refs["form"].validate(valid => {
if (valid) {
if (this.form.id != null) {
updateCategory(this.form).then(response => {
this.msgSuccess("修改成功");
this.open = false;
this.getList();
});
} else {
addCategory(this.form).then(response => {
this.msgSuccess("新增成功");
this.open = false;
this.getList();
});
}
}
});
},
/** 修改按钮操作 */
handleUpdate(row) {
this.reset();
this.getTreeselect();
if (row != null) {
this.form.pid = row.id;
}
getCategory(row.id).then(response => {
this.form = response.data;
this.open = true;
this.title = "修改资产分类";
});
},
/** 删除按钮操作 */
handleDelete(row) {
const ids = row.id || this.ids;
this.$confirm('是否确认删除资产分类编号为"' + ids+ '"的数据项?', "警告", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}).then(function() {
return delCategory(ids);
}).then(() => {
this.getList();
this.msgSuccess("删除成功");
})
},
/** 搜索按钮操作 */
handleQuery() {
this.getList();
},
/** 重置按钮操作 */
resetQuery() {
this.resetForm("queryForm");
this.handleQuery();
},
/** 导出按钮操作 */
handleExport() {
const queryParams = this.queryParams;
this.$confirm('是否确认导出所有资产数据项?', "警告", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning"
}).then(function() {
return exportCategory(queryParams);
}).then(response => {
this.download(response.msg);
}).catch(function() {});
},
// 多选框选中数据
handleSelectionChange(selection) {
this.ids = selection.map(item => item.id)
this.single = selection.length!=1
this.multiple = !selection.length
},
}
};
</script>