0

I am using angular 1.5 in my application, where I am using the JSON response from a rest service call to display data on the UI. I have a requirement now to change the text shown on UI based on particular 'statu's which comes from the rest service call. Can someone help me on how to use angular filters to achieve this?

JSON Response :

var response = {
  "recordCnt": 1,
  "details": [{
    "type": "user",
    "**status**": 0
  }]
};

The **status** could be '1' as well as '2'. I need to show different verbages based on these values on the UI.

eg : If the status is '0', I will have to show 'inactive', 'active' if status is '1' and 'expired' in case of status '2'

1
  • Can you accept an answer if your issue is solved? :) Commented Dec 16, 2016 at 8:47

3 Answers 3

1

I think you just need a ng-if condition in this case:

<div ng-if="response.details[0].status = 0">inactive</div>
<div ng-if="response.details[0].status = 1">active</div>
<div ng-if="response.details[0].status = 2">expired</div>

Try it on JSFiddle. You can play with the status value to see changes.

Sign up to request clarification or add additional context in comments.

5 Comments

Actually, I am using this status to be displayed in a cell of an angular UI grid . So I have to use filters for the same, since all the columns are configurable. Is it possible to do the same using a filter?
You can use ng-if on any HTML tag (not only div). If you work with a grid, you can adapt it to your needs (maybe a td instead of div?)
If you provide me a sample of your grid code, I guess I can adapt it for you.
Below is how the 'status' column is configured for the grid, status: { name: 'status', displayName: 'User Status', editable: false, width: '100', cellTemplate: '<div class="ui-grid-cell-contents">{{row.entity[col.field] | yourfilter}}</div>', } In the above snippet, 'yourfilter' is a filter function I expect to do the same which you explained in your answer.
You need to modify a bit your template (use external-scopes), look at this example.
1

You can use ng-if

 <div ng-controller="listController">
    <div ng-if="data.details[0].status == 0">inactive</div>
    <div ng-if="data.details[0].status == 1">active</div>
    <div ng-if="data.details[0].status == 2">expired</div>
  </div>

DEMO

Comments

0

I was able to use a custom angular filter for the same, it worked. Thanks @sajeetharan and @mistalis for the inputs.

Cell level configuration:

cellTemplate: '{{row.entity[col.field] | getUserStatus}}' //'getUserStatus' is the filter used.

Filter :

    (function() {
    'use strict';
    var statusMap = {'inactive', 'active', 'expired'};
    var filter = function(statusMap) {
        return function(status) {
            return statusMap[status];   // 'status' could be '0', '1' and '2'
        };
    };

    angular.module('app.userSearch).filter('getUserStatus', filter);
})();

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.