Ember Sort my Records so the null for a column are retuned first

房东的猫 提交于 2019-12-11 15:49:07

问题


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.


回答1:


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'),



回答2:


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



来源:https://stackoverflow.com/questions/46705793/ember-sort-my-records-so-the-null-for-a-column-are-retuned-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!