How to display an Enum item on my table using Angularjs?

Asked

Viewed 299 times

2

I have a table where I make a ng-repeat, hitherto normal.

<tr data-ng-repeat="item in itemsconfiguration">
                            <td>{{::item.Description}}</td>
                            <td>{{::item.Order}}</td>
                            <td>{{::item.Type}}</td>
                            <td><span ng-if="item.Active"></span></td>                            
                        </tr>

inserir a descrição da imagem aqui

Except that the Type field of my table which is an INT field, I would have to instead of displaying this integer value, display an item from my Enum table.

In my controller I’m already bringing the enuns...

        AuditingItemType.query().$promise.then(function (itemtypes) {
        $scope.itemtypes = itemtypes;
    })

Json I get from Enum table

[{"$id":"1","Id":0,"Name":"TEXT"},{"$id":"2","Id":1,"Name":"QUANTITY"},{"$id":"3","Id":2,"Name":"MULTIPLE"}]

how can I display it on my table dynamically?

  • Jhonas, your Enum already has the Annotation Description?

  • 1

    No... public Enum Auditingitemtype { TEXT, QUANTITY, MULTIPLE }

  • You are using Webapi 2.0?

  • I’m using yes.

1 answer

1


One possibility is to create a ng-repeat internal, and filter content - only display the description when the value of Id is the same as Type element referenced by main loop.

Example to follow:

function SampleController($scope) {

  $scope.itemsconfiguration = [
    {"Description": "Secretaria", "Order": 2, "Type":2, "Active": 0},
    {"Description": "Patrimonio", "Order": 3, "Type":3, "Active": 1},
    {"Description": "Tesouraria", "Order": 2, "Type":2, "Active": 1}
  ];

  $scope.itemtypes = [
    {"$id":"1","Id":0,"Name":"TEXT"},
    {"$id":"2","Id":1,"Name":"QUANTITY"},
    {"$id":"3","Id":2,"Name":"MULTIPLE"},
    {"$id":"3","Id":3,"Name":"OTHER"}
  ];

}
<html ng-app>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

  </head>
  <body>
    <div ng-controller="SampleController">

      <table>
        <tr>
        <th>Descrição</th>
        <th>Ordem</th>
        <th>Tipo</th>
        <th>Ativo</th>
        </tr>
        <tr ng-repeat="item in itemsconfiguration">
          <td>{{item.Description}}</td>
          <td>{{item.Order}}</td>
          <td>

            <span ng-repeat='type in itemtypes'
                  ng-if='type.Id == item.Type'>
              {{type.Name}}
            </span>

          </td>
          <td><span ng-if="item.Active">X</span></td>       
        </tr>
      </table>
    </div>
  </body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.