JavaScript AngularJs Data Manipulation before display data manipulation angularjs

I have a simple table in which there are 2 columns: a) Name b) Country. Currently it works fine and displays name of the user and country.

Now I want to add a condition so that if the country is Germany then instead of Germany it should print Europe and if country is Argentina or Brazil it should print South America, for other countries it will remain same, for example UK, Sweden or any other country will be printed as it is.

My angular code is this ::

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 

<table>
  <tr ng-repeat="x in names">
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("http://localhost/test/user_data.php")
    .then(function (response) {$scope.names = response.data.records;});
});
</script>

</body>
</html>

And json data in my user-data.php file is this::

{
    "records":[
    {"Name":"Alfreds Futterkiste","City":"Berlin","Country":"Germany"},
    {"Name":"Ana Trujillo Emparedados","City":"México D.F.","Country":"Mexico"},
    {"Name":"Antonio Moreno Taquería","City":"México D.F.","Country":"Mexico"},
    {"Name":"Around the Horn","City":"London","Country":"UK"},
    {"Name":"B's Beverages","City":"London","Country":"UK"},
    {"Name":"Berglunds snabbköp","City":"Luleå","Country":"Sweden"},
    {"Name":"Blauer See Delikatessen","City":"Mannheim","Country":"Germany"},
    {"Name":"Blondel père et fils","City":"Strasbourg","Country":"France"},
    {"Name":"Bólido Comidas preparadas","City":"Madrid","Country":"Spain"},
    {"Name":"Bon app'","City":"Marseille","Country":"France"},
    {"Name":"Bottom-Dollar Marketse","City":"Tsawassen","Country":"Canada"},
    {"Name":"Cactus Comidas para llevar","City":"Buenos Aires","Country":"Argentina"},
    {"Name":"Centro comercial Moctezuma","City":"México D.F.","Country":"Mexico"},
    {"Name":"Chop-suey Chinese","City":"Bern","Country":"Switzerland"},
    {"Name":"Comércio Mineiro","City":"São Paulo","Country":"Brazil"}
    ]
} 
Answer:1

There are numerous ways to achieve this in angular. (directives, functions, services, ng-if...etc.)

The cleanest in your case would be a custom filter:

.filter('countryFormat', function() {
    return function(input) {
        if (input != undefined) {
           //Your switch case here
           return 'South America'; // for example
        }
    }
});

And in the html:

<td>{{ x.Country | countryFormat }}</td>
Answer:2

you should create a function inside your controller to check which country it is and should decide what to output

check the Fiddle

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $scope.names =[
    {"Name":"Alfreds Futterkiste","City":"Berlin","Country":"Germany"},
    {"Name":"Ana Trujillo Emparedados","City":"México D.F.","Country":"Mexico"},
    {"Name":"Antonio Moreno Taquería","City":"México D.F.","Country":"Mexico"},
    {"Name":"Around the Horn","City":"London","Country":"UK"},
    {"Name":"B's Beverages","City":"London","Country":"UK"},
    {"Name":"Berglunds snabbköp","City":"Luleå","Country":"Sweden"},
    {"Name":"Blauer See Delikatessen","City":"Mannheim","Country":"Germany"},
    {"Name":"Blondel père et fils","City":"Strasbourg","Country":"France"},
    {"Name":"Bólido Comidas preparadas","City":"Madrid","Country":"Spain"},
    {"Name":"Bon app'","City":"Marseille","Country":"France"},
    {"Name":"Bottom-Dollar Marketse","City":"Tsawassen","Country":"Canada"},
    {"Name":"Cactus Comidas para llevar","City":"Buenos Aires","Country":"Argentina"},
    {"Name":"Centro comercial Moctezuma","City":"México D.F.","Country":"Mexico"},
    {"Name":"Chop-suey Chinese","City":"Bern","Country":"Switzerland"},
    {"Name":"Comércio Mineiro","City":"São Paulo","Country":"Brazil"}
    ]

    $scope.getCountry = function(country) {
    if(country === 'Germany'){
    return 'EU'
    } else {
    return country
    }
    }

});

and use it in html like below

<div ng-app="myApp" ng-controller="customersCtrl"> 

<table>
  <tr ng-repeat="x in names">
    <td>{{ x.Name }}</td>
    <td>{{ getCountry(x.Country) }}</td>
  </tr>
</table>

</div>
Answer:3

inside td of name

<td><div ng-if="x.name=="Germany">Europe</div><div ng-if="x.name=="argentina||brazil">south america</div>

Answer:4

you can use the ng-switch also

 <td ng-switch on="x.Country">
        <div ng-switch-when="Germany ">Europe </div>
        <div ng-switch-when="Argentina">South America</div>
         <div ng-switch-when="Brazil ">South America</div>
         <div ng-switch-when="Brazil ">South America</div>
         <div ng-switch-default>{{x.Country}}</div>
      </td>

for refrence https://plnkr.co/edit/OFWhePEJEuHSrBR3zW9i

Answer:5
<table>
  <tr ng-repeat="x in names">
    <td>{{ x.Name }}</td>
    <td ng-if = "x.Country === 'Germany'">Europe</td>
    <td ng-if = "x.Country === 'Argentina'|| x.Country ==='Brazil'">South America</td>
    <td ng-if = "x.Country !== 'Argentina' || x.Country !== 'Brazil' || x.Country !== 'Germany'">{{x.Country}}</td>
  </tr>
</table>
Answer:6

I have the following set up: And I am trying to do the following: case 'UPDATE_PASSWORD': return { ...state, //preserve current state, apply changes to it below password: action.password,...

I have the following set up: And I am trying to do the following: case 'UPDATE_PASSWORD': return { ...state, //preserve current state, apply changes to it below password: action.password,...

Is there a better way to run parallel queries with the RethinkDB Node driver than opening multiple connections per request? Or is this actually a good way of going about what I need? I'd prefer to ...

Is there a better way to run parallel queries with the RethinkDB Node driver than opening multiple connections per request? Or is this actually a good way of going about what I need? I'd prefer to ...

  1. rethinkdb nodejs
  2. rethinkdb nodejs example
  3. rethinkdb nodejs npm
  4. rethinkdb nodejs driver
  5. rethinkdb node js tutorial
  6. rethinkdb node.js socket.io
  7. rethinkdb node js module
  8. rethinkdb node js setup
  9. rethinkdb node js orm
  10. rethinkdb node js express
  11. rethinkdb example node js chat
  12. node.js rethinkdb example
  13. changefeeds rethinkdb nodejs
  14. nodejs rethinkdb socket.io
  15. nodejs rethinkdb tutorial
  16. rethinkdb connection node js
  17. nodejs rethinkdb cursor
  18. nodejs rethinkdb chat
  19. nodejs use rethinkdb
  20. nodejs rethinkdb express

This is my FIDDLE <table> <tr> <td> <div style="background:blue;color:white">hello</div> </td> </tr> <tr> ...

This is my FIDDLE <table> <tr> <td> <div style="background:blue;color:white">hello</div> </td> </tr> <tr> ...

  1. sort table rows javascript
  2. sort table rows matlab
  3. sort table rows with jquery
  4. sort table rows
  5. sort table rows in word
  6. sort table rows html
  7. sort table rows using ajax php
  8. sort table rows angularjs
  9. sort table rows using jquery
  10. sort table rows angular
  11. sort table rows mysql
  12. sort table row php
  13. order table rows jquery
  14. order table rows javascript
  15. order table rows
  16. order table rows html
  17. sort pivot table rows
  18. javascript sort table rows by column
  19. js sort table rows
  20. jquery sort table rows by column value

I've recently installed Jade(Pug) on a Mac OS X Yosemite. I installed the node.js last version and then used the terminal command: $ sudo npm install pug-cli -g Everything was fine until i had to ...

I've recently installed Jade(Pug) on a Mac OS X Yosemite. I installed the node.js last version and then used the terminal command: $ sudo npm install pug-cli -g Everything was fine until i had to ...