前端element-ui实现表格调换位置

在这里插入图片描述
在这里插入图片描述

      <el-table
        border
        v-loading
        :data="columnList"
        ref="columnTable"
        max-height="400px"
      >
        <el-table-column
          type="selection"
          align="center"
          width="55"
        ></el-table-column>
        <el-table-column
          label="Columns Name"
          prop="label"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column label="Operation" align="center">
          <template slot-scope="scope">
            <div class="flex itemsCenter flexCenter">
             //向上按钮
              <i
                class="el-icon-upload2 pointer"
                v-if="scope.$index !== 0"
                @click="upHandle(scope.$index)"
                :style="{
                  'margin-right':
                    scope.$index !== columnList.length - 1 ? '10px' : 0,
                  'font-size': '16px',
                }"
              ></i>
              //向下按钮
              <i
                class="el-icon-download pointer"
                v-if="scope.$index !== columnList.length - 1"
                @click="downHandle(scope.$index)"
                style="font-size: 16px"
              ></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
    upHandle(index) {
      //当前行
      let currData = this.columnList[index];
      // //移除当前行并插入到上一行之前
      this.columnList.splice(index, 1);
      this.columnList.splice(index - 1, 0, currData);
    },
    downHandle(index) {
      //下一行
      let nextData = this.columnList[index + 1];
      // //移除下一行并插入到当前行之前
      this.columnList.splice(index + 1, 1);
      this.columnList.splice(index, 0, nextData);
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Element UI 是一套基于 Vue.js 2.0 的组件库,提供了许多常用的组件,包括表格组件。下面是一个使用 Element UI 实现动态表格的示例代码: ``` <template> <div> <el-button @click="addRow">添加</el-button> <el-table :data="tableData"> <el-table-column v-for="(col, index) in columns" :key="index" :label="col.label"> <template v-if="col.type === 'input'"> <el-input v-model="tableData[index][col.prop]"></el-input> </template> <template v-if="col.type === 'select'"> <el-select v-model="tableData[index][col.prop]"> <el-option v-for="option in col.options" :key="option.value" :label="option.label" :value="option.value"></el-option> </el-select> </template> </el-table-column> </el-table> </div> </template> <script> export default { data() { return { columns: [ { label: '姓名', prop: 'name', type: 'input' }, { label: '性别', prop: 'gender', type: 'select', options: [{ value: 'male', label: '男' }, { value: 'female', label: '女' }] }, { label: '年龄', prop: 'age', type: 'input' }, { label: '电话', prop: 'phone', type: 'input' } ], tableData: [ { name: '张三', gender: 'male', age: 18, phone: '1234567890' }, { name: '李四', gender: 'female', age: 20, phone: '0987654321' } ] }; }, methods: { addRow() { this.tableData.push({}); } } }; </script> ``` 上面的代码中,`columns` 数组中定义了表格的列数据,其中每个元素包含 `label`(列名)、`prop`(对应数据对象中的属性名)、`type`(组件类型,可以是输入框或下拉框)等属性。`tableData` 数组中保存了表格数据,可以在 `addRow` 方法中添加新的数据。根据 `columns` 数组动态生成表格列,根据 `tableData` 数组动态生成表格,并根据 `type` 属性生成对应的输入框或下拉框组件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值