This is part 3 of the ionic 5 shopping app and we are going to add product sorting and searching in this app.
We will use following pages here
Our purpose is to maintain the sorting like when we filter the data it should be sorted as before.
Go to src/app/tab1
(Product listing page), we have added a sort icon before the filter icon and above the product listing. It will also ensure that if sorting is already applied, it will show a filled icon otherwise the outline icon will be there.
isSorted() { return Object.values(this.sort).some(el => el); }
When user click on the sort icon, it will open the ion-action-sheet with available sorting options:
After selecting one of the options from the above list, it will go to the product service applyLocalSort
method.
applyLocalSort ( column, order, type ) { this.uncheckSorts(); this.sort[type] = true; console.log('column :>> ', column); this.beforeSort = this.products; this.products = this.beforeSort.sort((a, b) => { console.log('sort :>> ', a, b); if ( order === 'desc' ) { return a[column] > b[column]; } else { return a[column] < b[column]; } }); this.showResultCount(); }
Go to src/app/tab1
(Product listing page), we have already added an ion-searchbar there and now it is working.
<ion-searchbar mode="ios" (ionChange)="search($event)" debounce="500" style="padding: 16px 0px;"></ion-searchbar>
You can also integrate a common search page here but for this tutorial we are using this search box only.
On each search we are using product service method called searchProducts
with three product criteria,
The product searching will work in a sequence that if the searched term is not found in the name then it will move to category and then to price.
searchProducts( term: string ) { this.products = []; if ( `${term}`.trim() ) { let NotFoundInName = this.allProducts.map(item => { if ( item.name.toLocaleLowerCase().indexOf(term.toLocaleLowerCase()) < 0 ) { return item; } else { this.products.push(item); } }); console.log({NotFoundInName}); let NotFoundInCategory = NotFoundInName.map(item => { if (item) { if ( item.category.toLocaleLowerCase().indexOf(term.toLocaleLowerCase()) < 0 ) { return item; } else { this.products.push(item); } } }); console.log({NotFoundInCategory}); let foundInPrice = NotFoundInCategory.map(item => { if (item) { if ( `${item.price}`.toLocaleLowerCase().indexOf(term.toLocaleLowerCase()) > -1 ) { this.products.push(item); } } }); } else { this.products = this.allProducts; } this.showResultCount(); }
With this approach it will need to process less data, only the data which is not matched with searched Term.
Hope you enjoy the tutorial.
See you in the next learning journey.
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…