2

original data:

[
{
    "SHFUserID": "400",
    "AlertID": "12",
    "TickerID": "4512",
    "Ticker": "GOOG",
    "Active": "1",
    "Status": null
},
{
    "SHFUserID": "400",
    "AlertID": null,
    "TickerID": "4512",
    "Ticker": "GOOG",
    "Active": null,
    "Status": null
},
{
    "SHFUserID": "400",
    "AlertID": null,
    "TickerID": "10190",
    "Ticker": "ABM",
    "Active": null,
    "Status": null
},
{
    "SHFUserID": "400",
    "AlertID": null,
    "TickerID": "712",
    "Ticker": "DPAX",
    "Active": null,
    "Status": "1"
}

]

uniqueTickers filter:

.filter('uniqueTickers', function() {
return function(tickers) {
    var tags = {};
    angular.forEach(tickers, function(obj) {

  if(!(obj.Ticker in tags)){
        tags[obj.Ticker] = {id: obj.TickerID, name:obj.Ticker};

    if(!tags[obj.Ticker].pending){
      tags[obj.Ticker].pending = 0;
    }
    if(!tags[obj.Ticker].settled){
      tags[obj.Ticker].settled = 0;
    }
    if(!tags[obj.Ticker].order){
      tags[obj.Ticker].order = 3;
    }
  }

  if(obj.Status === "1"){
    tags[obj.Ticker].pending = 1;
    if(tags[obj.Ticker].order > 2){
      tags[obj.Ticker].order = 2;
    }
  }
  if(obj.Status === "2"){
    tags[obj.Ticker].settled = 1;
    if(tags[obj.Ticker].order > 1){
      tags[obj.Ticker].order = 1;
    }
  };
    });
return tags;
};

resulting data:

{"id":"10190","name":"ABM","pending":0,"settled":0,"order":3}
{"id":"712","name":"DPAX","pending":1,"settled":0,"order":2}
{"id":"4512","name":"GOOG","pending":0,"settled":0,"order":3}

html:

Search: <input ng-model="query.Ticker">
  Sort by:
  <select ng-model="orderProp">
    <option value="name">Alphabetical</option>
    <option value="order">Alert Status</option>
  </select>
  <ul class="tickerList">
    <li ng-repeat="ticker in tickers | filter:query | uniqueTickers | orderBy:orderProp">
      <a href="#/ticker/{{ticker.id}}">{{ticker.name}}</a>
      <p>{{ticker}}</p>
    </li>
  </ul>

I am trying to filter alphabetically by "name" or by "order", but the orderBy filters do not work once I run the uniqueTickers filter. I am not sure what I need to expose to orderBy to make this work.

1 Answer 1

1

You are currently exposing an object to orderBy, this should probably be an array.

Try replacing return tags; at the end of your uniqueTickers filter with the following:

var arr = [];
for (var ticker in tags) {
    arr.push(tags[ticker]);
}
return arr;
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I actually don't know much about angular I just noticed that the documentation for orderBy indicates that it operates on arrays. My guess is that most if not all filters will require arrays.
i am new to javascript, i assumed arrays and objects were interchangeable in javascript. i now know they are not. thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.