element ui中表格输入框回车跳到另一输入框

纯输入框的回车

在这里插入图片描述
组件代码

<template>
  <div>
    <el-button @click="add">新增</el-button>
    <el-button @click="save">保存</el-button>
    <el-form id="table-form" ref="form" :rules="rules" :model="form">
      <el-table ref="table" :data="form.tableData" border>
        <el-table-column>
          <template slot="header">
            <p>
              姓名
              <span style="color:red;font-size:16px;">*</span>
            </p>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop=" 'tableData.' + scope.$index + '.name' " :rules="rules.name">
              <el-input v-model="scope.row.name" @keyup.native.enter="nextFocus($event,scope.row)"></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column>
          <template slot="header">
            <p>
              年龄
              <span style="color:red;font-size:16px;">*</span>
            </p>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop=" 'tableData.' + scope.$index + '.age' " :rules="rules.age">
              <el-input
                v-model.number="scope.row.age"
                type="number"
                @keyup.native.enter="nextFocus($event,scope.row)"
              ></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="身高">
          <template slot-scope="scope">
            <el-form-item>
              <el-input
                v-model.number="scope.row.height"
                type="number"
                @keyup.native.enter="nextFocus($event,scope.row)"
              ></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column label="年龄+身高" prop="other" />
      </el-table>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inputDoms: "",
      tableIndex: 0,
      form: {
        tableData: []
      },
      rules: {
        name: [{ required: true, message: "名字不能为空", trigger: "blur" }],
        age: [{ required: true, message: "年龄不能为空", trigger: "blur" }]
      }
    };
  },
  watch: {
    "form.tableData": {
      handler(val) {
        val.forEach(item => {
          item.other = item.age + item.height;
        });
      },
      deep: true
    }
  },
  created() {
    this.$nextTick(() => {
      this.initInputDOM();
    });
  },
  methods: {
    initInputDOM() {
    //获取id为table-form下的所有input 框
      const inputDoms = document.querySelectorAll(
        "#table-form .el-input__inner"
      );
      //遍历这个input框给他们一个标识
      inputDoms.forEach((item, index) => {
        item.setAttribute("data-index", index);
      });
      this.inputDoms = inputDoms;
    },
    //回车事件
    nextFocus(event, row) {
      const index = event.target.getAttribute("data-index");
      const nextIndex = parseInt(index) + 1;
      const length = this.inputDoms.length;
      if (nextIndex < length) {
        this.inputDoms[nextIndex].focus();
      } else {
        this.inputDoms[0].focus();
      }
    },
    add() {
      this.form.tableData.push({
        name: "",
        age: "",
        height: "",
        other: ""
      });
      this.$nextTick(() => {
        this.initInputDOM();
      });
    },
    del(index) {
      this.form.tableData.splice(index, 1);
    },
    save() {
      this.$refs.form.validate(valid => {
        if (valid) {
          console.log("通过");
          console.log(this.form.tableData);
        }
      });
    }
  }
};
</script>


补充一点:

如果表格在弹出框的时候 那需要在控制弹出按钮的事件中添加先获取表格的输入框 事件
this.$nextTick(() => {
this.initInputDOM();
});

 addClick() {
     this.diaLogTitle = "新增";
      this.$nextTick(() => {
        this.initInputDOM();
      });
      
    },

注意

如果包含下拉框的table,可以使用自定义指令来实现,

在这里插入图片描述

封装自定义指令

const datas ={}
const testdisabled = (parasmt,tmp,dataArr)=>{
        const data = datas[parasmt.id];
        tmp.i++ 
        if(parasmt.Fn && data.length -1 === parasmt.index){
    
            parasmt.Fn(parasmt.index)
            //下一行的第一个聚焦 宏线程触发
            setTimeout(() => {
                const name =  Object.keys(data[data.length - 1])[0]
                data[data.length - 1][name].querySelector('input').focus();
                return;
            },0);
        }else if(parasmt.Fn && data.length -1 !== parasmt.index){
            const elm = data[parasmt.index + 1][dataArr[0]] && data[parasmt.index +1][dataArr[0]].querySelector('input')
            elm.focus()
            elm.select() //选中
        }else{
            const elm = data[parasmt.index][dataArr[tmp.i]] && data[parasmt.index][dataArr[tmp.i]].querySelector('input')
            if(elm.attributes.disabled){ 
                //碰到禁用就回调重新开始
                testdisabled(parasmt,tmp,dataArr)
            }else{
                // const Fn = ()=>{
                    elm.select ? elm.select() : elm.focus()
                // }
                // setTimeout(() => {
                //     if(parasmt.cellback && typeof parasmt.cellback.cb !='function'){
                //         Promise.resolve().then(Fn)
                        
                //     }else if(parasmt.cellback && typeof parasmt.cellback.cb =='function'){
                //         return
                //     }else{
                //         Promise.resolve().then(Fn)
                //     }
                // }, 0);
            }
        }
        
    }
    //回车事件
Vue.directive('keyupEnter', {
    inserted: function (el) {
        const parasmt = arguments[1].value;
        if(!datas[parasmt.id]){datas[parasmt.id]=[]}
        if(!datas[parasmt.id][parasmt.index])datas[parasmt.id].push({})
        datas[parasmt.id][parasmt.index][parasmt.name] = el;
        el.addEventListener('keyup',(event)=>{
            if(event.key=="Enter"){
                const datasArr = Object.keys(datas[parasmt.id][parasmt.index])
                datasArr.forEach((item,index) =>{
                    if(item == parasmt.name){
                        const tmp ={i:index}
                        testdisabled(parasmt,tmp,datasArr)
                    }
                })
            }
        },false)
    },
    unbind:function(){
        datas[arguments[1].value.id].splice(arguments[1].value.index)
    }
})

组件中使用 每一列都要加 且在最后一行后需要增加一个新增下一行的事件

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值