人员选择组件,记住用户选择,分页查询也管用:核心逻辑

//template
<el-table
	  ref="tableRef"
	  :height="440"
	  :data="orgList"
	  v-loading="loading"
	  @row-click="handleClickRow"
	  @selection-change="handleSelectionChange"
	  @select="handleSelectChange"
	  @select-all="handleSelectAll"
	>
	<el-table-column type="selection"></el-table-column>
</el-table>
props:
//最开始已经选择的数据
  initEmpArray: {
    type: Array<any>,
    default: [],
  },
  //接口获取的数据中 可以将conversionKey对应的字段值 复制一份给到 checkKey
  conversionKey: {
    type: String,
    default: "",
  },
  //校验的key,标记选中还是未选择数据逻辑中对比的key
  checkKey: {
    type: String,
    default: "id",
  },

/**
 * 全选
 * @param selection
 */
const handleSelectAll = (selection: any) => {
  handleAddOrDelete(selection, orgList.value, selectedEmpList.value);
};
/**
 * 勾选多行
 * @param data
 */
function handleSelectionChange(selection: any) {}
/**
 * 表格勾选事件
 */
const handleSelectChange = (selection: any, row: any) => {
  //判断当前是勾选还是取消勾选  selection 里面有 row 就是 新增,  没有就是删除
  let isCheck = false;
  if (
    selection.filter((item: any) => item[props.checkKey] == row[props.checkKey])
      .length != 0
  ) {
    isCheck = true;
  } else {
    isCheck = false;
  }
  handleRowAddOrDelete(row, isCheck, selectedEmpList.value);
};

//运行逻辑
/**
 * 处理单个数据的新增删除
 * @param row
 * @param isCheck
 * @param allSelectedData
 */
const handleRowAddOrDelete = (
  row: any,
  isCheck: boolean,
  allSelectedData: any
) => {
  if (isCheck) {
    allSelectedData.push(row);
  } else {
    allSelectedData.forEach((item: any, index: number) => {
      if (item[props.checkKey] == row[props.checkKey]) {
        allSelectedData.splice(index, 1);
      }
    });
  }
  return allSelectedData;
};
/**
 * 处理当前已经选择的数据中是否需要新增和修改
 */
const handleAddOrDelete = (
  selection: any,
  tableData: any,
  allSelectedData: any
) => {
  //从显示的表格数据中取出未被选中的数据
  const noSelectedEmp = tableData.filter(
    (item: any) =>
      selection.findIndex(
        (oItem: any) => oItem[props.checkKey] == item[props.checkKey]
      ) == -1
  );
  //将noSelectedEmp数据和allSelectedData中数据对比,如果没有就说明数据被取消勾选了,需要在allSelectedData中删除
  noSelectedEmp.map((noItem: any) => {
    const index = allSelectedData.findIndex(
      (sItem: any) => sItem[props.checkKey] == noItem[props.checkKey]
    );
    if (index != -1) {
      allSelectedData.splice(index, 1);
    }
  });
  //将selection 和 allSelectedData 中数据对比,有不处理,没有就加进去
  selection.map((item: any) => {
    const index = allSelectedData.findIndex(
      (sItem: any) => sItem[props.checkKey] == item[props.checkKey]
    );
    if (index == -1) {
      allSelectedData.push(item);
    }
  });
  return allSelectedData;
};
/**
 * 表格数据对比已经选择的数据,处理是否需要勾选上
 */
const handleCheckTableData = (tableData: any, allSelectedData: any) => {
  tableData.forEach((item: any, index: number) => {
    const selectIndex = allSelectedData.findIndex(
      (sItem: any) => sItem[props.checkKey] == item[props.checkKey]
    );
    if (selectIndex != -1) {
      tableRef.value.toggleRowSelection(item, true);
    }
  });
};


getTableData(queryParams)
    .then((res: any) => {
      const data = res.data;
      //这里复制一个数据的原因是因为 可能 列表查询接口中返回的数据里面没有我们需要对比的 key
      if (props.conversionKey && props.conversionKey != "") {
        data.list.forEach((item: any) => {
          item[props.checkKey] = item[props.conversionKey];
        });
      }
      //标记表格选中
      nextTick(() => {
        handleCheckTableData(data.list, selectedEmpList.value);
      });
    })
    .finally(() => {
      loading.value = false;
    });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个基于 Vue 3 的分页组件,其中包含 `updateCurrentPage` 函数来更新当前页码: ```html <template> <div class="pagination"> <button @click="previousPage" :disabled="currentPage === 1">上一页</button> <span>{{ currentPage }}</span> <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button> </div> </template> <script> export default { props: { totalItems: { type: Number, required: true }, itemsPerPage: { type: Number, default: 10 } }, computed: { totalPages() { return Math.ceil(this.totalItems / this.itemsPerPage); } }, data() { return { currentPage: 1 }; }, methods: { previousPage() { if (this.currentPage > 1) { this.currentPage--; this.updateCurrentPage(); } }, nextPage() { if (this.currentPage < this.totalPages) { this.currentPage++; this.updateCurrentPage(); } }, updateCurrentPage() { // 在这里执行更新当前页码的操作 console.log("当前页码是:" + this.currentPage); } } }; </script> ``` 该组件接收 `totalItems` 和 `itemsPerPage` 两个参数,分别表示总共的条目数和每页显示的条目数。在 `computed` 中计算出总页数 `totalPages`,在 `data` 中定义当前页码 `currentPage`,在 `methods` 中实现了上一页和下一页的点击事件,并在其中调用了 `updateCurrentPage` 函数来更新当前页码。 您可以在 `updateCurrentPage` 函数中添加其他操作来更新当前页码所对应的数据。在父组件中使用该分页组件时,需要传入 `totalItems` 和 `itemsPerPage` 两个参数,并监听 `updateCurrentPage` 事件来获取当前页码。例如: ```html <template> <div> <my-pagination :total-items="totalItems" :items-per-page="itemsPerPage" @update-current-page="onUpdateCurrentPage" /> <!-- 在这里渲染当前页码所对应的数据 --> </div> </template> <script> import MyPagination from "./MyPagination.vue"; export default { components: { MyPagination }, data() { return { totalItems: 100, itemsPerPage: 10, currentPage: 1 }; }, methods: { onUpdateCurrentPage(page) { this.currentPage = page; // 在这里更新当前页码所对应的数据 } } }; </script> ``` 在这个例子中,父组件监听了 `updateCurrentPage` 事件,当事件触发时将当前页码 `currentPage` 更新为事件传递过来的页码 `page`,并在 `onUpdateCurrentPage` 函数中更新当前页码所对应的数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值