antd分页组件table 支持slot 搜索条件

在这里插入图片描述

<template>
  <div class="data-table">
    <a-spin :spinning="spinning">
      <a-card :title="title" :bordered="false">
        <div class="search" v-if="$slots.search || $slots.searchmore">
          <!--  -->
          <a-form
            size="mini"
            class="dt-search-form-inline"
            layout="inline"
            :model="searchParams"
            @submit.prevent="search({ _fromBar: true, _begin: 0 })"
          >
            <div v-if="showmore">
              <div class="formSearchLittle">
                <slot name="search" />
              </div>
            </div>
            <div v-else>
              <div >
                <slot name="searchmore" />
              </div>
            </div>
          </a-form>
        </div>
        <div class="dt-toolbar" v-if="$slots.toolbar">
          <slot name="toolbar" />
        </div>
        <a-table
          :columns="columns"
          :data-source="dataSource"
          :pagination="false"
          :bordered="bordered"
          rowKey="id"
        >

        </a-table>
        <div class='pagination' v-if="pageable&&total>0">
          <span >显示第1{{total}}项结果,{{total}}</span>
          <a-pagination
            v-model:current="current"
            v-model:pageSize="pageSize"
            :total="total"
            :showQuickJumper="true"
            @change="onChange"
          >
          </a-pagination>
        </div>
      </a-card>
    </a-spin>
  </div>
</template>
<script>
import { defineComponent, ref, watch } from "vue";
import { Form } from "ant-design-vue";
export default defineComponent({
  name: "data-table",
  props: {
    //是否显示边框
    bordered: {
      type: Boolean,
      default: false,
    },
    // 请求的返回渲染参数
    resultKey: {
      type: String,
      default: "data",
    },
    // 行
    columns: Array,
    //请求地址
    serverId: { type: Function, default: function () {} },
    //搜索请求
    searchParams: {
      type: Object,
      default: () => ({}),
    },
    title: String,
    // 是否显示分页
    pageable: {
      type: Boolean,
      default: true,
    },
  },
  setup(props, context) {
    const dataSource = ref([
	]);
    const { resetFields } = Form.useForm(props.searchParams, {});
    const spinning = ref(false);
    const pageSize = ref(10);
    const current = ref(1);
    const total = ref(0);
    const onChange = (pageNumber) => {
      search();
    };

    const request = (param) => {
      return props.serverId(param);
    };
    // 页面搜索
    const search = (params = {}, fn = (rows) => rows) => {
      const { _begin } = params;
      if (_begin >= 0) {
        current.value = _begin + 1;
      }
      const defaultParams = {
        pageNo: current.value,
        pageSize: pageSize.value,
        ...props.searchParams,
      };
      spinning.value = true;
      return request(defaultParams)
        .then((res) => {
          let result = res.data;
          if (result.code === 200) {
            dataSource.value = result.data;
            total.value = result.total;
            current.pageNo = result.pageNo;
          }
        })
        .catch((err) => {
          console.log(err);
        })
        .finally(() => {
          spinning.value = false;
        });
    };

    //展开与收起
    const showmore = ref(true);
    const toogleShow = () => {
      showmore.value = !showmore.value;
    };
    watch(pageSize, () => {
      search();
    });
    return {
      dataSource,
      onChange,
      total,
      pageSize,
      request,
      current,
      search,
      spinning,
      resetFields,
      showmore,
      toogleShow,
    };
  },
});
</script>
<style lang="less" scoped>
.search {
  margin-bottom: 16px;
}
.pagination {
  margin-top: 6px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值