elementui + vue2实现表格行的上下移动

scope.$index 获取当前点击的序号

<el-dialog
    append-to-body
    title="条件编辑"
    :visible.sync="dialogVisible"
    width="60%"
>
    <el-table :data="data1" border style="width: 100%">
        <el-table-column type="index" label="序" width="100" align="center">
        </el-table-column>
        <el-table-column prop="edit" label="" align="center">
            <template slot-scope="scope">
               <el-button
                  :disabled="scope.$index === 0"
                  plain
                  type="primary"
                  icon="el-icon-top"
                  @click="moveUp(scope.$index, scope.row)"
                  >上移</el-button>
                <el-button
                :disabled="scope.$index === data1.length - 1"
                  plain
                  type="primary"
                  icon="el-icon-bottom"
                  @click="moveDown(scope.$index, scope.row)"
                  >下移</el-button>
             </template>
        </el-table-column>
     </el-table>
</el-dialog>
 this.data1 是列表的总数据

// 上移
moveUp(index, row) {
    if(index !== 0){
      const currentRow = row;
      this.data1.splice(index, 1);
      this.data1.splice(index - 1, 0, currentRow);
     }
},
// 下移
moveDown(index, row) {
    if(index !== this.data1.length-1){
      const currentRow = row;
      this.data1.splice(index, 1);
      this.data1.splice(index + 1, 0, currentRow);
     }
},

如果不想受分页影响

表格第一列为自增序号(不受分页影响)

<el-table-column label="序号" width="70" align="left">
    <template slot-scope="scope">
       {{ (scope.$index+1)+(currentPage-1)*pageSize }}
    </template>
</el-table-column>

  --------->

elementui + vue2实现表格行的上下移动_<el-button @click="moveup(scope.$index)">up</el-bu-CSDN博客

VUE element-ui之table表格自增序号(前端实现)_vue前端增加一个序号列-CSDN博客

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Element UI 和 Vue 2 的组合来根据 JSON 数据生成表单和表格。以下是一个基本的示例: 首先,你需要在 Vue 中创建一个名为 `form` 的对象来存储表单数据。然后,你需要使用 `el-form` 组件来渲染表单,使用 `v-for` 指令来遍历 JSON 数据并动态生成表单元素。 ```html <template> <div> <el-form :model="form" label-width="120px"> <el-form-item v-for="(item, index) in formData" :key="index" :label="item.label"> <el-input v-if="item.type === 'text'" v-model="form[item.field]" /> <el-select v-if="item.type === 'select'" v-model="form[item.field]" :options="item.options" /> </el-form-item> </el-form> </div> </template> <script> export default { data() { return { form: {}, formData: [ { label: '姓名', field: 'name', type: 'text' }, { label: '性别', field: 'gender', type: 'select', options: [ { value: 'male', label: '男' }, { value: 'female', label: '女' } ] } ] }; } }; </script> ``` 接下来,你可以使用 `el-table` 组件来渲染表格,使用 `v-for` 指令遍历 JSON 数据并动态生成表格列。你还需要将表格数据存储在 `tableData` 中。 ```html <template> <div> <el-table :data="tableData"> <el-table-column v-for="(item, index) in tableColumns" :key="index" :label="item.label" :prop="item.field" /> </el-table> </div> </template> <script> export default { data() { return { tableData: [], tableColumns: [ { label: '姓名', field: 'name' }, { label: '性别', field: 'gender' } ] }; } }; </script> ``` 最后,你需要使用 `mounted` 钩子函数来初始化表单和表格数据。你可以使用 `Object.assign` 方法将 JSON 数据中的默认值合并到表单对象中,并将表格数据存储在 `tableData` 中。 ```javascript mounted() { // 初始化表单数据 this.formData.forEach(item => { this.$set(this.form, item.field, item.defaultValue || ''); }); // 初始化表格数据 this.tableData = [ { name: '张三', gender: '男' }, { name: '李四', gender: '女' } ]; } ``` 这就是使用 Element UI 和 Vue 2 根据 JSON 数据生成表单和表格的基本过程。当然,你也可以根据自己的需求对代码进修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值