Vue el-table 字段自定义排序

需求:
  • 使用 el - dialog 组件实现弹窗功能,通过 showModal 控制弹窗的显示与隐藏。
  • el - table 展示列表数据,通过 type="selection" 实现每行数据的勾选功能。
  • 在操作列中,通过 moveUp 和 moveDown 方法实现每行数据的上下排序功能,在 confirm 方法中可以处理勾选的数据逻辑。
效果:
代码实现:
复制代码
  <template>
  <div>
    <button @click="showModal = true">打开弹窗</button>
    <el - dialog :visible.sync="showModal" title="列表弹窗">
      <el - table :data="tableData" border>
        <el - table - column type="selection" width="55"></el - table - column>
        <el - table - column label="字段" prop="field"></el - table - column>
        <el - table - column label="操作">
          <template slot - scope="scope">
            <el - button size="mini" @click="moveUp(scope.$index)" :disabled="scope.$index === 0">↑</el - button>
            <el - button size="mini" @click="moveDown(scope.$index)" :disabled="scope.$index === tableData.length - 1">↓</el - button>
          </template>
        </el - table - column>
      </el - table>
      <div slot="footer" class="dialog - footer">
        <el - button @click="showModal = false">取 消</el - button>
        <el - button type="primary" @click="confirm">确 认</el - button>
      </div>
    </el - dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showModal: false,
      tableData: [
        { field: '一启' },
        { field: '二乔' },
        { field: '三思' },
        { field:'四维' },
        { field: '伍德' },
        { field: '六离' },
        { field: '七彩' },
        { field: '八荒' },
        { field: '九歌' },
        { field: '十方' },
        { field: '十一郎' },
        { field: '十二辰' },
        { field: '十三弦' },
        { field:'十四行' },
        { field:'十五夜' }
      ]
    };
  },
  methods: {
    moveUp(index) {
      if (index > 0) {
        const temp = this.fields[index];
        this.tableData.splice(index, 1);
        this.tableData.splice(index - 1, 0, temp);
      }
    },
    moveDown(index) {
      if (index < this.tableData.length - 1) {
        const temp = this.fields[index];
        this.tableData.splice(index, 1);
        this.tableData.splice(index + 1, 0, temp);
      }
    },
    confirm() {
      // 这里可以处理勾选的数据等逻辑
      const selectedRows = this.$refs.table.selection;
      console.log('勾选的数据:', selectedRows);
      this.showModal = false;
    }
  }
};
</script>

<style scoped>
/* 可以根据需要添加样式 */
.el-table .el-table__header .cell .el-checkbox__inner{
    display: none !important; 
}
.el-table .el-table__header .el-table-column--selection .cell::before{
    content: '选择';
    text-align: center;
    line-height: 37px;
}
</style>
相关推荐
passerby606138 分钟前
完成前端时间处理的另一块版图
前端·github·web components
掘了1 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅1 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅1 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅2 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment2 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅2 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊2 小时前
jwt介绍
前端
爱敲代码的小鱼2 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax
吹牛不交税2 小时前
admin.net-v2 框架使用笔记-netcore8.0/10.0版
vue.js·.netcore