Skip to content

Instantly share code, notes, and snippets.

@kohgpat
Created September 23, 2013 07:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kohgpat/6667400 to your computer and use it in GitHub Desktop.
Save kohgpat/6667400 to your computer and use it in GitHub Desktop.
angular.js currency filter using accounting.js
angular.module("app.users", ["ngResource"]).factory("Users", function($resource) {
return $resource("users.json", {}, {get: {method: "GET", isArray: true}});
});
angular.module("app.filters", []).filter("currency", function() {
return function(number, currencyCode) {
var currency = {
USD: "$",
RUB: ""
},
thousand, decimal, format;
if ($.inArray(currencyCode, ["USD", "RUB"]) >= 0) {
thousand = " ";
decimal = ".";
format = "%s%v";
}
else {
thousand = ".";
decimal = ",";
format = "%s%v";
};
return accounting.formatMoney(number, currency[currencyCode], 2, thousand, decimal, format);
};
});
angular.module("app", ["app.users", "app.filters"]);
function UsersController($scope, Users) {
$scope.users = Users.get();
}
<!doctype html>
<html lang="ru" ng-app="app">
<head>
<title>Angular: Filters</title>
<meta chartset="utf-8">
<script type="text/javascript" src="src/js/jquery.min.js"></script>
<script type="text/javascript" src="src/js/angular.min.js"></script>
<script type="text/javascript" src="src/js/angular-resource.min.js"></script>
<script type="text/javascript" src="src/js/accounting.min.js"></script>
<script type="text/javascript" src="src/js/app.js"></script>
<link type="text/css" rel="stylesheet" href="src/css/app.css"></link>
</head>
<body>
<div id="container" ng-controller="UsersController">
<h1>Users</h1>
<div id="filter">
<input type="text" ng-model="userFilter.firstName" placeholder="First Name" />
<input type="text" ng-model="userFilter.lastName" placeholder="Last Name" />
<input type="text" ng-model="userFilter.email" placeholder="Email" />
<input type="text" ng-model="userFilter.income" placeholder="Income" />
</div>
<table class="table">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Income</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="user in users | filter: userFilter">
<td>{{user.firstName}}</td>
<td>{{user.lastName}}</td>
<td>{{user.email}}</td>
<td>{{user.income | currency: "RUB"}}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
[
{
"firstName": "John",
"lastName": "Doe",
"email": "jdoe@gmail.com",
"income": "101720240.82"
},
{
"firstName": "Alice",
"lastName": "Appleseed",
"email": "aapple@gmail.com",
"income": "100444331.65"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment