Is it possible to sort the content of an ArrayController by both multiple fields and multiple directions? Would this work, trying a sort like "lastName ASC, zip DESC":
App.TableController = Ember.ArrayController.extend({
sortProperties: ['lastName', 'zip'],
sortAscending: [true, false]
})
I searched posts here and through the Ember docs but couldn't find any information on multiple 'sortAscending' options.
Bryan
Unfortunately, it's not possible, as noted by this discussion. You'd have to write your own sorting function to do it.
Related
I want to filter and then sort my ember records such that the null records for a column are returned first. I'm not much familiar with ember and have a rails back ground.
filteredByData = myLeads.filterBy('status', 'open').filterBy('doNotCall', false).filterBy('phoneValid', true)
filtered = filteredByData.sortBy 'last_dialed_at', 'last_name', 'first_name', 'id'
Right now, my records are getting ordered according to the id.
What i want is to implement the NULLS LAST/FIRST from sql or postgres in any query (such a s SELECT * FROM t1 ORDER BY c1 DESC NULLS FIRST here in my sort so i get the records such that the last_dialed_atnull records come first.
I know the same can be implemented in rails like
Foo.order('last_dialed_at DESC NULLS FIRST') but i have no idea for same in Ember
Any help will be appreciated. Thanks in Advance.
P.S: I'll try to create a ember twiddle meanwhile to explain myself better.
Although you can resort to a SortableMixin/ArrayProxy, those are lower-level APIs than you should really need to use. sortBy works fine for a single value, but I generally use computed.sort when I need to sort multiple things. Take a look at the code and link in this answer https://stackoverflow.com/a/46788377/334913 they should get you going.
One of the common ways Ember suggests doing things is to do computed properties for each step (or set of steps) as it makes it far easier to reason about.
So I would do something along the lines of:
filteredLeads: Ember.computed('leads', function() {
return filteredResultsHere;
}),
sortDefinition: [], // put your entries from your SortableMixin here
sortedFilteredLeads: Ember.computed.sort('filteredLeads', 'sortDefinition'),
I solved my issue by using the Ember.SortableMixin instead of sortBy. I ended up with the following code and this worked perfectly for me.
leads = Ember.ArrayProxy.createWithMixins Ember.SortableMixin, content: filteredByData, sortProperties: ['lastDialedAt', 'firstName', 'lastName', 'id'], sortAscending: true
I'm not sure or much aware why this worked and the fundamentals to an extent that i can explain this behaviour. If there's any explanation for same from anyone, please feel free to put your answer below and i'll accept it as the answer.
Thanks
I am trying to do a very basic query from the explorer which looks like this:
var sensorFilter = [];
sensorFilter.push({
property_name: "uuid",
operator: "eq",
property_value: "1234"
});
var avg_sensor_pm = new Keen.Query("average", {
eventCollection: "status_update",
targetProperty: "sensors[0].properties[0].value",
filters: sensorFilter
});
The query returns nothing, I am wondering if the syntax to access indexed element in an array is not correct?
Anybody with the same problem?
The problem is that doing averages on values within an arrays is technically hard. I would recommend sending separate events for each status_update for each sensor and then doing an average on sensor.value.
You can definitely store arrays in Keen IO, but arrays of objects are not recommended. For more info: https://keen.io/docs/streams/data-modeling-guide/#arrays
Since I don't know anything about your data model, I should say that there might be other ways to do it too. Feel free to share it if the way I suggested wouldn't work for your use case.
Also this may be helpful: https://keen.io/docs/streams/data-modeling-guide-200/#avoid-trapping-your-data-lists
I have a list of Comments. These comments have an attribute called "vote" (users can vote comments) and they are initially sorted by votes (descending, onRender).
Right now, when users vote, the order of the comments is reactively updated to reflect the new votes.
Is it possible to somehow keep the initial sorting order intact? I would like to avoid confusing the user with comments automatically swapping order while she/he is on the page.
Is there any good way to solve this? I was thinking perhaps of a one-time sort when rendering the page, or somehow saving the order and then reapplying it whenever the collection is reactively refreshed.
You could use a function to sort your Minimongo query. So something like:
const initialVotes = new Map();
Comments.find({}, {sort: (a, b) => {
if (!initialVotes.has(a._id)) initialVotes.set(a._id, a.votes);
if (!initialVotes.has(b._id)) initialVotes.set(b._id, b.votes);
return initialVotes.get(b._id) - initialVotes.get(a._id);
});
This will make it so that comments are sorted by initial votes. If anything else changes (like user edits the comments), that will reactively propagate, if a new comment is made, it will reactively be added. But if votes change, order will not change (but the vote number maybe rendered will still update).
You can do a non-reactive find with the reactive: false option, ex:
Comments.find({},{sort: {vote: -1}, reactive: false});
If you want to append new comments after the original list then I would render two lists of comments, one right after the other. First render the sorted list of existing comments then reactively render the list of comments that were created after the time of initial rendering in creation order. The second list will initially be empty and will not render at all but eventually new comments will appear there with their own vote counts.
Now, since the cursor above is non-reactive, how do we go about getting the vote counts to update reactively without changing the order? Answer: (in the case of Blaze) use a reactive helper!
Template.myTemplate.helpers({
currentVote(){
return Comments.findOne(this._id).vote;
}
});
This is actually surprisingly cheap from a performance pov.
since you don't care about new Comments, only new votes, i would do the initial population with a Meteor.call() sorted the way you want, as mentioned by #sdybskiy. then i would set up a subscription for those comments to get the vote count.
in the onCreated() of your template, you could set up a subscription like this:
let voteCursor = Votes.find({commentIds: comments});
voteCursor.observe({
added: function(newDocument, oldDocument) {
// the published vote would have a commentId, so here you
// would go to your client store for the comments, find the
// comment, and increment the count
},
removed: function(oldDocument) {
// same idea here, but process a vote being removed
}
});
notice i'm passing in the ids of all the comments returned from the method, so the publish would publish the votes only for those comments.
I have domain object named Roll and on the list page i want to show the user all the Roll objects iterating through a list, sorted by entry date.
Here is the code i am using
[rollList: Roll.findAll(sort:"rollDate"){userid==uid}]
rollDate is a field inside the Roll object with data type java.util.Date
Any suggestion on why the output is not sorted by rollDate. When i iterate through the rollList on the gsp page it is not sorted.
Also, on the Roll domain object i have even put this code, but it's still not sorting.
static mapping = {
sort "rollDate"
}
Thank you.
Why aren't you using the dynamic finders?
Roll.findAllByUserid( uid, [ sort:"rollDate", order: 'desc'] )
should work.
The findAll( Map, Closure ) method appeared not a long time ago, perhaps it was not tested well...
You might need to use order in your query as well, then add order to it
[rollList: Roll.findAll(sort:"rollDate", order: 'desc'){userid==uid}]
After trying both the solutions mentioned it still didn't work. So i thought something might be wrong on the front end.
While researching more i found that since i was using jquery data tables it used to re order the sorting. The solutions i found was here
jQuery DataTable rows order
So both the answers above are correct. The issue was actually with jquery data tables.
I have a collection of data that has a collection called Approvals in it. The Approvals object has a property call ApplicationName. I want a list of all the Distinct Approvals ApplicationNames. So if I have a list of Approvals with applicationNames of Nick, Nick, Jack, Daniel I want to return the whole approval object of Nick, Jack, Daniel. Or at least just a list of the names Nick, Jack, Daniel
This isnt working for me..
theApplicationNames = theData.Approvals.Select(c => new WebsiteApplicationInfo
{
Name=c.ApplicationName,
ID=c.ApplicationId
}).Distinct().ToList();
If you want a list of the distinct names, you would use
var names = theData.Approvals.Select(a => a.ApplicationName).Distinct();
This would result in a list of all the names (presumably strings?).
One likely reason your version "isn't working" for you is that you are using .Distinct() on a collection of WebsiteApplicationInfo objects, which might not have a correct equality comparison implemented. Without knowing what you mean by "isn't working", though, we can't really be sure.
theData.Approvals.Select(a => a.ApplicationName).Distinct();
If you project a single property, you can just distinct on that. If you are projecting anonymous types, you either need to use the GroupBy trick or create a custom IComparer that you pass to the Distinct. You should be able to find quite a few solutions using the following search: https://stackoverflow.com/search?q=distinctby&submit=search