Change sort based on model condition

I recently came upon an interesting requirement. Users wanted the ability to filter for items in the past and items in the future. But they wanted the sorting to change based on the filter the user chose. I was unable to find the option to change the sorting based on a filter or even to run a snippet after selecting a filter. I’m also unable to add a unique id to a filter (so I can target it via javascript.) Either way, here’s the workaround I created in order to accomplish this.

There are some flaws with this approach, the main one being that if there are multiple dropdown filters this will target all of them and then change the sort. but in my case there is only a single dropdown.

Figured I’d post in case anyone else has a need for 

Here’s the entire inline snippet.

(function(skuid){<br /> var $ = skuid&#46;$; var activateHandler = function(){ skuid&#46;$("#table &#46;nx-conditioncontainer &#46;nx-actionselect-arrow")&#46;on("click", function() { setTimeout(function(){ skuid&#46;$("#table &#46;nx-conditioncontainer &#46;nx-actionselect-dropdown &#46;nx-actionselect-dropdown-item")&#46;on("mousedown" /* use mousedown because it happens before "click" */, function() { var text = $(this)&#46;text(), m = skuid&#46;$M("model-name"); switch (text) { case "Upcoming" : m&#46;orderByClause = "Date__c"; &#47;&#47;change the sort to show closest to current date first&#46; break; default: m&#46;orderByClause = "Date__c DESC"; break; } }); }) }); }; $(document&#46;body)&#46;one('pageload', activateHandler); skuid&#46;events&#46;subscribe("models&#46;loaded", activateHandler); })(skuid);


Feel free to post thoughts/suggestions.