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>
相关推荐
东东51630 分钟前
基于ssm的网上房屋中介管理系统vue
前端·javascript·vue.js
harrain2 小时前
什么!vue3.4开始,v-model不能用在prop上
前端·javascript·vue.js
阿蒙Amon7 小时前
TypeScript学习-第7章:泛型(Generic)
javascript·学习·typescript
睡美人的小仙女1277 小时前
Threejs加载环境贴图报错Bad File Format: bad initial token
开发语言·javascript·redis
fanruitian7 小时前
uniapp android开发 测试板本与发行版本
前端·javascript·uni-app
rayufo7 小时前
【工具】列出指定文件夹下所有的目录和文件
开发语言·前端·python
RANCE_atttackkk7 小时前
[Java]实现使用邮箱找回密码的功能
java·开发语言·前端·spring boot·intellij-idea·idea
摘星编程8 小时前
React Native + OpenHarmony:Timeline垂直时间轴
javascript·react native·react.js
2501_944525549 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 支出分析页面
android·开发语言·前端·javascript·flutter
jin1233229 小时前
React Native鸿蒙跨平台完成剧本杀组队详情页面,可以复用桌游、团建、赛事等各类组队详情页开发
javascript·react native·react.js·ecmascript·harmonyos