0

GOAL : Setup a table that is sortable after data is returned from our server.

Data Format Returned from server :

{
    "result" : {
        "code" : "OK",
        "msg" : ""
    },
    "data" : [{
            "row" : ["Fred", "64233", "197"]
        }, {
            "row" : ["Steve", "158879", "36"]
        }, {
            "row" : ["Ted", "115222", "12"]
        }
    ]
}

Limitation : Can't change the data format the server returns. The data that I want sortable is contained in the array in the "row" object. I tried doing things like

<tr ng-repeat="roll in myWelcome.data" | orderBy:'roll.row[1]'">
   <td>{{ roll.row[0] }}</td><td>{{ roll.row[1] }}</td><td>{{ roll.row[2] }}</td>
</tr>

but it didn't work. Any ideas?

3
  • "but it didn't work" What does that mean? Commented Jan 26, 2016 at 19:07
  • not sure that orderBy will work without being able to pass it an object property name. Does code work without orderBy? Probably have to create custom filter for ordering Commented Jan 26, 2016 at 19:16
  • The data displays correctly in the table, but is not sorted. Commented Jan 26, 2016 at 19:20

2 Answers 2

2

I would recommend organizing your data when it's returned from the server to make it more usable. Failing that, you could simply implement a custom filter that knows where to look.

<tr ng-repeat="roll in myWelcome.data | customOrderBy:1">

Filter:

angular.module("Your_App").filter("customOrderBy", [function() {
    return function(data, index) {
         return data.sort(function(o) {
             return o.row[index];
         });
    };
}]);
Sign up to request clarification or add additional context in comments.

2 Comments

I'm sorry for the newb question, but where do I put this function? I tried right after my $scope.getRequest = function () { and it says, (intermediate value)(intermediate value).filter is not a function
You need to register it with your angular app. I updated the answer.
0

I'd suggest transforming your data into something a little more standard. It will enable you to better maintain and reason about the code.

Since you can't change the format of the data the server sends you, you can just change it once it arrives on the client.

fetchData(results) {
   // Transform the results we get back from the server.
   this.myWelcomeData = results.data.map(function(input) {
      return {
         name: input.row[0],
         id: input.row[1],
         age: input.row[2]
      }
   });
}

Then you can order using the normal Angular repeater syntax:

<tr ng-repeat="roll in myWelcomeData" | orderBy:'id'">

1 Comment

This worked. I put it right after my $http.get statement I changed the first two lines to : .then(function(response) { $scope.myWelcome = response.data.data.map(function(input) {

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.