Vue.js 1.x 与 Vue.js 2.x 表格渲染和字段焦点

标签 vue.js vuejs2

前言

我有一个允许用户添加行的表,但用户要求将行添加到表的顶部。

问题:** 使用推荐的拼接(添加到数组顶部)添加新行在 vue 1.x 中效果很好,但在 vue 2.x 中添加第一个新行在添加第一行时失败但添加 2 到 n 行效果很好 - 错误 **vue.min.js:6 TypeError: Cannot read property 'focus' of null 似乎告诉我新添加的行为 null,即使 watch 在添加新行时触发。

如果我遗漏了一些内容,请告诉我 - 下面有适用于 vue 1.x 和 vue 2.x 的 fiddle 可以正确说明问题。

Vue.js 1.x

这在 vue.js v1.x 中运行良好。新行已添加,并且光标聚焦在“日期”列上:

fiddle :https://jsfiddle.net/barbedCoil/n2wrq39t/

以下是添加新行的相关代码:

        onAddTipEntry: function (employeeId, employeeJobCode)
        {
            HS.Main.vm.tipAddNew = true;

            // find last row num
            var last_row = 0;
            _(this.tipEntries).forEach(function (entry)
            {
                if (entry.row_num > last_row) last_row = entry.row_num;
            });

            var newEntry = _.cloneDeep(HS.Main.vm.to_raw(HS.Main.vm.tipEntry));
            newEntry.row_num = (last_row + 1);
            newEntry.person_id = employeeId;
            newEntry.job_code = employeeJobCode;
            newEntry.tip_date = moment(this.businessDate).format("YYYY-MM-DD");
            newEntry.status_code = this.status.new_code;

            // we insert into the top of the array instead of bottom
            this.tipEntries.splice(0, 0, newEntry);
        }

下面是“watch”中的代码,它仅将焦点设置在“日期”字段上:

    watch:
    {
        // we just watch to see when new entry by user
        "tipEntries": function (val, oldVal)
        {
            if (this.tipAddNew === false) return;

            // no error side effect even if fails
            //$("#tip-0").focus();

            // uncomment this to see the vue error
            document.getElementById("tip-0").focus();

            HS.Main.vm.tipAddNew = false;
        },
    },

Vue.js 2.x

在 vue.js 2.x 中,相同的代码在添加的第一行上不起作用,但在后续行上运行良好。新行已添加,但光标聚焦在日期列上(请参阅下面发生错误的“watch”代码段)。 **我认为这不是真正的问题,而是副作用

这是控制台中显示的错误:

vue.min.js:6 TypeError: Cannot read property 'focus' of null
at pt.tipEntries (test_vue2.js:72)
at So.run (vue.min.js:7)
at $e (vue.min.js:6)
at Array.<anonymous> (vue.min.js:7)
at e (vue.min.js:7)
at <anonymous>

fiddle :https://jsfiddle.net/barbedCoil/4z6q8arn/2/

这是添加新行的相关代码。 这与 v1.x 代码完全相同:

        onAddTipEntry: function (employeeId, employeeJobCode)
        {
            HS.Main.vm.tipAddNew = true;

            // find last row num
            var last_row = 0;
            _(this.tipEntries).forEach(function (entry)
            {
                if (entry.row_num > last_row) last_row = entry.row_num;
            });

            var newEntry = _.cloneDeep(HS.Main.vm.to_raw(HS.Main.vm.tipEntry));
            newEntry.row_num = (last_row + 1);
            newEntry.person_id = employeeId;
            newEntry.job_code = employeeJobCode;
            newEntry.tip_date = moment(this.businessDate).format("YYYY-MM-DD");
            newEntry.status_code = this.status.new_code;

            // we insert into the top of the array instead of bottom
            this.tipEntries.splice(0, 0, newEntry);
        }

这是“watch”中的代码,它仅将焦点设置在“日期”字段上。 与 v1.x 中的代码相同。请注意,错误来自以下焦点:

    watch:
    {
        // we just watch to see when new entry by user
        "tipEntries": function (val, oldVal)
        {
            if (this.tipAddNew === false) return;

            // no error side effect even if fails
            //$("#tip-0").focus();

            // uncomment this to see the vue error
            document.getElementById("tip-0").focus();

            HS.Main.vm.tipAddNew = false;
        },
    },

最佳答案

Renders in Vue 2 occur asychronously 。当您尝试聚焦该元素时,该元素尚未渲染到屏幕上。相反,将焦点排队以在下一次渲染之后发生。

"tipEntries": function (val, oldVal)
{
  if (this.tipAddNew === false) return;

  this.$nextTick(() => document.getElementById("tip-0").focus())
            
  HS.Main.vm.tipAddNew = false;
},

这是您的fiddle updated .

关于Vue.js 1.x 与 Vue.js 2.x 表格渲染和字段焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45107727/

相关文章:

vue.js - 试图在我的 Vue 组件中访问mounted() 中的状态对象

vue.js - 在 CI 管道中运行开发服务器

javascript - 为什么单击 ag-grid 中的过滤器时行会消失?

vue.js - 嵌套路由未在路由器 View 中呈现

vue.js - 未捕获的类型错误 : Cannot read property 'location' of undefined vue

vuejs2 - Vee验证: how to attach several validation rules after object constructed

javascript - 用路由器链接填充 vue-multiselect

javascript - VueJS v-bind 不适用于 kebab-case

javascript - Vue.js 绑定(bind)对象属性

vue.js - Vue : binding with v-model in custom checkbox component doesn't work