亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

VueJs自動聚焦在新行上

VueJs自動聚焦在新行上

米琪卡哇伊 2022-07-21 10:05:19
我有動態行,它通過單擊按鈕或掃描輸入成功添加新行,現在的問題是我無法專注于新行。演示https://i.stack.imgur.com/i72Vp.gif代碼為避免混淆,我已對代碼中的所有行進行了注釋。Template<table class="table table-bordered table-striped table-hover">    <thead>        <tr>            <td><strong>Serial Number</strong></td>            <td width="50"></td>        </tr>    </thead>    <tbody>        <tr v-for="(row, index) in form.barcode_serial_number" :key="index">            <td>                <el-input ref="barcode" v-model="row.barcode_serial_number"></el-input>            </td>            <td>                <el-link v-on:click="removeElement(index);" style="cursor: pointer">Remove</el-link>            </td>        </tr>    </tbody></table><div>    <button type="button" class="button btn-primary" @click="addRow">Add row</button></div>Scriptdata() {    return {        form: {            barcode_serial_number: [], //get data of all rows        },    }},created() {    //scanner    const eventBus = this.$barcodeScanner.init(this.onBarcodeScanned, { eventBus: true })    if (eventBus) {        eventBus.$on('start', () => {            this.loading = true;            console.log('start')        })        eventBus.$on('finish', () => {            this.loading = false;            console.log('finished')            // add new row when scan succeed            this.$nextTick(function () {                 this.form.barcode_serial_number.push({                    barcode_serial_number: ''                });            })        })    }},methods: {    // add autofocus to new row    focusInput() {        this.$refs.barcode.focus();    },    // add new row by clicking button    addRow: function() {        var barcodes = document.createElement('tr');        this.form.barcode_serial_number.push({            barcode_serial_number: ''        });    },    // remove row by clicking button    removeElement: function(index) {        this.form.barcode_serial_number.splice(index, 1);    },}問題如何在新添加的行上設置自動對焦?
查看完整描述

1 回答

?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

在新serial_number插入的那一刻,DOM 沒有更新,所以我們不能集中它。


當 DOM 更新時,我們需要使用nextTick來運行一個函數。


Vue.config.devtools = false;

Vue.config.productionTip = false;


var app = new Vue({

  el: '#app',

  data: {

    form: {

      barcode_serial_number: []

    }

  },

  methods: {

    addRow() {

      this.form.barcode_serial_number.push({

        barcode_serial_number: ''

      });

      this.$nextTick(() => {

      const nbBarcodes = this.$refs.barcode.length;

        this.$refs.barcode[nbBarcodes - 1].focus();

      });

    },

    removeElement(index) {

      this.form.barcode_serial_number.splice(index, 1);

    },

  }

})

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">

  <table>

    <thead>

      <tr>

        <td><strong>Serial Number</strong></td>

        <td width="50"></td>

      </tr>

    </thead>

    <tbody>

      <tr v-for="(row, index) in form.barcode_serial_number" :key="index">

        <td>

          <input ref="barcode" v-model="row.barcode_serial_number"></input>

        </td>

        <td>

          <button v-on:click="removeElement(index);">Remove</button>

        </td>

      </tr>

    </tbody>

  </table>

  <div>

    <button @click="addRow">Add row</button>

  </div>

</div>


查看完整回答
反對 回復 2022-07-21
  • 1 回答
  • 0 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號