el-table行移动下一行上一行

1、html部分

<el-table
  :data="tableData"
  :fit="true"
  :cell-style="cellStyle"
  border
  class="ccui-multifunctional-table"
  @header-dragend="tableTitleWidth"
  @row-click="handleRowClick"
  @selection-change="handleSelectionChange"
>
  <el-table-column
    label="序号"
    align="center"
    :width="$TW.sn"
    fixed
  >
    <template slot-scope="scope">
      <span>{{
        (pageNoCode - 1) * pageSizeCode + scope.$index + 1
      }}</span>
    </template>
  </el-table-column>
  <!-- <el-table-column type="selection" width="55" align="center" /> -->
  <template v-for="(table, index) in tableTheadOptions">
    <el-table-column
      :fixed="table.fixed || false"
      :key="index"
      :type="table.type"
      :label="table.label"
      :min-width="table.width"
      :prop="table.prop"
      :align="table.align || 'left'"
      :header-align="table.headerAlign || 'left'"
      show-overflow-tooltip
    >
      <template slot-scope="scope">
        <div v-if="table.prop == 'projectStageDict'">
          {{
            findNameByCode(
              scope.row.projectStageDict,
              projectStageDict
            )
          }}
        </div>
        <div v-else-if="table.prop == 'sourceIncomeCodeDict'">
          {{
            findNameByCode(
              scope.row.sourceIncomeCodeDict,
              projectStageDict
            )
          }}
        </div>
        <div v-else-if="table.prop == 'operation'">
            <!-- 主要代码 -->
          <el-button
            @click.native.prevent="
              deleteRow(scope.$index, scope.row, 'up')
            "
            type="text"
            size="small"
            >上移</el-button
          >
          <el-button
            @click.native.prevent="
              deleteRow(scope.$index, scope.row, 'down')
            "
            type="text"
            size="small"
            >下移</el-button
          >
        </div>
        <div v-else>{{ scope.row[table.prop] }}</div>
      </template>
    </el-table-column>
  </template>
</el-table>

2、javaScript部分

methods: {    
    // 上移下移
    deleteRow(index, e, type) {
      if (type === "up") {
        if (index === 0) {
          return this.$message.warning("已经是第一条,不可上移");
        }
        // 在上一项插入该项
        this.tableData.splice(index - 1, 0, this.tableData[index]);
        // 删除后一项
        this.tableData.splice(index + 1, 1);
        this.getmoveUpAndMoveDown(e, 0);
      } else if (type === "down") {
        if (index === this.tableData.length - 1) {
          return this.$message.warning("已经是最后一条,不可下移");
        }
        // 在下一项插入该项
        this.tableData.splice(index + 2, 0, this.tableData[index]);
        // 删除前一项
        this.tableData.splice(index, 1);
        this.getmoveUpAndMoveDown(e, 1);
      }
    },

    // 请求接口
    getmoveUpAndMoveDown(row, status) {
        // 需要的参数
      const params = {
        sn: row.sn,
        id: row.id,
        reserve1: row.reserve1,
        moveUpAndDown: status
      };
      moveUpAndMoveDown(params).then(res => {
        if (res.status === 200) {
          let { code, message } = res.data;
          if (code === "0") {
            // 请求列表接口 刷新列表
            this.queryList({});
          } else {
            this.$message.error(message);
          }
        }
      });
    },
},

### 解决 Element UI `el-table` 中上下输入框移动问题 当处理 `el-table` 内部嵌套有表单控件(如输入框),并希望实现这些控件能够随着表格的切换而平滑过渡时,可以采取以下几种策略来优化用户体验: #### 方法一:固定高度与布局调整 通过设置 `.el-table__row` 的样式属性,确保每一行的高度保持一致,并且内部元素不会因为数据变化而导致重新渲染引起的跳动。 ```css .el-table .el-table__row { height: 48px; } ``` 为了防止因内容长度不均造成的视觉抖动,建议给所有可能变动大小的内容设定固定的宽度或最大最小尺寸[^1]。 #### 方法二:禁用虚拟滚动 如果启用了虚拟列表功能,在某些情况下可能会引起编辑器位置错乱。可以通过关闭此特性来解决问题: ```javascript <template> <el-table :data="tableData" row-key="id" lazy load=false> <!-- 表格列定义 --> </el-table> </template> ``` 注意这里设置了 `lazy` 和 `load` 属性为 false 来强制禁用懒加载机制[^2]。 #### 方法三:增强事件监听逻辑 对于包含可编辑字段的情况,应该加强针对键盘导航的支持,比如按方向键可以在不同单元格间顺畅转移焦点而不触发页面重绘为。这通常涉及到自定义指令或者混合使用 Vue.js 提供的相关 API 实现更精细的操作控制[^3]。 ```html <!-- HTML结构示例 --> <el-table-column prop="name"> <template slot-scope="{ row }"> <el-input ref="inputRef" @keyup.enter.native="$refs.inputRef.focus()" /> </template> </el-table-column> ``` 上述代码片段展示了如何利用原生 DOM 事件处理器配合 `$ref` 访问子组件实例的方法,从而达到更好的交互效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值