vue 中后台 列表的增删改查同一解决方案

查看 & 查询

常⻅业务列表⻚都是由 搜索栏 和 数据列表 组成。 其中:

  • 搜索栏包含 搜索条件 、 新增 、 批量xx 、 导出 等对 数据列表 全局操作功能项。
  • 数据列表包含 分⻚ 和每条数据末尾的 操作 项, ⽤于对当前这条数据进⾏ 修改 、 删除 等操

作。

 

这⾥将 列表 、 删除 、 导出 功能放在⼀起实现, 是因为它们都属于直接在 列表⻚ 中进⾏操作的功

能, 同时项⽬中已将 列表⻚ 中 常⽤的业务功能 封装成⼀个 简易 的 业务可复⽤类 。 其中可配置属性如下:

data () {
    /* eslint-disable */
    return {
      // 设置属性
      mixinViewModuleOptions: {
        createdIsNeed: true,       // 此页面是否在创建时,调用查询数据列表接口?
        activatedIsNeed: false,    // 此页面是否在激活(进入)时,调用查询数据列表接口?
        getDataListURL: '',       // 数据列表接口,API地址
        getDataListIsPage: false, // 数据列表接口,是否需要分页?
        deleteURL: '',            // 删除接口,API地址
        deleteIsBatch: false,     // 删除接口,是否需要批量?
        deleteIsBatchKey: 'id',   // 删除接口,批量状态下由那个key进行标记操作?比如:pid,uid...
        exportURL: ''             // 导出接口,API地址
      },
      // 默认属性
      dataForm: {},               // 查询条件
      dataList: [],               // 数据列表
      order: '',                  // 排序,asc/desc
      orderField: '',             // 排序,字段
      page: 1,                    // 当前页码
      limit: 10,                  // 每页数
      total: 0,                   // 总条数
      dataListLoading: false,     // 数据列表,loading状态
      dataListSelections: [],     // 数据列表,多选项
      addOrUpdateVisible: false,   // 新增/更新,弹窗visible状态
      lookVisible: false,          // 查看
    }
    /* eslint-enable */
  }
  1. 在业务组件中 ***.vue 文件, 修改 API请求 相关配置属性, 新增 name 和 type 查询条件。
data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/demo/goods/page',
        getDataListIsPage: true,
        deleteURL: '/demo/goods',
        deleteIsBatch: true,
        exportURL: '/demo/goods/page'
      },
      dataForm: {
        paramCode: '',
        type: null
      }
    }
  },

 

增加 & 修改

常⻅业务 新增 和 修改 功能⼏乎⼀样, 唯⼀区别在于它们 ⾃增ID 是否存在, 所以业务将它们放在同

⼀弹窗中处理。

考虑到它们的功能会存在⼀些特定业务处理, 就没有封装 业务可复⽤类 。

  1. 新建打开 ***add-or-update.vue.vue 文件, 写入 Form 表单及其相关校验方法等。
  2. 增加请求方法
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http
        .get(`/demo/product/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          }
        })
        .catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs.dataForm.validate((valid) => {
          if (!valid) {
            return false
          }
          this.$http[!this.dataForm.id ? 'post' : 'put'](
            '/demo/product/',
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg)
              }
              this.$message({
                message: this.$t('prompt.success'),
                type: 'success',
                duration: 500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
            .catch(() => {})
        })
      },
      1000,
      { leading: true, trailing: false }
    )
  }

 

posted @ 2020-09-09 13:31  仲灏  阅读(627)  评论(0编辑  收藏  举报