如何正确使用PrimeVue组件库的表格组件DataTable与DataView的分页功能?
3 2025-06-28 02:20:21

如何正确使用PrimeVue组件库的表格组件DataTable与DataView的分页功能?

PrimeVue中,表格自带的分页功能表示对已有数据进行分页,即绑定的数据是全量数据,如果要分页从服务器获取数据展示,则要使用单独的分页组件Paginator,代码如下

vue 复制代码
<DataTable :value="data" :size="'small'" tableStyle="min-width: 50rem">
  <template #empty>
    <div class="flex flex-col items-center">
      <div class="py-[32px]">
        <i class="pi pi-box text-[36px]"></i>
      </div>
      <div class="pb-[32px]">
        暂无数据
      </div>
    </div>
  </template>
  <Column header="文件类型">
    <template #body="slotProps">
      {{ slotProps.data.contentType }}
    </template>
  </Column>
  <Column field="ext" header="文件后缀" class="w-[100px]"></Column>
  <Column field="size" header="文件大小">
    <template #body="slotProps">
      {{ calcFileSize(slotProps.data.size) }}
    </template>
  </Column>
  <Column field="createdTime" header="上传时间" class="w-[190px]"></Column>
  <Column class="text-center" :frozen="true" header="操作" align-frozen="right">
    <template #body="{ data }">
      <SplitButton label="更多操作" :model="dataOpts(data)" size="small" severity="help"></SplitButton>
    </template>
  </Column>
</DataTable>
<Paginator :rows="pageParams.size" :totalRecords="total" @page="changePage" :alwaysShow="true" :rowsPerPageOptions="[10, 15, 20, 50, 100]">
  <template #start>
    共有 {{ total }} 条数据
  </template>
</Paginator>

js部分代码

javascript 复制代码
const pageParams = ref({
  size: 10,
  current: 1,
  model: {}
})
const loading = ref(false);
const data = ref([]);
const total = ref(0);
const load = () => {
  // 从服务器加载数据
}

const changePage = (p) => {
  pageParams.value.size = p.rows
  pageParams.value.current = p.page + 1
  load()
}

效果如下:

目录
2025 Hi code!
为天地立心,为生民立命,为往圣继绝学,为万世开太平