当前位置: 首页 > article >正文

elementui Cascader 级联选择器的使用总结

实现效果

 技术要点总结如下:

 1、点击添加自动增加多行,实现自主选择增加多条节点数据

 2、节点地址使用的是Cascader 级联选择器,需要动态生成,涉及到一个技术要点是:因v-modal只能获取value不能获取label,故需要解决在多个动态生成的Cascader 分别获取他们选中的label和value,


 

下面开始展示相关代码:

html:

 <el-dialog :title="title" :visible.sync="open" width="1300px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddPlatCarLineDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeletePlatCarLineDetail">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="platCarLineDetailList" :row-class-name="rowPlatCarLineDetailIndex" @selection-change="handlePlatCarLineDetailSelectionChange" ref="platCarLineDetail">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="节点名称" prop="nodeName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.nodeName" placeholder="请输入节点名称" />
            </template>
          </el-table-column>
          <el-table-column label="节点地址" prop="nodeAddress" width="250" >
            <template slot-scope="scope">
              <el-cascader
                v-model="valueNode[scope.$index]"
                :options="optionsNodeSource"
                :props="{value:'id',label:'name'}"
                :ref="'cascader'+scope.$index"
                @change="handleChange(scope.$index)"></el-cascader>
            </template>
          </el-table-column> 
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
js:

handleChange(index){
  var that=this
  var name='cascader'+index
//this.valueNode[index] 对应的是省市区的id  nameArr对应的是省市区的name
  var nameArr=this.$refs[name].getCheckedNodes()[0].pathLabels
  that.$nextTick(function () {
    that.platCarLineDetailList[index].nodeAddress=nameArr
    console.log(that.platCarLineDetailList,that.valueNode)
    that.$forceUpdate()
  })
},
/** 车线详情添加按钮操作 */
handleAddPlatCarLineDetail() {
  var that=this
  that.$nextTick(function () {
    let obj = {};
    obj.nodeName = "";
    obj.nodeAddress = ""; 
    that.valueNode.push([])//在data中定义格式为[[]]
    that.platCarLineDetailList.push(obj); 
    that.$forceUpdate()
  })
},
/** 车线详情删除按钮操作 */
handleDeletePlatCarLineDetail() {
  if (this.checkedPlatCarLineDetail.length == 0) {
    this.$modal.msgError("请先选择要删除的车线详情数据");
  } else {
    const platCarLineDetailList = this.platCarLineDetailList;
    const valueNode = this.valueNode;
    const checkedPlatCarLineDetail = this.checkedPlatCarLineDetail;
    this.platCarLineDetailList = platCarLineDetailList.filter(function(item) {
      return checkedPlatCarLineDetail.indexOf(item.index) == -1
    });
    this.valueNode = valueNode.filter(function(item,indexV) {
      var num=indexV+1
      return checkedPlatCarLineDetail.indexOf(num) == -1
    });
 
  }
},

 代码解释


                

http://www.kler.cn/a/300193.html

相关文章:

  • 你真的懂吗系列——串口通信
  • uniapp ios sticky定位,内部 u-tabs(包含scroll-view)消失问题
  • 【QT】基础入门学习
  • 如何使用elementui实现一个根据页面进度实时增长/前进的进度条
  • DBA实战手记,技术书的黑神话
  • Codeforces Round 971 (Div. 4)——C题题解
  • 最直接显示 ubuntu 版本号的命令
  • 6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)
  • idea向git上推送被拒绝 push to master was rejected
  • 代码随想录27期|Python|Day52|​动态规划|​647. 回文子串|516. 最长回文子序列
  • react js 路由 Router
  • 类和对象(中)
  • 哈喽GPT-4o,现代程序员提高编码能力的正确打开方式
  • NVIDIA AI Workbench 让 Windows 上的 GPU 使用更加简便
  • 基于Spring搭建SpringMvc框架
  • pptpd配置文件/etc/pptpd.conf详解
  • 数据库中的主键和外键分别是什么意思?
  • Leetcode面试经典150题-207.课程表
  • 【代码随想录训练营第42期 Day56打卡 - 图论Part6 - 并查集2 - 冗余连接问题
  • Debug-027-el-tooltip组件的使用及注意事项