vue element-table分页回显选中与再次更改保留状态,前端手动过滤多条件查询

需求:
1.table表格多选,并且切换分页后记住上一页选项
2.回显数据,切换分页后依然回显
3.全选,取消全选数据正常变化
4.后台分页
5.前端手动过滤,多条件查询
代码:

  props: {
  //回显的数据,我这里是上一页传过来的数据
    questionListChoose: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
data() {
    return {
    	roleList:[],//总数据
    	echoList: [], //选中的id
        echoListObj: [], //选中的对象
        echoListAllObj: [], //选中的对象--备份全部
        filter: {},//筛选条件
    }
}
  mounted() {
    this.getList();
    this.echoList = JSON.parse(
      JSON.stringify(
        this.questionListChoose.map((i) => {
          return i.question_id;
        })
      )
    );
    this.echoListObj = JSON.parse(JSON.stringify(this.questionListChoose));//数据对象
    this.echoListAllObj = JSON.parse(JSON.stringify(this.questionListChoose));//备份
    console.log("echoList==", this.echoList);
  },
 <el-table
          ref="allTable"
          v-loading="loading"
          :data="roleList"
          @select="select2"
          @select-all="selectAll2"
          height="500"
        >
          <el-table-column width="55" type="selection" />
 </el-table>
  /** 查询列表 */
    getList() {
      this.loading = true;
      this.queryParams.question_bank_id = this.question_bank_id;
      questionList(this.queryParams).then((response) => {
        this.roleList = response.data;
        this.total = response.total;
        //在当前分页列表中查询与回显数据是否有一致的,一致的则回显勾选
        this.$nextTick(() => {
          this.roleList.forEach((item) => {
            if (this.echoList.includes(item.question_id)) {
              this.$refs.allTable.toggleRowSelection(item, true);
            }
          });
        });
        this.loading = false;
      });
    },
    // 单选
    select2(selecteds, row) {
      if (!this.echoList.includes(row.question_id)) {
        // 回显数据⾥没有本条,把这条加进来(选中)
        this.echoList.push(row.question_id);
        this.echoListObj.push(row);
        this.echoListAllObj.push(row);
      } else {
        // 回显数据⾥有本条,把这条删除(取消选中)
        this.echoList.forEach((question_id, index) => {
          if (question_id === row.question_id) {
            this.echoList.splice(index, 1);
            this.echoListObj.splice(index, 1);
            this.echoListAllObj.splice(index, 1);
          }
        });
      }
    },
//全选,取消全选
    selectAll2(selecteds) {
      if (selecteds.length > 0) {
        //全选
        selecteds.forEach((item) => {
          if (!this.echoList.includes(item.question_id)) {
            this.echoList.push(item.question_id);
            this.echoListObj.push(item);
            this.echoListAllObj.push(item);
          }
        });
      } else {
        this.roleList.forEach((item) => {
          this.echoList.forEach((question_id, index) => {
            if (question_id === item.question_id) {
              this.echoList.splice(index, 1);
              this.echoListObj.splice(index, 1);
              this.echoListAllObj.splice(index, 1);
            }
          });
        });
      }
    },

前端手动过滤,多条件查询

//前端搜索
handleQuery() {
 // 搜索已选试题
        var objIsEmpty =
          this.filter.topic_dry == '' &&
          this.filter.difficulty == "" &&
          this.filter.topic == '' &&
          this.filter.dictionary_name =='';
        if (objIsEmpty) {
         // 都为空不做处理
        } else {
          let tempFilter = {};
          for (var key in this.filter) {
            if (
              typeof this.filter[key] != "undefined" &&
              typeof this.filter[key] != "null" &&
              this.filter[key] != null &&
              this.filter[key] != ""
            ) {
              tempFilter[key] = this.filter[key];
            }
          }
          console.log(tempFilter, "输出tempFilter");
          this.$nextTick(() => {
            this.echoListObj = this.echoListAllObj.filter((item) => {
              let flag = false;
              for (key in tempFilter) {
                if (
                  item[key].toString().indexOf(tempFilter[key].toString()) >= 0
                ) {
                  flag = true;
                } else {
                  flag = false;
                  break;
                }
              }
              if (flag) {
                return item;
              }
            });
            // 搜索结果回显选中
            this.$nextTick(() => {
              this.echoListObj.forEach((i) => {
                this.$refs.multipleTable.toggleRowSelection(i, true);
              });
            });
          });
        }
        console.log(this.echoListObj, "输出筛选结果");

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue.js中,使用Element UI组件库可以很方便地实现分页查询和条件查询。以下是一个简单的例子,可以帮助你理解如何使用Element UI组件库实现分页查询和条件查询: HTML代码: ``` <template> <div> <el-form :inline="true" :model="queryForm" class="query-form"> <el-form-item label="姓名"> <el-input v-model="queryForm.name"></el-input> </el-form-item> <el-form-item label="年龄"> <el-input v-model="queryForm.age"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="query">查询</el-button> </el-form-item> </el-form> <el-table :data="tableData" style="width: 100%"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> </el-table> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" :total="total"> </el-pagination> </div> </template> ``` JavaScript代码: ``` <script> export default { data () { return { queryForm: { name: '', age: '' }, tableData: [], currentPage: 1, pageSize: 10, total: 0 } }, methods: { query () { // 执行查询操作 this.getTableData() }, getTableData () { // 模拟异步请求数据 setTimeout(() => { // 根据条件查询数据 let data = this.data.filter(item => { return item.name.includes(this.queryForm.name) && item.age.includes(this.queryForm.age) }) // 计算分页数据 let start = (this.currentPage - 1) * this.pageSize let end = start + this.pageSize let pageData = data.slice(start, end) // 更新表格数据分页信息 this.tableData = pageData this.total = data.length }, 1000) }, handleSizeChange (pageSize) { this.pageSize = pageSize this.getTableData() }, handleCurrentChange (currentPage) { this.currentPage = currentPage this.getTableData() } }, mounted () { // 初始化查询数据 this.getTableData() } } </script> ``` 在上面的代码中,我们使用了Element UI的el-form和el-table组件来实现条件查询和表格渲染。在el-form组件中,我们使用v-model指令来绑定查询条件到queryForm对象上,并在el-button组件中使用@click事件来触发查询操作。在el-table组件中,我们使用:data指令来绑定表格数据tableData对象上,并使用el-table-column组件来定义表格列。最后,我们使用el-pagination组件来实现分页功能,并使用@size-change和@current-change事件来处理分页信息的变化。 在getTableData方法中,我们使用setTimeout模拟了一个异步请求数据的过程,并根据查询条件过滤数据。然后,我们根据当前页码和每页条数计算分页数据,并更新表格数据分页信息。 希望这个例子能帮助你理解如何使用Element UI组件库实现分页查询和条件查询

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值