下拉新增选项

<template>
  <div>
    <el-form :model="setTablePointKey" label-width="auto" label-position="left">
      <div>
        <div style="font-weight: 700;color:#606266;">下拉数据</div>
        <el-row :gutter="10" style="font-size:12px;font-weight: 700;color:#606266;">
          <el-col :span="8" class="center">列值</el-col>
          <el-col :span="8" class="center">列名</el-col>
          <el-col :span="8" class="center">
            <el-button type="primary" size="small" @click="addSetTablePointKey">新增</el-button>
          </el-col>
        </el-row>
        <el-row :gutter="10"  v-for="item,index in setTablePointKey.tablePointKeyList" :key="index" style="padding:4px 0;">
          <el-col :span="8" class="center">
            <el-select v-model="item.pointKey"  placeholder="请选择"  @click="handleClickSetTablePointKey('pointName',item.pointName)" @change="handleSelectSetTablePointKey(item.id,item.pointKey)">
              <el-option
                v-for="item in YKDropdownList"
                :key="item.pointKey"
                :label="item.pointKey"
                :value="item.pointKey"
              />
            </el-select>
          </el-col>
          <el-col :span="8" class="center">
            <el-input size="small" v-model="item.pointName" @change="handleSetTablePointKey(item.id,'pointName',item.pointName,item)"/>
          </el-col>
          <el-col :span="8" class="center">
            <el-button type="danger" size="small" @click="deleteSetTablePointKey(item)">删除</el-button>
          </el-col>
        </el-row>
      </div>
    </el-form>
  </div>
</template>

<script setup>

const setTablePointKey = ref({
  tablePointKeyList:[
    {id:1,pointName:'都的读1', pointKey:'130_xnjd_YC_1_zkg'},
    {id:2,pointName:'都的读2', pointKey:'130_xnjd_YC_1_zmxtqd'},
  ]
})
const YKDropdownList=ref([
  {pointKey:'130_xnjd_YC_1_zkg',pointName:'都的读1'},
  {pointKey:'130_xnjd_YC_1_zmxtqd',pointName:'都的读2'},
  {pointKey:'130_xnjd_YC_1_kqysxtqd',pointName:'都的读13'},
])
const addSetTablePointKey = () => {
  const maxId = Math.max(...setTablePointKey.value.tablePointKeyList.map(obj => obj.id)); 
  setTablePointKey.value.tablePointKeyList.push({id:maxId=='-Infinity'?1:maxId+1, pointName:'新列', pointKey:'pointKey'},)
}
const deleteSetTablePointKey = (v) => {
  console.log('deleteSetTablePointKey,',setTablePointKey.value,v);
  setTablePointKey.value.tablePointKeyList = setTablePointKey.value.tablePointKeyList.filter(item => item.id !== v.id);
}
const handleClickSetTablePointKey = (label,value) => {
  // console.log(label,value);
}



const handleSelectSetTablePointKey = (id,v) => {
  const YKDropdownItem=YKDropdownList.value.find(item=>item.pointKey===v)
  const tablePointKeyItem=setTablePointKey.value.tablePointKeyList.find(item=>item.id===id)
  tablePointKeyItem.pointName=YKDropdownItem.pointName

}
</script>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,Vue下框可以实现下选择并新增选项的功能。通常可以通过在下框中添加一个“其他”选项,当用户选择“其他”选项时,弹出一个输入框供用户填写新的选项。具体实现方式可以参考以下步骤: 1. 在下框的选项列表中添加一个“其他”选项。 ``` <select v-model="selectedOption"> <option v-for="option in options" :value="option.value">{{ option.label }}</option> <option value="other">其他</option> </select> ``` 2. 监听下框的值变化,当选择了“其他”选项时,弹出输入框。 ``` <select v-model="selectedOption" @change="onSelectChange"> <option v-for="option in options" :value="option.value">{{ option.label }}</option> <option value="other">其他</option> </select> ... methods: { onSelectChange() { if (this.selectedOption === 'other') { this.showInput = true; } else { this.showInput = false; } } } ``` 3. 根据输入框的值,动态添加新的选项。 ``` <select v-model="selectedOption" @change="onSelectChange"> <option v-for="option in options" :value="option.value">{{ option.label }}</option> <option value="other">其他</option> </select> ... <div v-if="showInput"> <input type="text" v-model="newOption" @blur="addNewOption" /> </div> ... methods: { onSelectChange() { if (this.selectedOption === 'other') { this.showInput = true; } else { this.showInput = false; } }, addNewOption() { if (this.newOption.trim() !== '') { const newOption = { label: this.newOption, value: this.newOption }; this.options.push(newOption); this.selectedOption = newOption.value; } this.showInput = false; this.newOption = ''; } } ``` 以上是一个简单的示例,具体实现方式可以根据项目需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值