VUE结合Element-ui动态生成添删改查页面组件

VUE结合Element-ui动态生成添删改查页面组件

组件下载链接:

组件附件下载链接


前言

这篇文章是因为后台项目中添删改查的页面几乎都是差不多,于是本来就整合了一下element-ui下的几个组件,只需要传入json数据,页面就能自动根据json数据生成,大大见减少了重复代码,文章开头有本文章的对应文件,我就不贴文件代码了(太长了)
PS:文章使用的是VUE+Element-UI整合

先看看生成后的大概样子吧

生成后大概的样子

使用示例

<template>
<div>
  <crud-table
    theme-name="按键设置"
    :list="keyList"
    :form-body="formBody"
    :search-body="searchBody"
    :table-head="tableHead"
    @del="doDel"
    @add="doAdd"
    @search="doSearch"
    @delSelection="doDelSelection"
  >
  </crud-table>
</div>
</template>

<script>
import * as keyword from '@/api/subAdmin/keyWord'
export default {
  data() {
    return {
      keyList:[],
      tableHead:[],
      formBody:[
        {
          fieldKey:'keyword_num',
          fieldName:'按键变量',
          fieldType:'text',
          setting:{
            text:{
              placeholder:'请输入按键变量'
            }
          }
        },
        {
          fieldKey:'description',
          fieldName:'语音描述',
          fieldType:'text',
          setting:{
            text:{
              placeholder:'请输入语音描述'
            }
          }
        },
        {
          fieldKey:'call_center_id',
          fieldName:'呼叫中心id',
          fieldType:'text',
          setting:{
            text: {
              placeholder: '请输入呼叫中心id(之后就是自己获取呼叫中心id)'
            }
          }
        }
      ],
      searchBody:[],
      exportExcelHead:{},
    }
  },
  computed: {},
  created() {
    this.getAll();
  },
  methods: {
    getAll() {
      console.log("getAll")
    },
    // 删除回调
    doDel(row){
      console.log(row)
      // keyword.delKeyWordById(row)
    },
    // 添加回调
    doAdd(formEntity){
      console.log(JSON.stringify(Entity))
      let data={
        create_keyword_info: Entity
      }
      keyword.createKeyWord(data)
    },

    // 搜索回调
    doSearch(searchEntity){
      console.log(searchEntity)
    },

    // 删除所有行回调
    doDelSelection(selections){
      console.log(selections)
    },

    // 修改回调
    doEdit(fromEntity){
      console.log(fromEntity)
    },
  }
}
</script>
<style scoped>
</style>

crudTable Attributes

参数说明类型可选值默认值
themeName主题名 列:分类String主题
needAdd是否需要添加按钮Booleantrue
needDel是否需要删除按钮Booleantrue
needEdit是否需要修改按钮Booleantrue
needSearch是否需要搜索按钮Booleantrue
needAction是否需要操作字段Booleantrue
needPagination是否需要分页(暂时未集成)Booleanfalse
needSelection是否需要多选列Booleantrue
needExportExcel是否需要导出表格Booleantrue
needRefresh是否需要刷新按钮Booleantrue
list表格具体数据Array见下表
tableId表list数据中的ID字段名称 用于删除操作(如果不传的话删除操作返回当前行)Stringid
tableHead需要显示的表头(数据来源于list中)Array见tableHead Attributes
formBody表单需要字段(点击修改和添加时显示的表单)Array见formBody Attributes
searchBody搜索时需要的字段(顶部搜索显示的条件)Array见searchBody Attributes
tableLoading表格body是否加载中(用于数据请求的时候)Booleanfalse
saveLoading保存按钮是否加载中(用于数据请求的时候)Booleanfalse
delLoading删除按钮是否加载中(用于数据请求的时候)Booleanfalse
editLoading修改按钮是否加载中(用于数据请求的时候)Booleanfalse
buttonSize顶部按钮大小Stringmedium / small / minimedium
exportExcelHead导出的表格头Object见exportExcelHead Attributes

list 数据示例

// 表格数据
    list: {
      type: Array,
      default: function() {
        return [
          {
            id: '1',
            userName: 'Mr.He',
            password: '123456',
            switchValue: true,
            checkBoxValue: []
          },
          {
            id: '2',
            userName: 'Mr.Ming',
            password: '789123',
            selectValue: 'HeXunFang',
            radioValue: '单选按钮1',
            checkBoxValue: ['复选框1', '复选框2', '复选框3', '复选框1', '复选框2', '复选框3']
          }]
      }
    },

tableHead Attributes

参数说明类型可选值默认值
key表头对应List中的字段名称String
value表头显示名称String
type表格列的类型Stringtext/switch
setting当类型为switch时,这个值是必须得,你可以为空,但是要声明这字段Object见setting Attributes

tableHead setting Attrbutes

setting相关解释

tableHead 数据示例

    // 需要展示的表头 ps:通过key拿到list中的数据 value是表头中文名
    // params:[{
    //   key:"String",
    //   value:"String",
    //   type:"text"||"Switch"
    // }]
    tableHead: {
      type: Array,
      default: function() {
        return [
          {
            key: 'userName',
            value: '用户名',
            type: 'text'
          },
          {
            key: 'switchValue',
            value: '开关',
            type: 'switch',
            setting: {
              no: '正常',
              off: '封禁'
            }
          },
          {
            key: 'checkBoxValue',
            value: '复选框',
            type: 'checkBox'
          }
        ]
      }
    },

formBody Attrbutes

参数说明类型可选值默认值
fieldName表单字段名称(input框前面)String
fieldKey表单对应表头中的字段key(修改时获取数据)String
fieldType表单字段类型Stringtext/checkBox/radio/select/switch
setting相关设置Object见 formBody setting Attrbutes

formBody setting Attrbutes

参数说明类型可选值默认值
text类型为text相关设置Object{placeholder(输入框提示)}
checkBox类型为checkBox相关设置Object{checkBoxItem:[{lable:‘复选框的值’)}]}
radio类型为radio相关设置Object{radioItem:[{label:‘选项值’}]}
select类型为select相关设置Objectoption:[{label:‘下拉选项展示’,value:‘选中的值’}]
switch类型为switch相关设置Objectdisabled:false(是否禁用)

formBody 数据示例

// 表单字段
    // params:[
    //   {
    //     fieldName:"String", //字段中文名
    //     fieldKey:"String" //表单数据字段key
    //     fieldType:"text"||"select"||"radio"||"checkBox"||"DatePicker"||"Switch"||"textarea" //类型
    //     options:{
    //        text:{placeholder:"提示信息"}
    //        select:{相关设置}
    //     }
    //   }
    // ]
    formBody: {
      type: Array,
      default: function() {
        return [
          {
            fieldName: '文本',
            fieldKey: 'userName',
            fieldType: 'text',
            setting: {
              text: {
                placeholder: '请输入项目名'
              }
            }
          },
          {
            fieldName: '复选框',
            fieldKey: 'checkBoxValue',
            fieldType: 'checkBox',
            setting: {
              checkBox: {
                checkBoxItem: [
                  {
                    label: '复选框1'
                  },
                  {
                    label: '复选框2'
                  },
                  {
                    label: '复选框3'
                  },
                  {
                    label: '复选框4'
                  }
                ]
              }
            }
          },
          {
            fieldName: '单选框',
            fieldKey: 'radioValue',
            fieldType: 'radio',
            setting: {
              radio: {
                radioItem: [
                  {
                    label: '单选按钮1'
                  },
                  {
                    label: '单选按钮2'
                  }
                ]
              }
            }
          },
          {
            fieldName: '下拉框',
            fieldKey: 'selectValue',
            fieldType: 'select',
            setting: {
              select: {
                option: [
                  {
                    label: '下拉选择项1',
                    value: 'HeXunFang',
                    disabled: true
                  },
                  {
                    label: '下拉选择项2',
                    value: 'test2'
                  },
                  {
                    label: '下拉选择3',
                    value: 'test3'
                  }
                ]
              }
            }
          },
          {
            fieldName: '开关',
            fieldKey: 'switchValue',
            fieldType: 'switch',
            setting: {
              switch: {
                disabled: false
              }
            }
          }
        ]
      }
    },

searchBody Attrbutes

参数说明类型可选值默认值
searchName搜索字段名称String
searchKey搜索字段key(最好跟tableKey一样,不过也无所谓,最后返回的是一个实体给页面)
searchType搜索框的类型Stringtext/select/date
setting相关类型设置Object见searchBody setting Attrbutes

searchBody setting Attrbutes

参数说明类型可选值默认值
placeholder对应类型是(text/select)可用String
options对应类型select可用Object{options:[{label:‘选项’,value:‘选项value’}]
format对应类型date可用String参考element-ui官方时间组件timestamp

searchBody 数据示例

// 搜索条件的输入框
    searchBody: {
      type: Array,
      default: function() {
        return [
          {
            searchName: '用户名',
            searchKey: 'userName',
            searchType: 'text',
            setting: {
              placeholder: '搜索用户名'
            }
          },
          {
            searchName: '用户名',
            searchKey: 'userName1',
            searchType: 'select',
            setting: {
              options: [
                {
                  label: '选项一',
                  value: '1'
                }
              ],
              placeholder: '搜索用户名'
            }
          },
          {
            searchName: '用户名',
            searchKey: 'userName1',
            searchType: 'select',
            setting: {
              options: [
                {
                  label: '选项一',
                  value: '1'
                }
              ],
              placeholder: '搜索用户名'
            }
          },
          {
            searchName: '用户名',
            searchKey: 'date',
            searchType: 'date',
            setting: {
              // 参考element-ui 时间选择组件
              format:'timestamp'
            }
          }
        ]
      }
    },

exportExcelHead Attrbutes

参数说明类型可选值默认值
fileName导出表格名称String
format导出数据表格式Stringxlsx/jsonxlsx
autoWidth表格是否自动宽度Booleanfasle/truetrue
head所需要导出的表头Object见exportExcelHead head Attrbutes

exportExcelHead head Attrbutes

参数说明类型可选值默认值
exFieldName导出表头名称Stringtest
exFieldKey导出表头对应的key(与list字段key相关联)String

exportExcelHead 数据示例

// 导出表格的表头  如果没有字段,则导出全部字段
    exportExcelHead: {
      type: Object,
      default: function() {
        return {
          fileName: this.themeName + '数据表',
          format: 'xlsx',
          autoWidth: true,
          head: [
            {
              // 表头名
              exFieldName: '用户名',
              // 表头对应表格list中的字段key
              exFieldKey: 'userName'
            }
          ]
        }
      }
    }

crudTable Events

事件名称说明回调参数
Add添加提交的回调formEntity
Del删除行的回调如果this.tableId不为空的话返回选中行中this.tableId字段的值/否则返回当前行
Edit修改提交的回调formEntity
Search查询提交的回调searchEntity(查询的条件)
DelSelection删除选中行的回调selections(选中的row)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值