在使用vue(js)+elelment(ui)开发一些后台管理项目的时候,基本会用到 列表页,条件搜索(search),表格数据(table),分页,操作栏的,增、删、改、查几种操作。如下图这样的:
很多的页面都很类似,这里就给大家介绍一个,我自己最近研究的一个插件 avue-curd;整体的一个模块,包含列表,增、删、改、查;还可以通过slot插巣自定义局部组件内容
如下面,我按照产品自定义:隐藏了搜索模块、操作栏(这些都是可配置的)
上代码:主页面
<!--列表-->
<template><div class="execution"><basic-container><avue-crudref="crud"v-model="form":page="page":data="tableData":permission="permissionList":table-loading="tableLoading":option="tableOption"@on-load="getList"@refresh-change="refreshChange"@size-change="sizeChange"@current-change="currentChange"@row-update="handleUpdate"><template slot-scope="scope" slot="menu"><el-linkv-if="scope.row.status == -1"icon="el-icon-edit"type="primary"@click.stop="handleEdit(scope.row, scope.index)">审核</el-link></template><template slot="statusForm"><el-form-item style="margin-bottom: 0px"><el-radio-group@change="changeRequire"v-model="form.status"size="small"><el-radio border label="0">审核通过</el-radio><el-radio border label="-2">审核拒绝</el-radio></el-radio-group></el-form-item></template><template slot="status" slot-scope="scope"><span v-if="scope.row.status == 0" style="color: #67c23a">审核通过</span><span v-if="scope.row.status == -1" style="color: #409eff">待审核</span><span v-if="scope.row.status == -2" style="color: #f56c6c">审核拒绝</span></template></avue-crud></basic-container></div>
</template><script>
import { tenantPage, checkObj } from "@/api/admin/tenant";
import { tableOption } from "@/const/crud/admin/approval";
import { mapGetters } from "vuex";export default {name: "Tenant",data() {return {tableData: [],searchForm: {},form: {},page: {total: 0, // 总页数currentPage: 1, // 当前页数pageSize: 20, // 每页显示多少条},tableLoading: false,tableOption: tableOption,};},computed: {...mapGetters(["permissions"]),permissionList() {return {editBtn: this.vaildData(this.permissions.admin_systenant_edit, false),};},},methods: {handleEdit(row, index) {this.$refs.crud.rowEdit(row, index);},changeRequire(e) {if (e == "-2") {this.$refs.crud.formRules.rejectReason[0].required = true;} else {this.$refs.crud.formRules.rejectReason[0].required = false;}},getList(page, params) {this.tableLoading = true;tenantPage(Object.assign({current: page.currentPage,size: page.pageSize,},params,this.searchForm)).then((response) => {this.tableData = response.data.data.records;this.page.total = response.data.data.total;this.tableLoading = false;}).catch(() => {this.tableLoading = false;});},rowDel: function (row, index) {var _this = this;this.$confirm("是否确认删除机构名称为:" + row.name, "提示", {confirmButtonText: "确定",cancelButtonText: "取消",type: "warning",}).then(function () {return delObj(row.id);}).then((data) => {_this.$message.success("删除成功");this.getList(this.page);});},// 编辑弹窗确定handleUpdate: function (row, index, done, loading) {console.log(123321);checkObj(row).then((data) => {this.$message.success("修改成功");done();this.getList(this.page);}).catch(() => {loading();});},searchChange(form, done) {this.searchForm = form;this.page.currentPage = 1;this.getList(this.page, form);done();},refreshChange() {this.getList(this.page);},sizeChange(pageSize) {this.page.pageSize = pageSize;},currentChange(current) {this.page.currentPage = current;},},
};
</script>
引用配置表格配置字段option
/** 列表表格数据字段信息*/
var validatePhone = (rule, value, callback) => {const reg = /^1[3|4|5|7|8|9][0-9]{9}$/;if (reg.test(value)) {callback();} else {callback(new Error("手机号格式不正确"));}
};
var validateEmail = (rule, value, callback) => {const reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;if (reg.test(value)) {callback();} else {callback(new Error("邮箱格式不正确"));}
};
var dicStatus = [{value: "0",label: "审核通过",type: "status_type",description: "审核通过"},{value: "-2",label: "审核拒绝",type: "status_type",description: "审核拒绝"},{value: "-1",label: "待审核",disabled: true,type: "status_type",description: "待审核"}
];
export const tableOption = {border: true,index: true,indexLabel: "序号",stripe: true,menuAlign: "center",align: "center",labelWidth: 120, // 弹出框表单lable宽度span: 24, // 弹出框表单项dialogWidth: 600, // 弹出框宽度addBtn: false, // 添加按钮columnBtn: false, // 列显隐按钮delBtn: false, // 行内删除按钮refreshBtn: false, // 刷新按钮editBtn: false, // 表格编辑按钮editBtnText: "审核",editTitle: "租户注册审核",menuWidth: 150,// menu: false, // 隐藏菜单column: [{label: "ID",prop: "id",editDisabled: true,editDisplay: false,addDisplay: false},{label: "机构名称",prop: "name",width: 200,editDisabled: true,rules: [{ required: true, message: "请输入机构名称", trigger: "blur" },{ min: 3, max: 32, message: "长度在 3 到 32 个字符", trigger: "blur" }]},{label: "租户编号",prop: "code",editDisabled: true,editDisplay: false,addDisplay: false},{label: "联系人账号",prop: "contactUsername",width: 120,hide: true,editDisplay: false,addDisplay: false,rules: [{required: true,message: "请输入联系人",trigger: "blur"}]},{label: "联系人姓名",prop: "contactRealName",width: 120,editDisabled: true,addDisplay: false,rules: [{required: true,message: "请输入联系人",trigger: "blur"}]},{label: "手机号",prop: "contactPhone",width: 150,editDisabled: true,addDisplay: false,rules: [{required: true,message: "请输入手机号",trigger: "blur"},{validator: validatePhone,trigger: "blur"}],editDisabled: true},{label: "邮箱",prop: "contactEmail",width: 180,editDisabled: true,addDisplay: false,rules: [{required: true,message: "请输入邮箱",trigger: "blur"},{validator: validateEmail,trigger: "blur"}]},{label: "审核状态",prop: "status",type: "radio",border: true,dicData: dicStatus,addDisplay: false,formslot: true,typeslot: true,slot: true,rules: [{required: true,message: "请选择审核状态",trigger: "blur"}]},{label: "提交时间",prop: "createTime",width: 180,type: "datetime",format: "yyyy-MM-dd hh:mm:ss",valueFormat: "yyyy-MM-dd hh:mm:ss",rules: [{required: true,message: "请输入创建时间",trigger: "blur"}],editDisabled: true,addDisplay: false,editDisplay: false},{label: "拒绝原因",prop: "rejectReason",editDisabled: false,addDisplay: false,hide: true,rules: [{required: false,message: "请输入拒绝原因",trigger: "blur"}]}]
};
具体的配置可以自己看avue-curd的文档配置:https://www.bookstack.cn/read/avue-2.x/391487d752fdf241.md
官方文档地址:https://avuejs.com/doc/crud/crud-doc
再结合solt的使用,你就可以实现以下,个性化的配置了。
<!----------------------------------------------------2020.12.15此处更新一处,crud里面自定义搜索项,本例子已状态项status为例 ------------------------------->
需求是列表上方的状态需要加个全部的选项,如下图
如果直接修改数据源改成这样的
var dicStatus = [{value: null,label: "全部",type: "status_type",description: "全部"},{value: "0",label: "正常",type: "status_type",description: "状态正常"},{value: "9",label: "冻结",type: "status_type",description: "状态冻结"}
];
新增或者编辑回显的时候,就是出现尴尬的一幕,如下图,不需要全部啊,
因为avue-crud里面,所有的封装的参数项都是,同一出处;这里就需要搜索框自定义了,search: true, searchslot: true, 代码如下:
<avue-crudref="crud"v-model="form":page="page":data="tableData":permission="permissionList":table-loading="tableLoading":option="tableOption":search.sync="search" // 此处绑定搜索条件@on-load="getList"@search-change="searchChange"@refresh-change="refreshChange"@size-change="sizeChange"@current-change="currentChange"@row-update="handleUpdate"@row-save="handleSave"@row-del="rowDel"@sort-change="sortChange"><template slot="status" slot-scope="scope"><el-tag v-if="scope.row.status == 0">正常</el-tag><el-tag v-if="scope.row.status == 9" type="info">冻结</el-tag><el-tag v-if="scope.row.status == -1" type="warning">待审核</el-tag><el-tag v-if="scope.row.status == -2" type="danger">审核拒绝</el-tag></template> // 此处注意slot是 参数名字(status)+ Search<template slot="statusSearch" slot-scope="scope"><el-select v-model="search.status" placeholder="请选择"><el-optionv-for="item in statusOptions":key="item.value":label="item.label":value="item.value"></el-option></el-select></template></avue-crud>// data()里面新增search,这是crud搜索条件的集合search: {},statusOptions: [{value: null,label: "全部",type: "status_type",description: "全部",},{value: "0",label: "正常",type: "status_type",description: "状态正常",},{value: "9",label: "冻结",type: "status_type",description: "状态冻结",},],