Angular ng-repeat change values

I am making a table with angular using ng-repeat. And it all works, but in some cases json gives me back some data like PA-AC-DE and I want to change that in the table in Pending, Active and deactivate. And I don't know how I can do it. enter image description here

<table class="table table-bordered table-hover table-striped dataTable no-footer" data-sort-name="name" data-sort-order="desc">
    <tr role="row" class="info text-center">
        <th ng-click="order('msisdn')">Número Teléfono</th>
        <th ng-click="order('icc')">ICC</th>
        <!--th>IMEI</th-->
        <th ng-click="order('ActivationStatus')">Estado</th>
        <th ng-click="order('sitename')">Instalación</th>
        <th ng-click="order('siteaddress')">Dirección</th>
        <th ng-click="order('sitecity')">Ciudad</th>
        <th ng-click="order('sitezip')">Código Postal</th>
        <th ng-click="order('phonedesc')">Modelo Teléfono</th>
        <th ng-click="order('ContractingMode')">VBP</th>
    </tr>
    <tr class=" text-center" ng-repeat-start="object in filteredsites = (objects | filter:searchText) | filter:tableFilter| orderBy:predicate:reverse" ng-click="showDetails = ! showDetails">
        <td>{{object.msisdn}}</td>
        <td>{{object.icc}}</td>
        <td>{{object.ActivationStatus}}</td>
        <td>{{object.sitename}}</td>
        <td>{{object.siteaddress}}</td>
        <td>{{object.sitecity}}</td>
        <td>{{object.sitezip}}</td>
        <td>{{object.phonedesc}}</td>
        <td>{{ object.ContractingMode ? 'Yes': 'No'}}</td>
    </tr>
</table>

      

+3


source to share


6 answers


You can use a filter

{{object.ActivationStatus | statusFilter}}

      



and the statusFilter will look like this:

angular.module('module', []).filter('statusFilter', function() {
    return function(input) {
        //switch-case
   };});

      

+3


source


Based on AWolf's answer with a filter, here's a function in the controller:

http://jsfiddle.net/f4bfzjct/

angular.module('demoApp', [])
  .controller('mainController', function() {
    var vm = this;
    vm.data = [
        {status:'AC'},
        {status:'AC'},
        {status:'DE'},
        {status:'PA'},
    ];

    vm.getFullStatus = function(value) {

        var labels = {
            AC: 'active',
            DE: 'deactive',
            PA: 'pending'
        };

        return labels[value];

    }
});

      




<div ng-app="demoApp" ng-controller="mainController as ctrl">
    <ul>
        <li ng-repeat="row in ctrl.data">
            status: {{ctrl.getFullStatus(row.status)}}
        </li>
    </ul>
</div>

      

+2


source


I think you should create a filter in your module:

ngModule.filter('phoneNumberStatus', function() {
    statuses = {
        AC: 'Active'
        DE: 'Unactive'
    }
    return function(value) {
        return statuses[value] || "Unknown"
    }
})

      


and then use it in your template:

<td>{{ object.ActivationStatus | phoneNumberStatus }}</td>

      


This way will allow you to reuse this filter in any template, avoiding code duplication.

+2


source


You can use ng-show to display text depending on the value returned from your API, for example:

<td><span ng-show="object.ActivationStatus=='AC'">Active</span><span ng-show="object.ActivationStatus=='PA'">Other Label</span></td>

      

etc.

+1


source


With a custom filtering method, it will look like in the demo below or here on jsfiddle .

But also a getter function with the same code will be OK.

angular.module('demoApp', [])
.controller('mainController', function() {
    this.data = [
        {status:'AC'},
        {status:'AC'},
        {status:'DE'},
        {status:'PA'},
    ];
})
    .filter('filterStatus', function() {
        var labels = {
            AC: 'active',
            DE: 'deactive',
            PA: 'pending'
        };
        return function(input) {
            return labels[input];
        };
    });
      

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demoApp" ng-controller="mainController as ctrl">
    <ul>
        <li ng-repeat="row in ctrl.data">
            status: {{row.status | filterStatus}}
        </li>
    </ul>
</div>
      

Run codeHide result


+1


source


You can create a javascript function that returns the value you want:

$scope.getFullActivationText = function(input) {
if (input === 'PA') {
    return 'Pending';
}
else if (input === 'AC') {
    return 'Active';
}
else if (input === 'DE') {
    return 'Deactivate';
}

      

}

Now you can keep everything the same in your HTML, but replace:

<td>{{object.ActivationStatus}}</td>

      

in

<td>getFullActivationText(object.ActivationStatus)</td>

      

+1


source







All Articles