Liquid - Filter Array Of Products Using Properties - shopify

I would like to filter an array of products using values from the properties field. Specifically so in the cart with line_items.
I am currently trying to use the where filter, to only include products with the property of "group-id". Ideally, this would be as easy as:
{% assign filtered_items = items | where: 'properties.group-id' %}
But from trying this, and searching around in the documentation and online, it doesn't look like others were able to get it to work.
Example 1
Example 2
Jekyll looks to have a where_exp filter which would be able to solve this nested issue if it were a part of Shopify Liquid.
I understand this can be implemented using a for loop with conditional checks, but I am curious about the possibility this method.

I wasn't able to use the where filter as I was hoping, but ended up solving the issue anyway.
In my use case, I needed to create sub-arrays based on a common property. This common property always showed up contiguously across items in the original array.
This made the situation a little easier. I ended up iterating through the original array, kept track of the relevant indices, and took slices of it using slice to create the sub-arrays.

Related

React-Admin filters that relate to the current results

We're really enjoying using the capabilities offered by React-Admin.
We're using <ReferenceArrayInput> to allow filtering of a <List> by Country. The drop-down contains all countries in the database.
But, we'd like it to just contain the countries that relate to the current set of filtered records.
So, in the context of the React-Admin demo, if we've filtered for Returned, then the Customer drop-down would only contain customers who have returned items (see below). This would make a real-difference in finding the records of interest.
Our current plan is to (somehow) handle this in our <DataProvider>. But, is there are more ReactAdmin friendly way of doing this?
So you want to build dependent filters, which is not a native feature of react-admin - and a complex beast to tame.
First, doing so in the dataProvider will not work, because you'll only have the data of the first page of results. A record in a following page may have another value for your array input.
You could implement that logic in a custom Input component instead. This component can wrap the original <ReferenceArrayInput> and read the current ListContext to get the current data and filter value (https://marmelab.com/react-admin/useListContext.html), then alter the array of possible values using the filter prop (https://marmelab.com/react-admin/ReferenceArrayInput.html#filter).

SRSS: Dynamic amount of subreports in a report

it might be possible I'm searching for the wrong keywords, but so far I couldn't find anything useful.
My problem is quite simple: At the moment I get a list of individual Ids through a report parameter, I pass them to a procedure and show the results.
The new request is like this: Instead of showing the list for all individuals at once, there should be a list for each individual id.
Since I'm quite a beginner in srss, I thought the easiest approach would be the best: Create a subreport, copy the shown list, and create a subreport per individual id.
The amount of this IDs is dynamic, so I have to create a dynamic amount of subreports.
Funny enought, this doesnt seem to be possible. This http://forums.asp.net/t/1397645.aspx url doesnt show exactly the problem, but it shows the limit of the subreports.
I even ran trough the whole msdn pages starting http://technet.microsoft.com/en-us/library/dd220581.aspx but I couldnt find anything there.
So is there a possibility, to create a loop like:
For each Individual ID in Individual IDs, create a subreport and pass ONE ID to this?
Or is there another approach I should use to make this work?
I tried to create a 'Fake'-Dataset with no sql query but just for iterating the id list, but it seems the dataset needs a data-source...
As usual, thanks so far for all answers!
Matthias Müller
Or is there another approach I should use to make this work?
You didn't provide much detail about what sort of information needs to be included in the subreport, but assuming it's a small amount of data (say, showing a personnel record), and not a huge amount (such as a persons sales for the last year), a List might be the way to go.
I tried to create a 'Fake'-Dataset with no sql query but just for iterating the id list, but it seems the dataset needs a data-source...
All datasets require a data source, though if you're merely hard-coding some fake return data, any data source will do, even a local SQL instance with nothing in it.

Many-to-many relationships with NSFetchedResultsController

I have a model that looks like this:
and want to display it in a UITableView using an NSFetchedResultsController. I want to list all of the Objects for a given Owner, grouped by Group. For example, if we were to view the groups for some Owner Owner A the table view might look like this:
Group A
Object A
Object B
Object C
Group B
Object A
Object D
Group C
Group D
Object C
Object E
It's also important that the Groups be ordered by their name attribute (not shown in the schema above) and that NSFetchedResultsController delegate methods get called whenever Objects are modified or added/removed from a Group.
Given those requirements, I have set up the NSFetchedResultsController to fetch a bunch of Objects with a predicate like [NSPredicate predicateWithFormat:#"ANY groups.owner = %#", someOwner], which throws an NSInvalidArgumentException exception: "to-many key not allowed here". I have tried a few other predicates, but am stuck.
Any suggestions? Is this not something I should be using an NSFetchedResultsController for? Or is there a better way to model my data?
Thanks!
Edit: I actually got the predicate working with the above code, my mistake was in my sectionNameKeyPath argument. I was trying to pass in groups.name, which was what was producing the error. I can see why that way wouldn't work, but am struggling to find a different way to achieve the desired results. Perhaps a join object?
Edit 2: This works somewhat well with a join object like this:
There are two downsides that I see now. The first is that I have to enforce uniqueness myself using code. That's easy enough, but a bit of a nuisance. The second downside, that I don't yet see a way around, is that the NSFetchedResultsController will not call its delegate for updates to Objects anymore. I can live with that downside for now, but am happy to hear better suggestions.
I think the problem you're going to see is that if you have a fetched results controller searching for Objects then it's going to find each Object only exactly once. So each object will appear exactly once in your table. Whereas what you sort of want to do is invert things and find all the Groups, then displaying all the relevant contained Objects. In terms of the fetched results controller, rather than finding rows and thereby being able to divide into sections you want to find sections and thereby figure out what to supply as rows.
The easiest thing, I think, would be to create a fetched results controller on Group and to add an intermediary object of your own that remaps those to sections and supplies group.objects (with a suitably deterministic sorting applied) as the rows per section.
If you want the rows to be dynamic then I guess the easiest thing is to create a fetched results controller per section based on the feedback of the Group controller.
If that's all getting a bit painful and you want to just write your own collection logic then you'll probably want to catch NSManagedObjectContextDidSaveNotification and rerun your logic whenever that occurs. For memory efficiency reasons you probably want to keep hold only of the objectIDs and to get the appropriate objects only when the table view requests them via the existingObjectWithID:error: method on the context. Core Data has a built-in caching mechanism that responds appropriately to memory warnings so that's all written for you.
You shouldn't need a join object to get at data in Many to Many relationships
See this answer: How to deal with many to many relationships with NSFetchedResultsController?
And example project: https://github.com/dmathewwws/Many-to-Many-CoreData

Distance group NSFetchedResultsController sections

I'm using Core Data for a tableview. My data is ordered by the distance from your current location. What I'd like is to have a new section for items with 5, 10 and 20 miles.
My distance value is stored in the data store as an NSInteger and I get it out using a NSNumber in my object model.
I've done a bit of searching around and found that I need to use the sectionNameKeyPath attribute to make the data sectioned.
My problem is that I don't know the best way to group the data. During my searches I came across either a transient property or using a category of NSNumber to work out which section the item should be in.
Are either of these methods the best way of getting my end result, if so, can anyone provide any details on how to implement it?
Thanks.
You should add a derived attribute to the object (it's not strictly needed in the model), let's name it range. I'd make it a read only property, you can cache the value or not.
When it's 0-5, return 5, 5.x - 10 return 10, etc.
Then set range as your sectionNameKeyPath.
If you want a highly customized section behavior, you need to subclass NSFetchedResultsController to give you the behavior you want. See the NSFetchedResultsController class docs for details.
You will need to subclass in this case because your not looking at a single ordered attribute like the alphabetized first letter of a string attribute but rather a range in which each attribute falls into.
This is a cleaner solution than altering the data model because you can use it to display the data many different ways in many different tableviews without having to muddy up your data model.

Find SUM of an array of managed objects

I have a big problem with taking a simple Total Cost from an array!
I have an entity called currentCost, it contains two attributes: costAmount and costDesc. I bind it to an array controller and I have no problem when I add and remove items to and from this entity. The problem is to get the Total Cost from the costAmount attributes and show it in a text field.
I think I have to fetch all costAmounts to another array and take the total of them, but it seems I don't know how to do this! What is the best approach ?
Thank you in advance.
Bind the text field to the array controller and specify #sum.costAmount as the key path.