primevue-mirror/doc/datatable/LazyLoadDoc.vue

490 lines
21 KiB
Vue
Raw Normal View History

2023-02-28 08:29:30 +00:00
<template>
<DocSectionText v-bind="$attrs">
<p>
Lazy mode is handy to deal with large datasets, instead of loading the entire data, small chunks of data is loaded by invoking corresponding callbacks everytime <i>paging</i>, <i>sorting</i> and <i>filtering</i> occurs. Sample below
imitates lazy loading data from a remote datasource using an in-memory list and timeouts to mimic network connection.
</p>
<p>
Enabling the <i>lazy</i> property and assigning the logical number of rows to <i>totalRecords</i> by doing a projection query are the key elements of the implementation so that paginator displays the UI assuming there are actually records
of totalRecords size although in reality they are not present on page, only the records that are displayed on the current page exist.
</p>
<p>Note that, the implementation of <i>checkbox selection</i> in lazy mode needs to be handled manually as in this example since the DataTable cannot know about the whole dataset.</p>
</DocSectionText>
2023-12-31 14:08:33 +00:00
<DeferredDemo @load="loadDemoData">
<div class="card p-fluid">
<DataTable
ref="dt"
v-model:filters="filters"
v-model:selection="selectedCustomers"
:value="customers"
lazy
paginator
:first="first"
:rows="10"
dataKey="id"
:totalRecords="totalRecords"
:loading="loading"
@page="onPage($event)"
@sort="onSort($event)"
@filter="onFilter($event)"
filterDisplay="row"
:globalFilterFields="['name', 'country.name', 'company', 'representative.name']"
:selectAll="selectAll"
@select-all-change="onSelectAllChange"
@row-select="onRowSelect"
@row-unselect="onRowUnselect"
tableStyle="min-width: 75rem"
>
<Column selectionMode="multiple" headerStyle="width: 3rem"></Column>
<Column field="name" header="Name" filterMatchMode="startsWith" sortable>
<template #filter="{ filterModel, filterCallback }">
<InputText v-model="filterModel.value" type="text" @keydown.enter="filterCallback()" class="p-column-filter" placeholder="Search" />
</template>
</Column>
<Column field="country.name" header="Country" filterField="country.name" filterMatchMode="contains" sortable>
<template #body="{ data }">
2024-05-20 12:14:38 +00:00
<div class="flex items-center gap-2">
2023-12-31 14:08:33 +00:00
<img alt="flag" src="https://primefaces.org/cdn/primevue/images/flag/flag_placeholder.png" :class="`flag flag-${data.country.code}`" style="width: 24px" />
<span>{{ data.country.name }}</span>
</div>
</template>
<template #filter="{ filterModel, filterCallback }">
<InputText v-model="filterModel.value" type="text" @keydown.enter="filterCallback()" class="p-column-filter" placeholder="Search" />
</template>
</Column>
<Column field="company" header="Company" filterMatchMode="contains" sortable>
<template #filter="{ filterModel, filterCallback }">
<InputText v-model="filterModel.value" type="text" @keydown.enter="filterCallback()" class="p-column-filter" placeholder="Search" />
</template>
</Column>
<Column field="representative.name" header="Representative" filterField="representative.name" sortable>
<template #body="{ data }">
2024-05-20 12:14:38 +00:00
<div class="flex items-center gap-2">
2023-12-31 14:08:33 +00:00
<img :alt="data.representative.name" :src="`https://primefaces.org/cdn/primevue/images/avatar/${data.representative.image}`" style="width: 32px" />
<span>{{ data.representative.name }}</span>
</div>
</template>
<template #filter="{ filterModel, filterCallback }">
<InputText v-model="filterModel.value" type="text" @keydown.enter="filterCallback()" class="p-column-filter" placeholder="Search" />
</template>
</Column>
</DataTable>
</div>
</DeferredDemo>
2023-02-28 08:29:30 +00:00
<DocSectionCode :code="code" :service="['CustomerService']" />
</template>
<script>
import { CustomerService } from '@/service/CustomerService';
export default {
data() {
return {
loading: false,
totalRecords: 0,
customers: null,
selectedCustomers: null,
selectAll: false,
2023-11-06 10:02:17 +00:00
first: 0,
2023-02-28 08:29:30 +00:00
filters: {
name: { value: '', matchMode: 'contains' },
'country.name': { value: '', matchMode: 'contains' },
company: { value: '', matchMode: 'contains' },
'representative.name': { value: '', matchMode: 'contains' }
},
lazyParams: {},
columns: [
{ field: 'name', header: 'Name' },
{ field: 'country.name', header: 'Country' },
{ field: 'company', header: 'Company' },
{ field: 'representative.name', header: 'Representative' }
],
code: {
2023-09-22 12:54:14 +00:00
basic: `
2023-11-06 10:02:17 +00:00
<DataTable :value="customers" lazy paginator :first="first" :rows="10" v-model:filters="filters" ref="dt" dataKey="id"
2023-02-28 08:29:30 +00:00
:totalRecords="totalRecords" :loading="loading" @page="onPage($event)" @sort="onSort($event)" @filter="onFilter($event)" filterDisplay="row"
:globalFilterFields="['name','country.name', 'company', 'representative.name']"
v-model:selection="selectedCustomers" :selectAll="selectAll" @select-all-change="onSelectAllChange" @row-select="onRowSelect" @row-unselect="onRowUnselect" tableStyle="min-width: 75rem">
<Column selectionMode="multiple" headerStyle="width: 3rem"></Column>
<Column field="name" header="Name" filterMatchMode="startsWith" sortable>
<template #filter="{filterModel,filterCallback}">
<InputText type="text" v-model="filterModel.value" @keydown.enter="filterCallback()" class="p-column-filter" placeholder="Search"/>
</template>
</Column>
<Column field="country.name" header="Country" filterField="country.name" filterMatchMode="contains" sortable>
2023-11-06 10:02:17 +00:00
<template #body="{ data }">
2024-05-20 12:14:38 +00:00
<div class="flex items-center gap-2">
2023-11-06 10:02:17 +00:00
<img alt="flag" src="https://primefaces.org/cdn/primevue/images/flag/flag_placeholder.png" :class="\`flag flag-\${data.country.code}\`" style="width: 24px" />
<span>{{ data.country.name }}</span>
</div>
</template>
2023-02-28 08:29:30 +00:00
<template #filter="{filterModel,filterCallback}">
<InputText type="text" v-model="filterModel.value" @keydown.enter="filterCallback()" class="p-column-filter" placeholder="Search"/>
</template>
</Column>
<Column field="company" header="Company" filterMatchMode="contains" sortable>
<template #filter="{filterModel,filterCallback}">
<InputText type="text" v-model="filterModel.value" @keydown.enter="filterCallback()" class="p-column-filter" placeholder="Search"/>
</template>
</Column>
<Column field="representative.name" header="Representative" filterField="representative.name" sortable>
2023-11-06 10:02:17 +00:00
<template #body="{ data }">
2024-05-20 12:14:38 +00:00
<div class="flex items-center gap-2">
2023-11-06 10:02:17 +00:00
<img :alt="data.representative.name" :src="\`https://primefaces.org/cdn/primevue/images/avatar/\${data.representative.image}\`" style="width: 32px" />
<span>{{ data.representative.name }}</span>
</div>
</template>
2023-02-28 08:29:30 +00:00
<template #filter="{filterModel,filterCallback}">
<InputText type="text" v-model="filterModel.value" @keydown.enter="filterCallback()" class="p-column-filter" placeholder="Search"/>
</template>
</Column>
2023-10-15 09:38:39 +00:00
</DataTable>
`,
2023-09-22 12:54:14 +00:00
options: `
<template>
2023-02-28 08:29:30 +00:00
<div class="card p-fluid">
2023-11-06 10:02:17 +00:00
<DataTable :value="customers" lazy paginator :first="first" :rows="10" v-model:filters="filters" ref="dt" dataKey="id"
:totalRecords="totalRecords" :loading="loading" @page="onPage($event)" @sort="onSort($event)" @filter="onFilter($event)" filterDisplay="row"
:globalFilterFields="['name','country.name', 'company', 'representative.name']"
v-model:selection="selectedCustomers" :selectAll="selectAll" @select-all-change="onSelectAllChange" @row-select="onRowSelect" @row-unselect="onRowUnselect" tableStyle="min-width: 75rem">
2023-02-28 08:29:30 +00:00
<Column selectionMode="multiple" headerStyle="width: 3rem"></Column>
<Column field="name" header="Name" filterMatchMode="startsWith" sortable>
<template #filter="{filterModel,filterCallback}">
<InputText type="text" v-model="filterModel.value" @keydown.enter="filterCallback()" class="p-column-filter" placeholder="Search"/>
</template>
</Column>
<Column field="country.name" header="Country" filterField="country.name" filterMatchMode="contains" sortable>
2023-11-06 10:02:17 +00:00
<template #body="{ data }">
2024-05-20 12:14:38 +00:00
<div class="flex items-center gap-2">
2023-11-06 10:02:17 +00:00
<img alt="flag" src="https://primefaces.org/cdn/primevue/images/flag/flag_placeholder.png" :class="\`flag flag-\${data.country.code}\`" style="width: 24px" />
<span>{{ data.country.name }}</span>
</div>
</template>
2023-02-28 08:29:30 +00:00
<template #filter="{filterModel,filterCallback}">
<InputText type="text" v-model="filterModel.value" @keydown.enter="filterCallback()" class="p-column-filter" placeholder="Search"/>
</template>
</Column>
<Column field="company" header="Company" filterMatchMode="contains" sortable>
<template #filter="{filterModel,filterCallback}">
<InputText type="text" v-model="filterModel.value" @keydown.enter="filterCallback()" class="p-column-filter" placeholder="Search"/>
</template>
</Column>
<Column field="representative.name" header="Representative" filterField="representative.name" sortable>
2023-11-06 10:02:17 +00:00
<template #body="{ data }">
2024-05-20 12:14:38 +00:00
<div class="flex items-center gap-2">
2023-11-06 10:02:17 +00:00
<img :alt="data.representative.name" :src="\`https://primefaces.org/cdn/primevue/images/avatar/\${data.representative.image}\`" style="width: 32px" />
<span>{{ data.representative.name }}</span>
</div>
</template>
2023-02-28 08:29:30 +00:00
<template #filter="{filterModel,filterCallback}">
<InputText type="text" v-model="filterModel.value" @keydown.enter="filterCallback()" class="p-column-filter" placeholder="Search"/>
</template>
</Column>
</DataTable>
</div>
</template>
<script>
2023-03-09 07:54:55 +00:00
import { CustomerService } from '@/service/CustomerService';
2023-02-28 08:29:30 +00:00
export default {
data() {
return {
loading: false,
totalRecords: 0,
customers: null,
selectedCustomers: null,
selectAll: false,
2023-11-06 10:02:17 +00:00
first: 0,
2023-02-28 08:29:30 +00:00
filters: {
'name': {value: '', matchMode: 'contains'},
'country.name': {value: '', matchMode: 'contains'},
'company': {value: '', matchMode: 'contains'},
'representative.name': {value: '', matchMode: 'contains'},
},
lazyParams: {},
columns: [
{field: 'name', header: 'Name'},
{field: 'country.name', header: 'Country'},
{field: 'company', header: 'Company'},
{field: 'representative.name', header: 'Representative'}
]
}
},
mounted() {
this.loading = true;
this.lazyParams = {
2023-12-31 14:08:33 +00:00
first: 0,
rows: 10,
2023-02-28 08:29:30 +00:00
sortField: null,
sortOrder: null,
filters: this.filters
};
this.loadLazyData();
},
methods: {
loadLazyData() {
this.loading = true;
2023-11-06 10:02:17 +00:00
this.lazyParams = { ...this.lazyParams, first: event?.first || this.first };
2023-02-28 08:29:30 +00:00
setTimeout(() => {
CustomerService.getCustomers({ lazyEvent: JSON.stringify(this.lazyParams) }).then((data) => {
this.customers = data.customers;
this.totalRecords = data.totalRecords;
this.loading = false;
});
}, Math.random() * 1000 + 250);
},
onPage(event) {
this.lazyParams = event;
2023-11-06 10:02:17 +00:00
this.loadLazyData(event);
2023-02-28 08:29:30 +00:00
},
onSort(event) {
this.lazyParams = event;
2023-11-06 10:02:17 +00:00
this.loadLazyData(event);
2023-02-28 08:29:30 +00:00
},
2023-11-06 10:02:17 +00:00
onFilter(event) {
2023-02-28 08:29:30 +00:00
this.lazyParams.filters = this.filters;
2023-11-06 10:02:17 +00:00
this.loadLazyData(event);
2023-02-28 08:29:30 +00:00
},
onSelectAllChange(event) {
const selectAll = event.checked;
if (selectAll) {
2023-03-13 10:45:38 +00:00
CustomerService.getCustomers().then(data => {
2023-02-28 08:29:30 +00:00
this.selectAll = true;
this.selectedCustomers = data.customers;
});
}
else {
this.selectAll = false;
this.selectedCustomers = [];
}
},
onRowSelect() {
this.selectAll = this.selectedCustomers.length === this.totalRecords
},
onRowUnselect() {
this.selectAll = false;
}
}
}
<\/script>
2023-10-15 09:38:39 +00:00
2023-02-28 08:29:30 +00:00
`,
2023-09-22 12:54:14 +00:00
composition: `
<template>
2023-02-28 08:29:30 +00:00
<div class="card p-fluid">
2023-11-06 10:02:17 +00:00
<DataTable :value="customers" lazy paginator :first="first" :rows="10" v-model:filters="filters" ref="dt" dataKey="id"
2023-02-28 08:29:30 +00:00
:totalRecords="totalRecords" :loading="loading" @page="onPage($event)" @sort="onSort($event)" @filter="onFilter($event)" filterDisplay="row"
:globalFilterFields="['name','country.name', 'company', 'representative.name']"
v-model:selection="selectedCustomers" :selectAll="selectAll" @select-all-change="onSelectAllChange" @row-select="onRowSelect" @row-unselect="onRowUnselect" tableStyle="min-width: 75rem">
<Column selectionMode="multiple" headerStyle="width: 3rem"></Column>
<Column field="name" header="Name" filterMatchMode="startsWith" sortable>
<template #filter="{filterModel,filterCallback}">
<InputText type="text" v-model="filterModel.value" @keydown.enter="filterCallback()" class="p-column-filter" placeholder="Search"/>
</template>
</Column>
<Column field="country.name" header="Country" filterField="country.name" filterMatchMode="contains" sortable>
2023-11-06 10:02:17 +00:00
<template #body="{ data }">
2024-05-20 12:14:38 +00:00
<div class="flex items-center gap-2">
2023-11-06 10:02:17 +00:00
<img alt="flag" src="https://primefaces.org/cdn/primevue/images/flag/flag_placeholder.png" :class="\`flag flag-\${data.country.code}\`" style="width: 24px" />
<span>{{ data.country.name }}</span>
</div>
</template>
2023-02-28 08:29:30 +00:00
<template #filter="{filterModel,filterCallback}">
<InputText type="text" v-model="filterModel.value" @keydown.enter="filterCallback()" class="p-column-filter" placeholder="Search"/>
</template>
</Column>
<Column field="company" header="Company" filterMatchMode="contains" sortable>
<template #filter="{filterModel,filterCallback}">
<InputText type="text" v-model="filterModel.value" @keydown.enter="filterCallback()" class="p-column-filter" placeholder="Search"/>
</template>
</Column>
<Column field="representative.name" header="Representative" filterField="representative.name" sortable>
2023-11-06 10:02:17 +00:00
<template #body="{ data }">
2024-05-20 12:14:38 +00:00
<div class="flex items-center gap-2">
2023-11-06 10:02:17 +00:00
<img :alt="data.representative.name" :src="\`https://primefaces.org/cdn/primevue/images/avatar/\${data.representative.image}\`" style="width: 32px" />
<span>{{ data.representative.name }}</span>
</div>
</template>
2023-02-28 08:29:30 +00:00
<template #filter="{filterModel,filterCallback}">
<InputText type="text" v-model="filterModel.value" @keydown.enter="filterCallback()" class="p-column-filter" placeholder="Search"/>
</template>
</Column>
</DataTable>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
2023-03-09 07:54:55 +00:00
import { CustomerService } from '@/service/CustomerService';
2023-02-28 08:29:30 +00:00
onMounted(() => {
loading.value = true;
lazyParams.value = {
2023-12-31 14:08:33 +00:00
first: 0,
rows: 10,
2023-02-28 08:29:30 +00:00
sortField: null,
sortOrder: null,
filters: filters.value
};
loadLazyData();
});
const dt = ref();
const loading = ref(false);
const totalRecords = ref(0);
const customers = ref();
const selectedCustomers = ref();
const selectAll = ref(false);
2023-11-06 10:02:17 +00:00
const first = ref(0);
2023-02-28 08:29:30 +00:00
const filters = ref({
'name': {value: '', matchMode: 'contains'},
'country.name': {value: '', matchMode: 'contains'},
'company': {value: '', matchMode: 'contains'},
'representative.name': {value: '', matchMode: 'contains'},
});
const lazyParams = ref({});
const columns = ref([
{field: 'name', header: 'Name'},
{field: 'country.name', header: 'Country'},
{field: 'company', header: 'Company'},
{field: 'representative.name', header: 'Representative'}
]);
2023-11-06 10:02:17 +00:00
const loadLazyData = (event) => {
2023-02-28 08:29:30 +00:00
loading.value = true;
2023-11-06 10:02:17 +00:00
lazyParams.value = { ...lazyParams.value, first: event?.first || first.value };
2023-02-28 08:29:30 +00:00
setTimeout(() => {
2023-03-09 07:54:55 +00:00
CustomerService.getCustomers({ lazyEvent: JSON.stringify(lazyParams.value) }).then((data) => {
customers.value = data.customers;
totalRecords.value = data.totalRecords;
loading.value = false;
2023-02-28 08:29:30 +00:00
});
}, Math.random() * 1000 + 250);
};
const onPage = (event) => {
lazyParams.value = event;
2023-11-06 10:02:17 +00:00
loadLazyData(event);
2023-02-28 08:29:30 +00:00
};
const onSort = (event) => {
lazyParams.value = event;
2023-11-06 10:02:17 +00:00
loadLazyData(event);
2023-02-28 08:29:30 +00:00
};
2023-11-06 10:02:17 +00:00
const onFilter = (event) => {
2023-02-28 08:29:30 +00:00
lazyParams.value.filters = filters.value ;
2023-11-06 10:02:17 +00:00
loadLazyData(event);
2023-02-28 08:29:30 +00:00
};
const onSelectAllChange = (event) => {
2023-03-13 10:45:38 +00:00
selectAll.value = event.checked;
2023-02-28 08:29:30 +00:00
if (selectAll) {
2023-03-13 10:45:38 +00:00
CustomerService.getCustomers().then(data => {
2023-02-28 08:29:30 +00:00
selectAll.value = true;
selectedCustomers.value = data.customers;
});
}
else {
selectAll.value = false;
selectedCustomers.value = [];
}
};
const onRowSelect = () => {
selectAll.value = selectedCustomers.value.length === totalRecords.value;
};
const onRowUnselect = () => {
selectAll.value = false;
};
<\/script>
2023-03-10 08:19:45 +00:00
`,
data: `
{
id: 1000,
name: 'James Butt',
country: {
name: 'Algeria',
code: 'dz'
},
company: 'Benton, John B Jr',
date: '2015-09-13',
status: 'unqualified',
verified: true,
activity: 17,
representative: {
name: 'Ioni Bowcher',
image: 'ionibowcher.png'
},
balance: 70663
},
...`
2023-02-28 08:29:30 +00:00
}
};
},
2023-12-31 14:08:33 +00:00
methods: {
loadDemoData() {
this.loading = true;
2023-02-28 08:29:30 +00:00
2023-12-31 14:08:33 +00:00
this.lazyParams = {
first: 0,
rows: 10,
sortField: null,
sortOrder: null,
filters: this.filters
};
2023-02-28 08:29:30 +00:00
2023-12-31 14:08:33 +00:00
this.loadLazyData();
},
2023-11-06 10:02:17 +00:00
loadLazyData(event) {
2023-02-28 08:29:30 +00:00
this.loading = true;
2023-11-06 10:02:17 +00:00
this.lazyParams = { ...this.lazyParams, first: event?.first || this.first };
2023-02-28 08:29:30 +00:00
setTimeout(() => {
CustomerService.getCustomers({ lazyEvent: JSON.stringify(this.lazyParams) }).then((data) => {
this.customers = data.customers;
this.totalRecords = data.totalRecords;
this.loading = false;
});
}, Math.random() * 1000 + 250);
},
onPage(event) {
this.lazyParams = event;
2023-11-06 10:02:17 +00:00
this.loadLazyData(event);
2023-02-28 08:29:30 +00:00
},
onSort(event) {
this.lazyParams = event;
2023-11-06 10:02:17 +00:00
this.loadLazyData(event);
2023-02-28 08:29:30 +00:00
},
2023-11-06 10:02:17 +00:00
onFilter(event) {
2023-02-28 08:29:30 +00:00
this.lazyParams.filters = this.filters;
2023-11-06 10:02:17 +00:00
this.loadLazyData(event);
2023-02-28 08:29:30 +00:00
},
onSelectAllChange(event) {
const selectAll = event.checked;
if (selectAll) {
2023-03-13 10:45:38 +00:00
CustomerService.getCustomers().then((data) => {
2023-02-28 08:29:30 +00:00
this.selectAll = true;
this.selectedCustomers = data.customers;
});
} else {
this.selectAll = false;
this.selectedCustomers = [];
}
},
onRowSelect() {
this.selectAll = this.selectedCustomers.length === this.totalRecords;
},
onRowUnselect() {
this.selectAll = false;
}
}
};
</script>