I've been trying to learn AngularJS to prototype new concepts and designs for an ecommerce site. I'm currently trying to create a product search page with a series of filters based on different product attributes.
Initially the filter groups would consist of:
- Brand (single string value)
- Category (array with id and string)
- Labels (array with zero or more values, max 5)
- Callouts (array with zero or more values, max 5)
However this might change so ideally the solution would be abstracted to allow for new groups to be listed and filtered without having to rewrite the controller completely.
The values from each filter set would be listed as checkboxes in the sidebar and de-duplicated to only show unique values and ideally also count for how many products are in each.
I started using some of the code from Filter Array Using Checkboxes with AngularJS however it doesn't seem that scalable in terms of having a variety of different facets/filter groups and doesn't work with values in arrays.
Is there a way to abstract this solution into two different services? E.g.
- A service to list the available values for the filter
- A custom filter that would accept the attribute and value to filter by which you could use to add multiple instances of that custom filter.
My main obstacle at the moment is finding a practical way to iterate through the arrays and only show unique values, whilst at the same time getting a correct count. I tried to cheat by manually listing the filter options but couldn't get the count to work. Ideally I'm looking for a solution that fetches the filter values from JSON than hardcoded in the view.
The product JSON is structured as follows but can be modified if it makes things easier.
{
"Category": ["g122", "Side by Side Refrigerators"],
"Brand": "LG",
"ModelNo": "GC-P197DPSL",
"Sku": "50001129",
"Heading": "567L Side by Side Refrigerator",
"ShortDesc": "The LG 567L Side-by-Side Platinum Refrigerator with One Touch Home Bar offers a huge amount of storage capacity making it perfect for family sized homes!",
"Labels": ["New", "As Advertised"],
"Callout": ["Bonus Offer", "Sale"],
"Price": 1399,
"WasPrice": 1650,
"DeliveryTier": "4",
"Image": "50001129_62296"
},
{
"Category": ["g186", "LED TVs"],
"Brand": "LG",
"ModelNo": "55LA6620",
"Sku": "50017349",
"Heading": "55\"(139cm) FHD LED LCD 100Hz 3D Smart TV",
"ShortDesc": "The LG 55\" FHD LED LCD is a Smart TV for smart people. The LG Smart TV is a class above with dual core processor, virtual surround, LED plus and magic remote control with voice recognition.",
"Labels": ["New", "As Advertised"],
"Callout": ["Bonus Offer", "Sale"],
"Price": 1389,
"WasPrice": 1599,
"DeliveryTier": "4",
"Image": "50017349_87400"
}
Here's a JSfiddle of my progress so far http://jsfiddle.net/LifeOnLars/HWa2m/24/
Any help would be appreciated.