转载@ https://www.cnblogs.com/guozongzhang/p/10884621.html

前段碎片 专栏收录该内容
4 篇文章 0 订阅

项目中一个配置列表中有编辑功能; 自己用div写了一下;取值与绑定比较不方便;就借鉴一下;

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <!-- import CSS -->
    <link
      rel="stylesheet"
      href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"
    />
  </head>
  <body>
    <div id="app">
      <el-button type="primary" @click="addListItem()" size="mini"
        >添加</el-button
      >
      <el-button type="success" @click="saveList('form')" size="mini">保存</el-button>
      <el-form :model="model" ref="form" size="small">
        <el-table :data="model.list" style="width: 100%;" row-key="key">
          <el-table-column
            v-for="item in tableHeadData"
            :key="item.key"
            :label="item.la<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <!-- import CSS -->
    <link
      rel="stylesheet"
      href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"
    />
  </head>
  <body>
    <div id="app">
      <el-button type="primary" @click="addListItem()" size="mini"
        >添加</el-button
      >
      <el-button type="success" @click="saveList('form')" size="mini">保存</el-button>
      <el-form :model="model" ref="form" size="small">
        <el-table :data="model.list" style="width: 100%;" row-key="key">
          <el-table-column
            v-for="item in tableHeadData"
            :key="item.key"
            :label="item.label"
            :width="item.width"
          >
            <template slot-scope="scope">
              <el-form-item
                :prop="'list.' + scope.$index + '.'+item.name"
              >
                <!-- 判断是展示列表还是新增
                判断编辑状态下是input还是select -->
                <span v-if="!scope.row.isEdit && isSelect.indexOf(item.name) < 0">{{ scope.row[item.name] }}</span>
                <el-input
                  v-model="scope.row[item.name]"
                  v-if="scope.row.isEdit && isSelect.indexOf(item.name) < 0"
                ></el-input>
                <span v-if="!scope.row.isEdit && isSelect.indexOf(item.name) >-1">{{ scope.row[item.name+'List'][0].label}}</span>
                <el-select 
                  v-model="scope.row[item.name]"
                 v-if="scope.row.isEdit && isSelect.indexOf(item.name) > -1"
                  :disabled="!scope.row.isEdit"
                >
                  <el-option
                    v-for="childItem in scope.row[item.name+'List']"
                    :key="childItem.value"
                    :label="childItem.label"
                    :value="childItem.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
        </el-table>
        <!-- </el-form-item> -->
      </el-form>
    </div>
  </body>
  <!-- import Vue before Element -->
  <script src="https://unpkg.com/vue/dist/vue.js"></script>
  <!-- import JavaScript -->
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  <script>
    new Vue({
      el: '#app',
      data: function() {
        return {
          // list数据
          model: {
            list: []
          },
          // 表头字段
          tableHeadData: [
            {
              name: 'warehouse',
              width: 160,
              label: 'Warehouse'
            },
            {
              name: 'description',
              width: 160,
              label: 'Description'
            },
            {
              name: 'warehouseType',
              width: 160,
              label: 'Warehouse Type'
            }
          ],
          // 下拉选项判断条件
          isSelect: ['warehouseType'],
          // 校验规则
        }
      },
      mounted() {
        // 模拟接口请求赋值
        this.model.list = [{
          warehouse: '345',
          description: '345',
          warehouseTypeList: [
            { label: '内部仓库', value: '1' },
            { label: '外部仓库', value: '2' }
          ]
        }]
      },
      methods: {
        // 添加list
        addListItem() {
          let addItem = {}
          this.tableHeadData.forEach(item => {
            addItem[item.name] = ''
            addItem['isEdit'] = true
            addItem.key = Date.now()
            addItem['warehouseTypeList'] = [
              { label: '内部仓库', value: '1' },
              { label: '外部仓库', value: '2' }
            ]
          })
          this.model.list.unshift(addItem)
        },
        // 保存
        saveList(formName) {
          this.$refs[formName].validate(valid => {
            if (valid) {
              console.log('success')
            } else {
              console.log('error submit!!')
              return false
            }
          })
 
        }
      }
    })
  </script>
</html>
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值