Wanted to see what others are using to sort a list of things by their value i.e. name . I have collection rendered to the page that needs to be sorted by things like price, length, and year during a click event.
Thanks!
Wanted to see what others are using to sort a list of things by their value i.e. name . I have collection rendered to the page that needs to be sorted by things like price, length, and year during a click event.
Thanks!
I just pass a sort to the mongo query. Things like {sort: {price: 1}}
in the options parameter of mongo (or minimongo) find gives you an ordered list, and changing that query within an autorun causes the output to be reordered in that context, so you can do that in a blaze template, and the blaze template will reorder if you change the sort rule based on a reactive source.
I did try that approach , but using dynamic sort stored as a variable however it is not reactive. and I really rather not have a large amount of if/else statements and handle this via the client as all the data is displayed at a single time.
https://docs.meteor.com/api/reactive-var.html gives you a reactive variable to store that in.
As an example I have a helper in one of my projects:
logs(){
let sort = {};
Template.instance().sortColumn.get().forEach(column => {
sort[column.column] = column.direction;
});
return Log.Collection.find({}, {sort});
},
sortColumn is a reactiveVar on the template instance which stores an array of objects which have a column and a direction integer. Event code updates it.
I’ll give that approach with the loop this weekend. I am kind of curious though, So I have the initial rendering of the collection which displays everything. The based on a click event I update/add a sort event, is this sorted by injection the new set of parameters, I could kind of see one if/else here. I had the problem at [quote=“lassombra, post:4, topic:33492”]
sort[column.column] = column.direction;
[/quote]
which did not react.