elementui:el-table支持搜索、切换分页多选功能,以及数据回显

news/2025/2/8 17:44:43 标签: elementui, vue.js, javascript

1、el-table相关代码,需注意:row-key="(row) => { return row.id }" 以及 :reserve-selection="true"

   <div class="boxList">
              <div class="search-form">
                <!-- 搜索表单 -->
                <el-form :inline="true" :model="form" ref="form">
                  <el-form-item label="名称" prop="name">
                    <el-input
                      v-model="form.name"
                      placeholder="请输入名称"
                    ></el-input>
                  </el-form-item>

                  <el-form-item label="专业" prop="major">
                    <el-input
                      v-model="form.major"
                      placeholder="请输入专业"
                    ></el-input>
                  </el-form-item>

                  <!-- 操作按钮 -->
                  <el-form-item>
                    <el-button type="primary" @click="handleQuery"
                      >查询</el-button
                    >
                    <el-button type="warning" @click="resetForm"
                      >重置</el-button
                    >
                  </el-form-item>
                </el-form>
              </div>
              <el-table
                ref="table"
                :data="newtableData"
                border
                @selection-change="selectionChangeHandle"
                row-class-name="disable-selection"
                height="300"
                style="width: 100%"
              >
                <el-table-column
                  type="selection"
                  width="50"
                  fixed
                  :reserve-selection="true"
                  align="center"
                />
                <el-table-column prop="name" label="姓名"></el-table-column>
                <el-table-column prop="major" label="专业"></el-table-column>
                <el-table-column prop="deptName" label="部门"></el-table-column>
              </el-table>

              <!-- 分页 -->
              <div class="pagination">
                <el-pagination
                  background
                  layout="total, sizes, prev, pager, next"
                  :total="page.total"
                  :current-page="page.current"
                  :page-size="page.size"
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                >
                </el-pagination>
              </div>
              <div
                style="
                  display: flex;
                  align-items: center;
                  margin-top: 14px;
                  font-size: 14px;
                  margin-left: 3px;
                "
              >
                完成时间:
                <!-- <el-date-picker
                  clearable
                  v-model="form.date"
                  type="datetime"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  :placeholder="请选择日期"
                  style="width: 36%"
                ></el-date-picker> -->
                <el-input
                  type="Number"
                  v-model="form.workTime"
                  :placeholder="'请输入'"
                  clearable
                  style="width: 36%"
                >
                  <template #append>分钟</template>
                </el-input>
              </div>
              <div style="display: flex; justify-content: flex-end">
                <el-button type="primary" @click="onYunxing">运行 </el-button>
              </div>
            </div>

2、获取原数据列表和已选数据,注意// 回显已选中的行,$nextTick那段代码

    //获取列表
    async getUserDataAllListApi() {
      let params = {
        size: this.page.size,
        current: this.page.current,
        name: this.form.name,
        major: this.form.major,
      };
      let { data } = await repairTools.getUserDataAllList(params);
      this.page.size = data.size;
      this.page.total = data.total;
      this.newtableData = data.records;
    },
    //回填人员
    async getGroupUserDataListApi() {
      let params = {
        taskIds: this.schemeAllData.idList,
        taskLruName: this.schemeAllData.taskLruName,
      };
      let { data } = await repairTools.getGroupUserDataList(params);
      this.checkAll = data.data;
      // 回显已选中的行
      this.$nextTick(() => {
        this.newtableData.forEach((row) => {
          const isChecked = this.checkAll.some(
            (checkRow) => checkRow.id === row.id
          );
          if (isChecked) {
            this.$refs.table.toggleRowSelection(row, true);
          }
        });
      });
    },

3、table点击selection-change可以动态获取数据是否有变动

    //选中
    selectionChangeHandle(selected) {
      console.log('99999999999选中',selected)
      this.newSelected = selected;
      this.newtableData.forEach((row) => {
        row.ruleselected = selected.includes(row);
      });
    },

4、有关分页事件

    handleSizeChange(val) {
      this.page.size = val;
      this.tabClick();
    },
    handleCurrentChange(val) {
      this.page.current = val;
      this.tabClick();
    },

5、通过异步来定义调用接口的前后顺序

    //点击tab
   async tabClick() {
      if (this.activeName == "type2") {
      await  this.getUserDataAllListApi();
      await  this.getGroupUserDataListApi();
      } 
    },

6、搜索和重置

   // 查询方法
    handleQuery() {
      this.tabClick();
    },

    // 重置表单
    resetForm() {
      this.$refs.form.resetFields();
      this.form.name = "";
      this.form.major = "";
      this.tabClick();
    },

7、完成代码

<!-- 方案优化 -->
<template>
  <!-- fullscreen -->
  <el-dialog
    :show-close="!run_disabled"
    :title="schemeOptimization_open.onlyShow ? '维修方案详情' : '待修清单'"
    v-model="schemeOptimization_open.open"
    append-to-body
    center
    class="schemeOptimization"
    width="55%"
    @closed="closed"
    @opened="opened"
  >
    <div>
      <div class="WarWoundInfo pad">
        <el-tabs
          v-model="activeName"
          @tab-click="tabClick"
          :before-leave="beforeLeave"
          stretch
        >
          <el-tab-pane
            :label="
              schemeOptimization_open.onlyShow ? '维修方案详情' : '待修清单'
            "
            name="type1"
          >
        11
          </el-tab-pane>

          <el-tab-pane label="人员参数" name="type2">
            <div class="boxList">
              <div class="search-form">
                <!-- 搜索表单 -->
                <el-form :inline="true" :model="form" ref="form">
                  <el-form-item label="名称" prop="name">
                    <el-input
                      v-model="form.name"
                      placeholder="请输入名称"
                    ></el-input>
                  </el-form-item>

                  <el-form-item label="专业" prop="major">
                    <el-input
                      v-model="form.major"
                      placeholder="请输入专业"
                    ></el-input>
                  </el-form-item>

                  <!-- 操作按钮 -->
                  <el-form-item>
                    <el-button type="primary" @click="handleQuery"
                      >查询</el-button
                    >
                    <el-button type="warning" @click="resetForm"
                      >重置</el-button
                    >
                  </el-form-item>
                </el-form>
              </div>
              <el-table
                ref="table"
                :data="newtableData"
                border
                @selection-change="selectionChangeHandle"
                row-class-name="disable-selection"
                height="300"
                style="width: 100%"
              >
                <el-table-column
                  type="selection"
                  width="50"
                  fixed
                  :reserve-selection="true"
                  align="center"
                />
                <el-table-column prop="name" label="姓名"></el-table-column>
                <el-table-column prop="major" label="专业"></el-table-column>
                <el-table-column prop="deptName" label="部门"></el-table-column>
              </el-table>

              <!-- 分页 -->
              <div class="pagination">
                <el-pagination
                  background
                  layout="total, sizes, prev, pager, next"
                  :total="page.total"
                  :current-page="page.current"
                  :page-size="page.size"
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                >
                </el-pagination>
              </div>
              <div
                style="
                  display: flex;
                  align-items: center;
                  margin-top: 14px;
                  font-size: 14px;
                  margin-left: 3px;
                "
              >
                完成时间:
                <!-- <el-date-picker
                  clearable
                  v-model="form.date"
                  type="datetime"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  :placeholder="请选择日期"
                  style="width: 36%"
                ></el-date-picker> -->
                <el-input
                  type="Number"
                  v-model="form.workTime"
                  :placeholder="'请输入'"
                  clearable
                  style="width: 36%"
                >
                  <template #append>分钟</template>
                </el-input>
              </div>
              <div style="display: flex; justify-content: flex-end">
                <el-button type="primary" @click="onYunxing">运行 </el-button>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>

      <div
        v-show="
          schemeOptimization_open.onlyShow &&
          this.schemeData.type.sub_type == '人员限制下时间最短'
        "
        style="display: flex; justify-content: center; height: 50px"
      >
        <el-button
          size="small"
          type="primary"
          style="height: 30px"
          @click="goTo_schemeOptimization"
          >方案优化
        </el-button>
      </div>
      <div
        v-show="!schemeOptimization_open.onlyShow"
        style="width: max-content; margin: 0 auto; height: 50px"
      >
        <el-button
          v-show="activeName === 'type2'"
          :disabled="run_disabled"
          size="small"
          style="margin: 0 20px; padding: 9px 25px; height: 30px"
          @click="resave"
          >重置
        </el-button>
        <el-button
          :disabled="run_disabled"
          size="small"
          style="padding: 9px 25px; height: 30px"
          type="success"
          @click="run"
          >运行
        </el-button>
        <!-- <el-button type="primary" size="small" style="margin-left: 0;padding: 9px 25px">保存
                </el-button> -->
      </div>
    </div>

  
</template>

<script>
import EchartTreeDialog from "./EchartTreeDialog.vue";
import EchartGanttDialog from "./EchartGanttDialog.vue";
import MaintenanceResourceTable from "./MaintenanceResourceTable.vue";
// { getRepairResources }
import repairTools from "@/api/basicData/repairTools";

export default {
  name: "SchemeOptimization",
  props: {
  },
  components: { EchartTreeDialog, EchartGanttDialog, MaintenanceResourceTable },
  data() {
    return {
      newSelected: [],
      form: {
        name: null,
        major: null,
      },
      page: {
        size: 10,
        current: 1,
        total: null,
      },
      newtableData: [],
     
    };
  },
  mounted() {
    // this.getUserDataAllListApi();
  },
  watch: {
    schemeAllData: {
      deep: true,
      immediate: true,
      handler(a) {
        if (a) {
        }
      },
    },
  },
  methods: {
    //运行
    async onYunxing() {
      let arr = [];
      this.newSelected.forEach((i) => {
        arr.push(i.id);
      });
      let params = {
        workTime: this.form.workTime,
        userIdList: arr,
        taskIds: this.schemeAllData.idList,
        taskLruName: this.schemeAllData.taskLruName,
      };
      let { data } =
        await this.$API.repairTask.warWoundReport.getGeneticWorkData(params);
    
      if (data.code != 200) {
        this.$message.error(data.msg);
      } else {
        this.$message.success(data.msg);
        // eslint-disable-next-line vue/no-mutating-props
        this.schemeOptimization_open.open = false;
      }
    },
    //点击tab
   async tabClick() {
      if (this.activeName == "type2") {
      await  this.getUserDataAllListApi();
      await  this.getGroupUserDataListApi();
      } 
    },

    //回填人员
    async getGroupUserDataListApi() {
      let params = {
        taskIds: this.schemeAllData.idList,
        taskLruName: this.schemeAllData.taskLruName,
      };
      let { data } = await repairTools.getGroupUserDataList(params);
      this.checkAll = data.data;
      // 回显已选中的行
      this.$nextTick(() => {
        this.newtableData.forEach((row) => {
          const isChecked = this.checkAll.some(
            (checkRow) => checkRow.id === row.id
          );
          if (isChecked) {
            this.$refs.table.toggleRowSelection(row, true);
          }
        });
      });
    },
    //获取列表
    async getUserDataAllListApi() {
      let params = {
        size: this.page.size,
        current: this.page.current,
        name: this.form.name,
        major: this.form.major,
      };
      let { data } = await repairTools.getUserDataAllList(params);
      this.page.size = data.size;
      this.page.total = data.total;
      this.newtableData = data.records;
    },
    // 查询方法
    handleQuery() {
      this.tabClick();
    },

    // 重置表单
    resetForm() {
      this.$refs.form.resetFields();
      this.form.name = "";
      this.form.major = "";
      this.tabClick();
    },
    //选中
    selectionChangeHandle(selected) {
      console.log('99999999999选中',selected)
      this.newSelected = selected;
      this.newtableData.forEach((row) => {
        row.ruleselected = selected.includes(row);
      });
    },
    handleSizeChange(val) {
      this.page.size = val;
      this.tabClick();
    },
    handleCurrentChange(val) {
      this.page.current = val;
      this.tabClick();
    },

  },
};
</script>

8、效果如下


http://www.niftyadmin.cn/n/5845163.html

相关文章

突破YOLOv11训练:用幽默的方式玩转自定义数据集与物体检测

前言 你是否曾在训练深度学习模型时,望着屏幕上那一堆堆的错误信息,差点觉得自己的大脑要冒烟?如果你也曾体验过这种“技术折磨”,恭喜,你找对地方了!今天,我们将带你踏入YOLOv11的神奇世界,用幽默的方式教你如何训练物体检测模型,处理自定义数据集。放心,这不仅仅是…

Centos7 停止维护,docker 安装

安装docker报错 执行docker安装命令&#xff1a;sudo yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin&#xff0c;出现如下错误 更换yum源 [rootlocalhost yum.repos.d]# sudo mv /etc/yum.repos.d/CentOS-Base.repo /et…

园区网设计与实战

想做一个自己学习的有关的csdn账号&#xff0c;努力奋斗......会更新我计算机网络实验课程的所有内容&#xff0c;还有其他的学习知识^_^&#xff0c;为自己巩固一下所学知识。 我是一个萌新小白&#xff0c;有误地方请大家指正&#xff0c;谢谢^_^ 文章目录 前言 这个实验主…

基于web前端对简书页眉的开发及登陆的跳转

<!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>简书</title><link rel"stylesheet&q…

极客说|利用 Azure AI Agent Service 创建自定义 VS Code Chat participant

作者&#xff1a;卢建晖 - 微软高级云技术布道师 「极客说」 是一档专注 AI 时代开发者分享的专栏&#xff0c;我们邀请来自微软以及技术社区专家&#xff0c;带来最前沿的技术干货与实践经验。在这里&#xff0c;您将看到深度教程、最佳实践和创新解决方案。关注「极客说」&a…

笔记本电脑屏幕泛白问题解决详解(AMD显卡)

前些天发现了一个蛮有意思的人工智能学习网站,8个字形容一下"通俗易懂&#xff0c;风趣幽默"&#xff0c;感觉非常有意思,忍不住分享一下给大家。 &#x1f449;点击跳转到教程 前言&#xff1a; 本人的电脑是AMD显卡&#xff0c;出现屏幕泛白。 如果没有驱动管理软件…

鸿蒙接入支付宝SDK后模拟器无法运行,报错error: install parse native so failed.

鸿蒙项目接入支付宝后&#xff0c;运行提示error: install parse native so failed. 该问题可能由于设备支持的 Abi 类型与 C 工程中的不匹配导致. 官网error: install parse native so failed.错误解决办法 根据官网提示在模块build-profile.json5中添加“x86_64”依然报错 问…

腾讯云HAI,一键开启DeepSeek大模型探索之旅

引言 在当今这个科技飞速发展的时代&#xff0c;AI 浪潮正以前所未有的速度席卷全球&#xff0c;深刻地改变着我们生活的方方面面。从智能语音助手到自动驾驶汽车&#xff0c;从图像识别技术到医疗诊断辅助&#xff0c;AI 的身影无处不在。 DeepSeek 模型&#xff0c;作为众多…