【问题标题】:Click-and-edit text input with Vue使用 Vue 单击并编辑文本输入
【发布时间】:2018-07-24 16:35:31
【问题描述】:

我正在寻找一个点击编辑的 Vue 组件。

我找到了a fiddle 并进行了一些修改。它的工作原理是这样的:

The fiddle is here.

问题:我需要再次点击才能使输入成为焦点。如何让它自动聚焦?

小提琴的代码。 HTML:

<div id="app">
Click the values to edit!
  <ul class="todo-list">
    <li v-for = "todo in todos">
      <input v-if = "todo.edit" v-model = "todo.title"
      @blur= "todo.edit = false; $emit('update')"
      @keyup.enter = "todo.edit=false; $emit('update')">
            <div v-else>
        <label @click = "todo.edit = true;"> {{todo.title}} </label>
      </div>
    </li>
  </ul>


</div>

JS:

new Vue({
  el: '#app',
  data: {
    todos: [{'title':'one value','edit':false},
                  {'title':'one value','edit':false},
                    {'title':'otro titulo','edit':false}],
    editedTodo: null,
    message: 'Hello Vue.js!'
  },
  methods: {
    editTodo: function(todo) {
      this.editedTodo = todo;
    },
  }

})

【问题讨论】:

    标签: javascript vue.js


    【解决方案1】:

    @AitorDB 的帮助下,我为此编写了一个 Vue 组件,我称之为Click-to-Edit。已经可以使用了,所以我把它贴出来。

    它的作用:

    • 支持v-model
    • 在单击其他位置和按 Enter 时保存更改

    ClickToEdit.vue: (vue 2.x)

    <template>
      <div>
        <input type="text"
               v-if="edit"
               :value="valueLocal"
               @blur.native="valueLocal = $event.target.value; edit = false; $emit('input', valueLocal);"
               @keyup.enter.native="valueLocal = $event.target.value; edit = false; $emit('input', valueLocal);"
               v-focus=""
                 />
            <p v-else="" @click="edit = true;">
              {{valueLocal}}
            </p>
        </div>
    </template>
    
    <script>
      export default {
      
      props: ['value'],
      
      data () {
      return {
          edit: false,
          valueLocal: this.value
        }
      },
      
      watch: {
        value: function() {
          this.valueLocal = this.value;
        }
      },
      
      directives: {
        focus: {
          inserted (el) {
            el.focus()
          }
        }
      }
      
    }
    </script>
    

    编辑 3.x: [2.x 和 3.x 之间的重大变化]

    • 从事件处理程序中删除 .native
    • focus 挂钩更改为mounted,如Custom Directives 3.x 中所述。

    【讨论】:

    • 我试过你写的组件,我得到了像errors这样的错误所以我删除了.native部分modifications,它现在可以工作了。我不确定 '.native' 是做什么的,但是在我删除它之后它就可以工作了,哈哈。
    • 很棒的小组件!非常感谢。跟进 HelloBill 的评论。我正在使用 Vue2 并且还必须删除 .native 才能响应输入或模糊事件。但在这样做之后,它就像一个魅力。
    【解决方案2】:

    你可以使用指令,例如

    JS

    new Vue({
      el: '#app',
      data: {
        todos: [
          { title: 'one value', edit: false },
          { title: 'one value', edit: false },
          { title: 'otro titulo', edit: false }
        ],
        editedTodo: null,
        message: 'Hello Vue.js!'
      },
      methods: {
        editTodo: function (todo) {
          this.editedTodo = todo
        }
      },
      directives: {
        focus: {
          inserted (el) {
            el.focus()
          }
        }
      }
    })
    

    HTML

    <div id="app">
        Click the values to edit!
        <ul class="todo-list">
            <li v-for="todo in todos">
                <input
                    v-if="todo.edit"
                    v-model="todo.title"
                    @blur="todo.edit = false; $emit('update')"
                    @keyup.enter="todo.edit=false; $emit('update')"
                    v-focus
                >
                <div v-else>
                    <label @click="todo.edit = true;"> {{todo.title}} </label>
                </div>
            </li>
        </ul>
    </div>
    

    您可以在此处找到更多信息 https://vuejs.org/v2/guide/custom-directive.html

    【讨论】:

      【解决方案3】:

      以@Masen Furer 的工作为基础。当用户删除所有数据时,我添加了一些保护来处理。可能有一种方法可以使用“更新”来完成此操作,但我无法使其正常工作。 我还添加了点击逃脱并放弃任何更改的能力。

      <template>
        <span>
          <input type="text"
                 v-if="edit"
                 :value="valueLocal"
                 @blur="save($event);"
                 @keyup.enter="save($event);"
                 @keyup.esc="esc($event);"
                 v-focus=""/>
              <span v-else @click="edit = true;">
                {{valueLocal}}
              </span>
          </span>
      </template>
      
      <script>
        export default {
        
        props: ['value'],
        
        data () {
        return {
            edit: false,
            valueLocal: this.value,
            oldValue: (' ' + this.value).slice(1)
          }
        },
        methods: {
            save(event){
              if(event.target.value){             
                  this.valueLocal = event.target.value; 
                  this.edit = false; 
                  this.$emit('input', this.valueLocal);
              }
            },
            esc(event){
                this.valueLocal = this.oldValue; 
                event.target.value = this.oldValue;
                this.edit = false; 
                this.$emit('input', this.valueLocal);
            }
        },
        watch: {
          value: function() {
            this.valueLocal = this.value;
          }
        },
        
        directives: {
          focus: {
              inserted (el) {
                  el.focus()
              }
          }
        }
        
      }
      </script>

      【讨论】:

        猜你喜欢
        • 2017-12-16
        • 2012-04-29
        • 2013-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-25
        • 1970-01-01
        相关资源
        最近更新 更多