el-select实现el-option可编辑

鼠标悬浮出现编辑图标

点击编辑图标对选择项进行修改

核心代码如下,注意el-input不要使用@focus,会导致el-select面板收起来;使用@click.native.stop即可

<el-select v-model="value" placeholder="选择" style="width:300px;">
      <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
          @mouseenter.native="selectMouseenter(item)"
          @mouseleave.native="selectMouseleave(item)"
      >
        <div v-if="item.showEditInput">
          <el-input style="width:120px;float: left;" v-model="item.label"
                    @click.native.stop="inputFocus(item, $event)"></el-input>
          <span style="float: right;">
            <el-button type="text" size="small" @click.stop="saveEdit(item, $event)">保存</el-button>
            <el-button type="text" size="small" @click.stop="cancelEdit(item, $event)">取消</el-button>
          </span>
        </div>
        <div v-else>
          <span style="float: left;">{{ item.label }}</span>
          <el-icon style="float: right;margin-top: 10px;" @click.stop="editBtn(item)" v-if="item.showEditIcon">
            <Edit/>
          </el-icon>
        </div>
      </el-option>
    </el-select>

const value = ref('')
const options = ref([
      {
        value: 'Option1',
        label: 'Option1',
        showEditIcon: false,
        showEditInput: false
      },
      {
        value: 'Option2',
        label: 'Option2',
        showEditIcon: false,
        showEditInput: false
      },
      {
        value: 'Option3',
        label: 'Option3',
        showEditIcon: false,
        showEditInput: false
      },
      {
        value: 'Option4',
        label: 'Option4',
        showEditIcon: false,
        showEditInput: false
      },
      {
        value: 'Option5',
        label: 'Option5',
        showEditIcon: false,
        showEditInput: false
      },
    ])

const selectMouseenter = (item) => {
  item.showEditIcon = true
}
const selectMouseleave = (item) => {
  item.showEditIcon = false
}
const editBtn = (data) => {
  for (let item of options.value) {
    item.showEditInput = data.value === item.value;
  }
}
const saveEdit = (item, e) => {
  item.showEditInput = false
}
const cancelEdit = (item, e) => {
  item.showEditInput = false
}
const inputFocus = (item, e) => {
  e.stopPropagation()
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值