el-select获取id和name

一般选中节点只会返回:value绑定的数据给v-model中,要想获取id和name一并传给后端,需要如下几步:

1、给选择框添加点击事件 @input="selectChangeParent"

2、v-for中多添加一个参数 index

html 复制代码
<el-select v-model="inputForm.projectId" filterable placeholder="请选择"         
           @input="selectChangeParent">
              <el-option
                  v-for="(item, index) in projectInfo"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
</el-select>

3、添加选择框点击事件的方法

html 复制代码
          selectChangeParent(index){
                let listData=[];
                listData = this.projectInfo;
                for (let i = 0; i <listData.length ; i++) {
                    if ( listData[i].id==this.inputForm.projectId){
                        this.inputForm.projectName=listData[i].name
                    }

                }
            },

这是我在data中下拉框的数据结构

html 复制代码
                projectInfo: {
                    id: '',
                    name: ''
                },

总体思路,点击事件触发时获取到点击的id后,再和v-for绑定的数组做对比,对比时projectInfo的id和点击选择的id相同时,给inputForm里的对象赋值

完整代码:

html 复制代码
<%
layout("/include/_vue_body.html", {title:"列表", parent:"隐患处理"}){
%>
<style>
    body {
        background: #ffffff !important;
    }
</style>
<div id="app" class="bg-white">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading"
             label-width="120px">
        <el-row :gutter="15">
            <el-col :span="12">
                <el-form-item label="隐患名称" prop="hiddenName"
                              :rules="[
                  {required: true, message:'隐患名称不能为空', trigger:'blur'}
                 ]">
                    <el-input v-model="inputForm.hiddenName" placeholder="请填写隐患名称" maxlength="25"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="整改措施" prop="hiddenMeasurId"
                              :rules="[
                 ]">
                    <el-select v-model="inputForm.hiddenMeasurId" filterable placeholder="请选择">
                        <el-option
                                v-for="item in hiddenMeasureList"
                                :key="item.id"
                                :label="item.measureName"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="整改负责人" prop="principal"
                              :rules="[
                 ]">
                    <fs-user-select
                            :is_layer="true"
                            :limit="1"
                            :value="inputForm.principal"
                            @get-value="(value) => { inputForm.principal = value}"></fs-user-select>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="整改监督人" prop="supervision"
                              :rules="[
                 ]">
                    <fs-user-select
                            :is_layer="true"
                            :limit="1"
                            :value="inputForm.supervision"
                            @get-value="(value) => { inputForm.supervision = value}"></fs-user-select>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="隐患类型" prop="hiddenType"
                              :rules="[
                 ]">
                    <el-select v-model="hiddenType" @change="selectProjectType">
                        <el-option v-for="item in hiddenTypeList" :key="item.value" :label="item.label"
                                   :value="item.value"></el-option>
                    </el-select>

                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="所属项目" prop="projectId"
                              :rules="[
                 ]">
                    <el-select v-model="inputForm.projectId" filterable placeholder="请选择" @input="selectChangeParent">
                        <el-option
                                v-for="(item, index) in projectInfo"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-col>

            <el-col :span="24">
                <el-form-item label="隐患地点" prop="address"
                              :rules="[
                 ]">
                    <el-input v-model="inputForm.address" placeholder="请填写隐患地点" maxlength="64"
                              show-word-limit></el-input>
                </el-form-item>
            </el-col>

            <el-col :span="24">
                <el-form-item label="隐患详情" prop="hiddenDetail"
                              :rules="[
                 ]">
                    <el-input :rows="6" type="textarea" v-model="inputForm.hiddenDetail" placeholder="请填写隐患详情"
                              maxlength="200" show-word-limit></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="隐患图片" prop="hiddenUrl"
                              :rules="[
                 ]">
                    <fs-upload-imgs v-model="inputForm.hiddenUrl"></fs-upload-imgs>
                </el-form-item>
            </el-col>
        </el-row>
    </el-form>
</div>
<script>
    var formContainer = new Vue({
        el: '#app',
        mixins: [sharpMixin],
        data() {
            return {
                loading: false,
                inputForm: {
                    id: '${hiddenInfo.id}',
                    hiddenName: '',
                    principal: '',
                    supervision: '',
                    hiddenUrl: '',
                    hiddenDetail: '',
                    address: '',
                    projectId: '',
                    hiddenMeasurId: '',
                    projectType: '',
                    projectName:'',
                    hiddenMeasurName: ''
                },
                hiddenType: '',
                hiddenTypeList: [],
                projectInfo: {
                    id: '',
                    name: ''
                },
                hiddenMeasure:{
                    id: '',
                    measureName: '',
                },
                hiddenMeasureList:[]
            }
        },
        created() {
            let that = this
            this.clearForm(function () {
                if (that.inputForm.id != '') {
                    that.loadData()
                }
            });
            this.hiddenTypeList = this.getDictList("hidden_type")
            this.initHiddenMeasure()
        },
        methods: {
            loadData() {
                this.loading = true
                this.get('${ctx}/safety/hiddenInfo/info', {id: this.inputForm.id}).then((res) => {
                    if (res.data) {
                        this.inputForm = res.data
                        this.hiddenType = this.inputForm.projectType
                    }
                    this.initBasicData()
                    this.selectProjectType()
                    this.loading = false
                })
            },
            selectChangeParent(index){
                let listData=[];
                listData = this.projectInfo;
                for (let i = 0; i <listData.length ; i++) {
                    if ( listData[i].id==this.inputForm.projectId){
                        this.inputForm.projectName=listData[i].name
                    }

                }
            },
            initBasicData() {
                let initData = {}
                Object.assign(this.inputForm, initData);
            },
            initHiddenMeasure(){
                this.get('${ctx}/safety/hiddenMeasure/hiddenList').then((res) => {
                    if (res.data) {
                        this.hiddenMeasureList = res.data
                    }
                })
            },
            initProjectData(type) {
                this.get('${ctx}/safety/hiddenInfo/findProjectInfo?type=' + type).then((res) => {
                    if (res.data) {
                        this.projectInfo = res.data
                    }
                })
            },
            initBuildData(type) {
                this.get('${ctx}/safety/hiddenInfo/findProjectInfo?type=' + type).then((res) => {
                    if (res.data) {
                        this.projectInfo = res.data
                    }
                })
            },
            initRoomData(type) {
                this.get('${ctx}/safety/hiddenInfo/findProjectInfo?type=' + type).then((res) => {
                    if (res.data) {
                        this.projectInfo = res.data
                    }
                })
            },
            selectProjectType() {
                this.inputForm.projectType = this.hiddenType
                let type = this.inputForm.projectType
                if (this.hiddenType == 1) {
                    //hiddenType为1时查询在建项目信息
                    this.projectInfo = ''
                    this.initProjectData(type)
                } else if (this.hiddenType == 2) {
                    //hiddenType为2时查询建筑物信息
                    this.projectInfo = ''
                    this.initBuildData(type)
                } else {
                    //hiddenType为3时查询房屋信息
                    this.projectInfo = ''
                    this.initRoomData(type)
                }

            },
            clearForm(call) {
                let that = this
                setTimeout(function () {
                    that.$refs['inputForm'].resetFields();
                    if (call) {
                        call();
                    }
                }, 300)
            },
            // 提交
            doSubmit(call) {
                this.$refs['inputForm'].validate((valid) => {
                    if (valid) {

                        this.post('${ctx}/safety/hiddenInfo/save', this.inputForm).then((res) => {
                            if (res.success) {
                                this.$message.success(res.msg)
                                call()
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                    } else {
                        return false;
                    }
                })
            }
        }
    })
</script>
<% } %>
相关推荐
也无晴也无风雨1 小时前
深入剖析输入URL按下回车,浏览器做了什么
前端·后端·计算机网络
Martin -Tang2 小时前
Vue 3 中,ref 和 reactive的区别
前端·javascript·vue.js
FakeOccupational3 小时前
nodejs 020: React语法规则 props和state
前端·javascript·react.js
放逐者-保持本心,方可放逐4 小时前
react 组件应用
开发语言·前端·javascript·react.js·前端框架
曹天骄5 小时前
next中服务端组件共享接口数据
前端·javascript·react.js
阮少年、5 小时前
java后台生成模拟聊天截图并返回给前端
java·开发语言·前端
郝晨妤6 小时前
鸿蒙ArkTS和TS有什么区别?
前端·javascript·typescript·鸿蒙
AvatarGiser7 小时前
《ElementPlus 与 ElementUI 差异集合》Icon 图标 More 差异说明
前端·vue.js·elementui
喝旺仔la7 小时前
vue的样式知识点
前端·javascript·vue.js
别忘了微笑_cuicui7 小时前
elementUI中2个日期组件实现开始时间、结束时间(禁用日期面板、控制开始时间不能超过结束时间的时分秒)实现方案
前端·javascript·elementui