1

Suppose, I have two json files item-list.json and item-types.json containing items array and item types array separately. Then how to categorize the item list using angularjs? Here is Plunker link: https://embed.plnkr.co/eqL876dwMPxuROkLQ4OP/ Currently there is displaying different lists. But i want to combine both list like this:

  1. Type 1
    • List item 1
    • List item 2
    • List item 3
  2. Type 2
    • List item 4
  3. Type 3
    • List item 5
    • List item 6

Please help. I am new to AngularJS. Thanks in advance

2
  • I suggest you merge your 2 json files into 1 object and then make nested loops with angularjs. Of course you can have the job done by the view directly with filters, but it is not the right way imho. Commented Nov 30, 2017 at 17:06
  • @RaphaMex Thanks for reply. I can not combine both json. Actually i was using: var nestedList = $scope.itemsList.filter(function(item) { return item.ItemTypeId == $scope.itemsTypes.ItemTypeId; }); It shows empty array in console. Is there any other way? Commented Nov 30, 2017 at 17:34

2 Answers 2

1

What you want to do is to filter in the view using angular filters:

item in itemsList | filter:{ItemTypeId:itemType.ItemTypeId}

I advise you against using ng-if when not necessary. It will save you lots of performance and sub-scope issues.

IMHO the model manipulation (here mergin 2 JSON files into 1 object) should be done inside the controller, not the view.

To do so, I changed your code to ensure both JSON are got before updating $scope and rendering.

$http({
    method: 'GET',
    url: "item-types.json"

}).then((types_array) => {
    $http({
        method: 'GET',
        url: "item-list.json"
    }).then((items_array) => {
        let types = types_array.data.data.reduce( (acc,type) =>  {
            acc[type["ItemTypeId"]] = type["TypeName"];
            return acc;
        }, {});
        $scope.items = items_array.data.data.reduce( (acc,item) => {
            let type = types[item["ItemTypeId"]];
            if (! (type in acc) )
                acc[type] = [];
            acc[type].push(item);

            return acc;
        }, {});

    })
});

Then your HTML becomes:

<ol>
  <li ng-repeat="(type,items_array) in items">{{type}}
  <ul>
    <li ng-repeat="item in items_array">
      <p>{{item.ItemName}}</p>
      <small ng-class="{active:item.IsActive}">{{item.IsActive ?'active' : 'Inactive'}}</small>
    </li>
  </ul>
  </li>
</ol>
Sign up to request clarification or add additional context in comments.

1 Comment

It works like Charm!! Can we do the same using Kendo Ui List view?
0

I have updated your HTML and get results. PFB new plunkr with just updated HTML code.

https://embed.plnkr.co/FPp5LzLPnk1N2J6syY5M/

Comments

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.