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>
相关推荐
0思必得020 分钟前
[Web自动化] Selenium处理滚动条
前端·爬虫·python·selenium·自动化
Misnice22 分钟前
Webpack、Vite、Rsbuild区别
前端·webpack·node.js
青茶36023 分钟前
php怎么实现订单接口状态轮询(二)
前端·php·接口
大橙子额1 小时前
【解决报错】Cannot assign to read only property ‘exports‘ of object ‘#<Object>‘
前端·javascript·vue.js
WooaiJava2 小时前
AI 智能助手项目面试技术要点总结(前端部分)
javascript·大模型·html5
LYFlied2 小时前
从 Vue 到 React,再到 React Native:资深前端开发者的平滑过渡指南
vue.js·react native·react.js
爱喝白开水a3 小时前
前端AI自动化测试:brower-use调研让大模型帮你做网页交互与测试
前端·人工智能·大模型·prompt·交互·agent·rag
Never_Satisfied3 小时前
在JavaScript / HTML中,关于querySelectorAll方法
开发语言·javascript·html
董世昌413 小时前
深度解析ES6 Set与Map:相同点、核心差异及实战选型
前端·javascript·es6
B站_计算机毕业设计之家3 小时前
豆瓣电影数据采集分析推荐系统 | Python Vue Flask框架 LSTM Echarts多技术融合开发 毕业设计源码 计算机
vue.js·python·机器学习·flask·echarts·lstm·推荐算法