sort and pagination added (DataView)

pull/12/head
Merve Özçifçi 2019-05-07 15:54:32 +03:00
parent dbb5146eb8
commit 0f4b1f09a8
2 changed files with 66 additions and 16 deletions

View File

@ -3,18 +3,18 @@
<div class="p-dataview-header" v-if="$scopedSlots.header"> <div class="p-dataview-header" v-if="$scopedSlots.header">
<slot name="header"></slot> <slot name="header"></slot>
</div> </div>
<Paginator v-if="paginatorTop" :rows="rows" :first="first" :totalRecords="getTotalRecords" :pageLinkSize="pageLinkSize" :template="paginatorTemplate" :rowsPerPageOptions="rowsPerPageOptions" <Paginator v-if="paginatorTop" :rows.sync="rows" :first.sync="firstPage" :totalRecords="getTotalRecords" :pageLinkSize="pageLinkSize" :template="paginatorTemplate" :rowsPerPageOptions="rowsPerPageOptions"
:currentPageReportTemplate="currentPageReportTemplate" :class="{'p-paginator-top': paginatorTop}"></Paginator> :currentPageReportTemplate="currentPageReportTemplate" :class="{'p-paginator-top': paginatorTop}"></Paginator>
<div class="p-dataview-content"> <div class="p-dataview-content">
<div class="p-grid"> <div class="p-grid">
<template v-for="(data,index) of (value)"> <template v-for="(data,index) of (templateItems)">
<slot v-if="$scopedSlots.listItem && layout === 'list'" name="listItem" :data="data" :index="index"></slot> <slot v-if="$scopedSlots.listItem && layout === 'list'" name="listItem" :data="data" :index="index"></slot>
<slot v-if="$scopedSlots.gridItem && layout === 'grid'" name="gridItem" :data="data" :index="index"></slot> <slot v-if="$scopedSlots.gridItem && layout === 'grid'" name="gridItem" :data="data" :index="index"></slot>
</template> </template>
<div v-if="isEmpty" class="p-col-12">{{emptyMessage}}</div> <div v-if="isEmpty" class="p-col-12">{{emptyMessage}}</div>
</div> </div>
</div> </div>
<Paginator v-if="paginatorBottom" :rows="rows" :first="first" :totalRecords="getTotalRecords" :pageLinkSize="pageLinkSize" :template="paginatorTemplate" :rowsPerPageOptions="rowsPerPageOptions" <Paginator v-if="paginatorBottom" :rows.sync="rows" :first.sync="firstPage" :totalRecords="getTotalRecords" :pageLinkSize="pageLinkSize" :template="paginatorTemplate" :rowsPerPageOptions="rowsPerPageOptions"
:currentPageReportTemplate="currentPageReportTemplate" :class="{'p-paginator-bottom': paginatorBottom}"></Paginator> :currentPageReportTemplate="currentPageReportTemplate" :class="{'p-paginator-bottom': paginatorBottom}"></Paginator>
<div class="p-dataview-footer" v-if="$scopedSlots.footer"> <div class="p-dataview-footer" v-if="$scopedSlots.footer">
<slot name="footer"></slot> <slot name="footer"></slot>
@ -90,7 +90,23 @@
default: false default: false
} }
}, },
data() {
return {
firstPage: this.first ? this.first : 0
}
},
methods: { methods: {
processData() {
let data = this.value;
if (data && data.length) {
if (this.sortField) {
data = this.sort();
}
}
return data;
},
sort() { sort() {
if (this.value) { if (this.value) {
const value = [...this.value]; const value = [...this.value];
@ -119,17 +135,6 @@
else { else {
return null; return null;
} }
},
processData() {
let data = this.value;
if (data && data.length) {
if (this.sortField) {
data = this.sort();
}
}
return data;
} }
}, },
computed: { computed: {
@ -162,6 +167,27 @@
} }
else else
return null; return null;
},
templateItems (){
let value = this.processData();
if (value && value.length) {
if (this.paginator) {
const rows = this.rows;
const first = this.lazy ? 0 : this.firstPage;
const last = rows + first;
let items = [];
for (let i = first; i < last; i++) {
items.push(value[i]);
}
return items;
}
else {
return value;
}
}
else return null;
} }
} }
} }

View File

@ -9,10 +9,12 @@
<div class="content-section implementation"> <div class="content-section implementation">
<h3 class="first">Default</h3> <h3 class="first">Default</h3>
<DataView :value="cars" :layout="layout" paginatorPosition='both' :paginator="true" :rows="20"> <DataView :value="cars" :layout="layout" paginatorPosition='both' :paginator="true" :rows="5" :sortOrder="sortOrder" :sortField="sortField">
<template #header> <template #header>
<div class="p-grid"> <div class="p-grid">
<div class="p-col-6" style="text-align: right"></div> <div class="p-col-6" style="text-align: left">
<Dropdown v-model="sortKey" :options="sortOptions" optionLabel="label" placeholder="Sort By" @change="onSortChange($event)"/>
</div>
<div class="p-col-6" style="text-align: right"> <div class="p-col-6" style="text-align: right">
<DataViewLayoutOptions :layout="layout" @change="changeMode"></DataViewLayoutOptions> <DataViewLayoutOptions :layout="layout" @change="changeMode"></DataViewLayoutOptions>
</div> </div>
@ -61,6 +63,14 @@
return { return {
cars: null, cars: null,
layout: 'list', layout: 'list',
sortKey: null,
sortOrder: null,
sortField: null,
sortOptions: [
{label: 'Newest First', value: '!year'},
{label: 'Oldest First', value: 'year'},
{label: 'Brand', value: 'brand'}
]
} }
}, },
carService: null, carService: null,
@ -73,6 +83,20 @@
methods: { methods: {
changeMode(event) { changeMode(event) {
this.layout = event.value; this.layout = event.value;
},
onSortChange(event){
const value = event.value.value;
if (value.indexOf('!') === 0) {
this.sortOrder = -1;
this.sortField = value.substring(1, value.length);
this.sortKey = value;
}
else {
this.sortOrder = 1;
this.sortField = value;
this.sortKey = value;
}
} }
}, },
components: { components: {