element 表格操作列子

<template>
  <div>
    <el-card style="margin:20px 0">
      <CategorySelect :show="!isShwoTable" @getCategoryId="getCategoryId" />
    </el-card>
    <el-card>
      <div v-show="isShwoTable">
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addAttr">添加属性</el-button>
        <el-table
          style="width: 100%"
          border
          :data="attrList"
        >
          <el-table-column
            prop="prop"
            type="index"
            label="序号"
            width="80"
            align="center"
          />
          <el-table-column
            prop="attrName"
            label="属性名称"
            width="150"
          />
          <el-table-column
            prop="prop"
            label="属性值列表"
            width="width"
          >
            <template slot-scope="{row}">
              <!-- {{ row }} -->
              <el-tag v-for="(attrValue,index) in row.attrValueList" :key="attrValue.id" type="success">
                {{ attrValue.valueName }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="prop"
            label="修改"
            width="150"
          >
            <template slot-scope="{row}">
              <!-- {{ row }} -->
              <el-button type="warning" icon="el-icon-edit" size="mini" @click="upDateAttr(row)" />
              <el-button type="danger" icon="el-icon-delete" size="mini" />
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 添加属性或者修改属性结构 -->
      <div v-show="!isShwoTable">
        <el-form ref="form" :inline="true" label-width="80px" :model="attrInfo">
          <el-form-item label="属性名">
            <el-input v-model="attrInfo.attrName" placeholder="请输入属性名" />
          </el-form-item>
        </el-form>
        <el-button type="primary" icon="el-icon-plus" :disabled="!attrInfo.attrName" @click="addAttrValue">添加属性值</el-button>
        <el-button @click="isShwoTable=true">取消</el-button>
        <el-table
          :data="attrInfo.attrValueList"
          style="width: 100%;margin:20px 0"
          border=""
        >
          <el-table-column
            type="index"
            label="序号"
            width="80"
            align="center"
          />
          <el-table-column

            label="属性值名称"
            width="width"
          >
            <template slot-scope="{row,$index}">
              <el-input v-if="row.flag" :ref="$index" v-model="row.valueName" placeholder="请输入属性名称" size="mini" @blur="toLook(row)" @keyup.native.enter="row.flag=false" />
              <span v-else style="display:block" @click="toEdit(row,$index)">{{ row.valueName }}</span>
            </template>
          </el-table-column>
          <el-table-column

            label="操作"
            width="width"
          >
            <template slot-scope="{row,$index}">
              <!-- 气泡确认框 -->
              <el-popconfirm
                :title="`确定删除《${row.valueName}》?`"
                @onConfirm="deleteAttrValue($index)"
              >
                <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini" />
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-button type="primary" :disabled="attrInfo.attrValueList.length<1" @click="addOrUpdateAttr">保存</el-button>
        <el-button @click="isShwoTable=true">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
// 按需引入lodash的深拷贝

// import cloneDeep from 'lodash/clonedeep'
import cloneDeep from 'lodash/cloneDeep'
export default {
  name: 'Attr',
  data() {
    return {
      category1Id: '',
      category2Id: '',
      category3Id: '',
      // 平台属性字段
      attrList: [],
      isShwoTable: true,
      // 收集新增属性或修改属性
      attrInfo: {
        'attrName': '', // 属性名
        'attrValueList': [ // 属性名中属性值,因为属性值可以是多个,因此需要的是数组
          // {
          //   'attrId': 0, // 属性的id
          //   'valueName': '' // 属性值
          // }
        ],
        'categoryId': 0, // category3Id三级分类的id
        'categoryLevel': 3
      }
    }
  },
  methods: {
    getCategoryId({ categoryId, level }) {
      // console.log(categoryId)
      if (level === 1) {
        this.category1Id = categoryId
        this.category2Id = ''
        this.category3Id = ''
      } else if (level === 2) {
      // console.log(categoryId)
        this.category2Id = categoryId
        this.category3Id = ''
      } else {
      // console.log(categoryId)
        this.category3Id = categoryId
        this.getAttrList()
      }
    },

    async getAttrList() {
      // 获取分类的ID
      const { category1Id, category2Id, category3Id } = this
      // 获取属性列表的数据
      const result = await this.$API.attr.reqAttrList(
        category1Id,
        category2Id,
        category3Id
      )
      // console.log(result)
      if (result.code === 200) {
        this.attrList = result.data
      }
    },
    // 添加属性值
    addAttrValue() {
      // 向属性值里面添加元素 添加属性的时候还没有id 需要后台分配id
      // 响应属性值得名称valueName
      this.attrInfo.attrValueList.push({
        'attrId': this.attrInfo.id, // 修改某一属性的时候 在原有基础上修改id
        'valueName': '',
        flag: true // 给每个属性值添加标记 用于切换显示方式 查看模式和编辑模式 让每一个属性值控制自己的属性切换
      })
      // 点击添加进行自动聚焦
      this.$nextTick(() => {
        // console.log(this.$refs[index])
        // 获取响应的表单实现聚焦
        this.$refs[this.attrInfo.attrValueList.length - 1].focus()
      })
    },
    // 添加属性回调
    addAttr() {
      // 切换tabel的现实与隐藏
      this.isShwoTable = false
      // 清除数据
      this.attrInfo = {
        'attrName': '', // 属性名
        'attrValueList': [ // 属性名中属性值,因为属性值可以是多个,因此需要的是数组
          // {
          //   'attrId': 0, // 属性的id
          //   'valueName': '' // 属性值
          // }
        ],
        'categoryId': this.category3Id, // category3Id三级分类的id
        'categoryLevel': 3
      }
    },
    // 修改某一个属性
    upDateAttr(row) {
      this.isShwoTable = false
      // console.log(row)
      this.attrInfo = cloneDeep(row)
      // 修改属性添加flag值
      this.attrInfo.attrValueList.forEach(item => {
      //  item.flag=false // 这里不能这么用 因为这样添加的数据不能为响应式数据 因为vue无法探测普通的新增属性
        this.$set(item, 'flag', false)
      })
    },
    // 切换焦点事件
    toLook(row) {
      if (row.valueName.trim() == '') {
        this.$message('请你输入一个属性值')
        return
      }
      // 新增的属性不能与已有的重复
      const repet = this.attrInfo.attrValueList.some(item => {
        // 将row从数组里面判断去除
        if (row !== item) {
          return row.valueName == item.valueName
        }
      })
      if (repet) {
        this.$message('请请不要重复输入')
        return
      }
      // console.log(repet)
      row.flag = false
    },
    // 点击span进入编辑模式
    toEdit(row, index) {
      row.flag = true
      // 获取节点实现自动聚焦
      // console.log(this.$refs[index])
      // 点击切换时 页面重排 input 所以不能立刻获取
      // 当节点渲染完毕会执行$newxTickt
      this.$nextTick(() => {
        // console.log(this.$refs[index])
        // 获取响应的表单实现聚焦
        this.$refs[index].focus()
      })
    },
    // 气泡确认按钮回调函数
    // 目前模板中用到的elmentui 版本问题 请按照版本调取响应事件 这个elemnt框架版本为2.13.2
    deleteAttrValue(index) {
      // console.log(111)
      this.attrInfo.attrValueList.splice(index, 1)
    },
    // 保存按钮 进行添加或修改的按钮
    async addOrUpdateAttr() {
      // alert(111)
      // 整理获取的参数 ,1 去除用户提交的空属性值 2 提交的数据中不应该出现flag filter 会返回一个新数组  将过滤后的赋值回去
      this.attrInfo.attrValueList = this.attrInfo.attrValueList.filter(item => {
        // 过滤属性不为空的
        if (item.valueName !== '') {
          // 删除flag
          delete item.flag
          return true
        }
      })
      try {
        await this.$API.attr.reqAddOrUpdateAttr(this.attrINfo)
        this.isShwoTable = true
        this.$message({
          type: 'success',
          message: '保存成功'
        })
        this.getAttrList()
      } catch (error) {
        this.$message({
          // type: 'success',
          message: error
        })
      }
    }
  }
}
</script>

<style>

</style>

 

posted @ 2023-03-09 13:02  小白字太白  阅读(55)  评论(0编辑  收藏  举报