【问题标题】:Pass the target element through @change in Vuetify combo box通过 Vuetify 组合框中的 @change 传递目标元素
【发布时间】:2019-12-28 23:42:46
【问题描述】:

我需要通过updateTags方法传递目标事件。这是下面的组合框:

当我调用comboActive 方法时,我可以获得目标事件。

KeyboardEvent {isTrusted: true, key: "y", code: "KeyY", location: 0, ctrlKey: false, …}

注意,组合框中的comboActive 方法不发送任何参数,但在comboActive(event) 方法中我可以得到目标事件。

我希望能够在updateTags 方法中获取目标事件。如您所见,我尝试过使用$event,但这不起作用

HTML:

<v-combobox multiple
  v-model="select[i]"
  append-icon
  small-chips
  deletable-chips
  @keyup="comboActive"
  @paste="updateTags(item,i)"
  @change="updateTags(item,i,$event)">
</v-combobox>

脚本:

comboActive(event) {
  console.log('active ', event)
  event.target.parentElement.classList.add('saving')
},
updateTags(item, i, e) {
  this.$nextTick(() => {
    this.$nextTick(() => {
      console.log('complete ', item, e)
    })
  })
},

当我添加 $event@change="updateTags(item,i,$event)" 时,我会返回项目数组。我需要组合框本身,以便删除在 comboActive 方法期间添加的类。

【问题讨论】:

  • 您需要用结果创建一个新的组合框吗?对不起,我不明白这个问题
  • @ManuelRodriguezGil 我正在用更多信息更新问题:)
  • 好吧,我明白了,你只需要使用e.target
  • 我尝试了同样的问题here,它只输出选定的项目,如果你想使用 CSS 尝试类绑定而不是以这种方式访问​​元素
  • @BoussadjraBrahim 我很想通过类绑定来做到这一点,但我想不通。我的目标是当用户输入一个单词时,组合框的边框变成红色。保存新项目后,红色边框消失。

标签: javascript vue.js vuejs2 vuetify.js


【解决方案1】:

我建议使用class binding 来处理这个问题,或者使用color 计算属性并通过添加一个名为saving 的数据属性来有条件地更改它,并在@change 处理程序中更新它,例如:

   <v-combobox
             :color="color"
         ...
         @change="saving=true"
        ></v-combobox>

脚本

 data () {
      return {

        saving:false,
        select: ['Vuetify', 'Programming'],
        items: [
          'Programming',
          'Design',
          'Vue',
          'Vuetify',
        ],
      }
    },
  computed:{
    color(){
      return !this.saving? 'red':'grey'
    }
  },

full example

【讨论】:

    【解决方案2】:

    使用e.target 更改输入。

    comboActive(event) {
            console.log('active ', event)
            event.target.parentElement.classList.add('saving')
        },
    
    updateTags(item, i, e) {
                this.$nextTick(() => {
                    this.$nextTick(() => {
                        console.log('complete ', item, e);
                        console.log(e.target);
                        e.target.parentElement.classList.remove('saving');
                    });
                });
            },
    

    这只适用于简单的组件。我错了。

    您可以尝试的另一种方法是使用相同的index 设置Array,并且当您触发comboActive(item, i) 和updateTags(item, i) 时将此数组切换为true || false

    comboActive(i, event) {
        console.log('active ', event)
        this.isActive[i] = true;
    },
    
    updateTags(item, i) {
        this.$nextTick(() => {
            this.$nextTick(() => {
                this.isActive[i] = false;
            });
        });
    },
    

    【讨论】:

    • 不幸的是返回未定义
    • 手动,这里是e 吐出的内容:(2) ["jason", "hi", __ob__: Observer] 在这个例子中,我在组合框中输入了 jason 和 hi。
    • 不知道为什么你不能访问$event。我正在尝试小提琴,没​​有麻烦。
    • 组合框在循环内。不确定这是否有帮助
    • 我忘记了组合框的部分,对不起,请稍等
    猜你喜欢
    • 2012-10-17
    • 2016-04-09
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 2021-03-13
    • 2011-12-17
    • 1970-01-01
    相关资源
    最近更新 更多