Date filters

pull/973/head
Cagatay Civici 2021-02-08 15:35:16 +03:00
parent 4983330e7a
commit baacf46d6b
4 changed files with 67 additions and 31 deletions

View File

@ -11,10 +11,10 @@ const FilterMatchMode = {
GREATER_THAN : 'gt', GREATER_THAN : 'gt',
GREATER_THAN_OR_EQUAL_TO : 'gte', GREATER_THAN_OR_EQUAL_TO : 'gte',
BETWEEN : 'between', BETWEEN : 'between',
IS : 'is', DATE_IS : 'dateIs',
IS_NOT : 'isNot', DATE_IS_NOT : 'dateIsNot',
BEFORE : 'before', DATE_BEFORE : 'dateBefore',
AFTER : 'after' DATE_AFTER : 'dateAfter'
} }
export default FilterMatchMode; export default FilterMatchMode;

View File

@ -169,8 +169,8 @@ const FilterService = {
else else
return value >= filter; return value >= filter;
}, },
is(value, filter, filterLocale) { dateIs(value, filter) {
if (filter === undefined || filter === null || (typeof filter === 'string' && filter.trim() === '')) { if (filter === undefined || filter === null) {
return true; return true;
} }
@ -178,22 +178,42 @@ const FilterService = {
return false; return false;
} }
if (value.getTime && filter.getTime) return value.toDateString() === filter.toDateString();
return value.getTime() === filter.getTime();
else
return ObjectUtils.removeAccents(value.toString()).toLocaleLowerCase(filterLocale) == ObjectUtils.removeAccents(filter.toString()).toLocaleLowerCase(filterLocale);
}, },
isNot(value, filter, filterLocale) { dateIsNot(value, filter) {
return this.filters.notEquals(value, filter, filterLocale); if (filter === undefined || filter === null) {
return true;
}
if (value === undefined || value === null) {
return false;
}
return value.toDateString() !== filter.toDateString();
}, },
before(value, filter, filterLocale) { dateBefore(value, filter) {
return this.filters.lt(value, filter, filterLocale); if (filter === undefined || filter === null) {
return true;
}
if (value === undefined || value === null) {
return false;
}
return value.getTime() < filter.getTime();
}, },
after(value, filter, filterLocale) { dateAfter(value, filter) {
return this.filters.gt(value, filter, filterLocale); if (filter === undefined || filter === null) {
return true;
}
if (value === undefined || value === null) {
return false;
}
return value.getTime() > filter.getTime();
} }
}, },
register(rule, fn) { register(rule, fn) {
this.filters[rule] = fn; this.filters[rule] = fn;
} }

View File

@ -15,10 +15,10 @@ const defaultOptions = {
lte: 'Less than or equal to', lte: 'Less than or equal to',
gt: 'Greater than', gt: 'Greater than',
gte: 'Greater than or equal to', gte: 'Greater than or equal to',
is: 'Is', dateIs: 'Date is',
isNot: 'Is not', dateIsNot: 'Date is not',
before: 'Before', dateBefore: 'Date is before',
after: 'After', dateAfter: 'Date is after',
clear: 'Clear', clear: 'Clear',
apply: 'Apply', apply: 'Apply',
matchAll: 'Match All', matchAll: 'Match All',
@ -62,10 +62,10 @@ const defaultOptions = {
FilterMatchMode.GREATER_THAN_OR_EQUAL_TO FilterMatchMode.GREATER_THAN_OR_EQUAL_TO
], ],
date: [ date: [
FilterMatchMode.IS, FilterMatchMode.DATE_IS,
FilterMatchMode.IS_NOT, FilterMatchMode.DATE_IS_NOT,
FilterMatchMode.BEFORE, FilterMatchMode.DATE_BEFORE,
FilterMatchMode.AFTER FilterMatchMode.DATE_AFTER
] ]
} }
}; };

View File

@ -78,7 +78,7 @@
<Column header="Date" filterField="date" dataType="date"> <Column header="Date" filterField="date" dataType="date">
<template #body="{data}"> <template #body="{data}">
<span class="p-column-title">Date</span> <span class="p-column-title">Date</span>
{{data.date}} {{formatDate(data.date)}}
</template> </template>
<template #filter="{filterModel}"> <template #filter="{filterModel}">
<Calendar v-model="filterModel.value" dateFormat="mm/dd/yy" /> <Calendar v-model="filterModel.value" dateFormat="mm/dd/yy" />
@ -275,10 +275,26 @@ export default {
this.initFilters1(); this.initFilters1();
}, },
mounted() { mounted() {
this.customerService.getCustomersLarge().then(data => {this.customers1 = data; this.loading1 = false;}); this.customerService.getCustomersLarge().then(data => {
this.customerService.getCustomersLarge().then(data => {this.customers2 = data; this.loading2 = false;}); this.customers1 = data;
this.loading1 = false;
this.customers1.forEach(customer => customer.date = new Date(customer.date));
});
this.customerService.getCustomersLarge().then(data => {
this.customers2 = data;
this.loading2 = false;
this.customers2.forEach(customer => customer.date = new Date(customer.date));
});
}, },
methods: { methods: {
formatDate(value) {
return value.toLocaleDateString('en-US', {
day: '2-digit',
month: '2-digit',
year: 'numeric',
});
},
formatCurrency(value) { formatCurrency(value) {
return value.toLocaleString('en-US', {style: 'currency', currency: 'USD'}); return value.toLocaleString('en-US', {style: 'currency', currency: 'USD'});
}, },
@ -291,7 +307,7 @@ export default {
'name': {operator: FilterOperator.AND, constraints: [{value: null, matchMode: FilterMatchMode.STARTS_WITH}]}, 'name': {operator: FilterOperator.AND, constraints: [{value: null, matchMode: FilterMatchMode.STARTS_WITH}]},
'country.name': {operator: FilterOperator.AND, constraints: [{value: null, matchMode: FilterMatchMode.STARTS_WITH}]}, 'country.name': {operator: FilterOperator.AND, constraints: [{value: null, matchMode: FilterMatchMode.STARTS_WITH}]},
'representative': {value: null, matchMode: FilterMatchMode.IN}, 'representative': {value: null, matchMode: FilterMatchMode.IN},
'date': {operator: FilterOperator.AND, constraints: [{value: null, matchMode: FilterMatchMode.IS}]}, 'date': {operator: FilterOperator.AND, constraints: [{value: null, matchMode: FilterMatchMode.DATE_IS}]},
'balance': {operator: FilterOperator.AND, constraints: [{value: null, matchMode: FilterMatchMode.EQUALS}]}, 'balance': {operator: FilterOperator.AND, constraints: [{value: null, matchMode: FilterMatchMode.EQUALS}]},
'status': {operator: FilterOperator.OR, constraints: [{value: null, matchMode: FilterMatchMode.EQUALS}]}, 'status': {operator: FilterOperator.OR, constraints: [{value: null, matchMode: FilterMatchMode.EQUALS}]},
'activity': {value: null, matchMode: FilterMatchMode.BETWEEN}, 'activity': {value: null, matchMode: FilterMatchMode.BETWEEN},