当前位置: 首页 > news >正文

Element UI实现表格全选、半选

制作如图所示的表格全选、半选:

父组件

<template>
  <div id="app">
    <SelectHost :hostArray="hostArray" />
  </div>
</template>

<script>
import SelectHost from './components/SelectHost.vue'
export default {
  name: 'App',
  components: {
    SelectHost
  },
  data() {
    return {
      hostArray: [
        {
          "uuid": "bdd6565dfef7424885e48b655134b856",
          "ip": "30.184.21.55",
          "port": "80",
          "priority": "0",
          "status": "Running",
          "vmName": "CQB-L0703002",
          "vmType": "ECS",
          "weight": "10"
        },
        {
          "uuid": "493e1c76bbd4457aa74a9066f0eb1fa3",
          "ip": "30.184.8.7",
          "port": "80",
          "priority": "0",
          "status": "Running",
          "vmName": "CQB-L0615064",
          "vmType": "ECS",
          "weight": "10"
        },
        {
          "uuid": "6f98e7fa0ec94ca7923afc11660ea642",
          "ip": "30.98.188.157",
          "port": "80",
          "priority": "0",
          "status": "Running",
          "vmName": "SZE-L0812244",
          "vmType": "ECS",
          "weight": "1"
        }
      ]
    }
  },
}
</script>

<style lang="scss">
#app {
  padding: 20px;
}
</style>

子组件:自己手搓

<template>
  <div style="position: relative">
    <!-- 全选 or 取消全选 -->
    <div class="header-checkbox">
      <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAll" />
    </div>

    <el-table :data="hostArray" border>
      <el-table-column width="44" align="center" class-name="row-uuid">
        <template slot-scope="scope">
          <el-checkbox-group v-model="checkList" @change="handleCheckRow">
            <el-checkbox :label="scope.row.uuid" />
          </el-checkbox-group>
        </template>
      </el-table-column>
      <el-table-column label="主机名" prop="vmName" align="center" />
      <el-table-column label="主机IP" prop="ip" align="center" />
      <el-table-column label="状态" prop="status" align="center" />
      <el-table-column label="主机类型" prop="vmType" align="center" />
      <el-table-column label="后端端口" prop="port" align="center" />
      <el-table-column label="权重" prop="weight" align="center" />
      <el-table-column label="优先级" prop="priority" align="center" />
    </el-table>


  </div>
</template>

<script>
export default {
  name: 'SelectHost',
  props: {
    hostArray: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      isIndeterminate: false,  // 设置半选的样式
      checkAll: false,  // 全选、取消全选的状态
      checkList: [],  // 单元行的选择状态
    }
  },
  methods: {
    // 全选、取消全选
    handleCheckAll(val) {
      this.checkList = val ? this.hostArray.map(item => item.uuid) : []
      this.isIndeterminate = false
    },
    // 单元行的选择
    handleCheckRow(value) {
      const checkedCount = value.length
      this.checkAll = checkedCount === this.hostArray.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.hostArray.length
    }
  },
}
</script>

<style lang="scss" scoped>
::v-deep .row-uuid .el-checkbox__label {
  display: none;
}
.header-checkbox {
  width: 20px;
  height: 20px;
  position: absolute;
  left: 15px;
  top: 10px;
}
</style>

子组件:Element UI封装好的

<template>
  <div>
    <el-table
      ref="multipleTable"
      :data="hostArray"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="44">
      </el-table-column>
      <el-table-column label="主机名" prop="vmName" align="center" />
      <el-table-column label="主机IP" prop="ip" align="center" />
      <el-table-column label="状态" prop="status" align="center" />
      <el-table-column label="主机类型" prop="vmType" align="center" />
      <el-table-column label="后端端口" prop="port" align="center" />
      <el-table-column label="权重" prop="weight" align="center" />
      <el-table-column label="优先级" prop="priority" align="center" />
    </el-table>

    <div style="margin-top: 20px">
      <el-button @click="toggleSelection([hostArray[1], hostArray[2]])">切换第二、第三行的选中状态</el-button>
      <el-button @click="toggleSelection()">取消选择</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SelectHost',
  props: {
    hostArray: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      multipleSelection: []
    }
  },
  methods: {
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  },
}
</script>

<style lang="scss" scoped>
</style>

相关文章:

  • 微信小程序引入TDesign组件后报错一直提示路径不对(Component is not found in path)的解决方法
  • k8s存储介绍(五)PV与PVC
  • Windows系统中,通过局域网共享文件夹让同一路由器下的其他设备访问文件
  • Kubernetes
  • 【Java/数据结构】优先级队列(PriorityQueue)
  • .git 文件夹
  • ctfshow WEB web7
  • redis配置文件解析
  • oracle密码过期 ORA-28001解决方案: the password has expired
  • Linux学习笔记(应用篇三)
  • Java 大视界 -- 基于 Java 的大数据分布式系统的监控与运维实践(155)
  • RustDesk部署到linux(自建服务器)
  • Win11+VS2022+CGAL5.6配置
  • 自然语言处理NLP-文本预处理
  • nodejs-原型污染链
  • 4.Matplotlib:基础绘图
  • QT自运行程序
  • 在 VMware Workstation 17 中安装的 Ubuntu 虚拟机无法使用桥接模式
  • 【FDTD Solutions仿真入门及应用】
  • 【高项】信息系统项目管理师(十二)项目干系人管理【3分】
  • 新能源汽车,告别混乱创新
  • 专访|导演刘江:给谍战题材注入现实主义的魂
  • 习近平出席中国-拉美和加勒比国家共同体论坛第四届部长级会议开幕式并发表重要讲话
  • 重庆三峡学院回应“中标价85万设备网购300元”:已终止采购
  • 退休夫妻月入1.2万负债1.2亿申请破产,律师:“诚实而不幸”系前置条件
  • 印控克什米尔地区再次传出爆炸声