AngularJS: orderBy - string & array are not the same - angularjs

I have an array of users:
$scope.users = [
{name: 'Anh', age: 10},
{name: 'Ánh', age: 10},
{name: 'Ba' , age: 10}
]
When i use orderBy:'name' the result is:
Anh
Ánh
Ba
but when i use multiple fields orderBy:['name','age'] the result is different:
Anh
Ba
Ánh
Waiting for your help!

try this..
var app = angular.module('filterSample', []);
app.controller('filterCtrl', function ($scope) {
$scope.data = {
messages: ['first', 'second', '3rd', 'fourth', 'fifth']
}
$scope.startsWith = function (actual, expected) {
var lowerStr = (actual + "").toLowerCase();
return lowerStr.indexOf(expected.toLowerCase()) === 0;
}
});
and html as
<ul border="1px" ng-repeat="msg in data.messages | filter:search:startsWith">
<li>{{msg}}</li>
</ul>
or check on this plunkr-http://plnkr.co/edit/gcVYfZXTqsDU1Z7REU2I?p=preview

Related

Get only values from the array of array using angular js

I got an array from database as
[
{id:
{unitno: 'abc'},
amount: 100},
{id:
{unitno: 'xyz'},
amount: 150}
]
Now my required answer is that should be in the following format,
[["abc",100],
["xyz",150]]
But after some coding I got an array of Array as shown below in the console
[0:[0:"abc",1:100]
1:[0:"xyz",1:150]]
Before question down / marking duplicate please read my requirement, and if its there then mark it and please post that link as per my required solution so i can get my solution from there
So any help will be great help....
You can do like this:
data = [
{id:
{unitno: 'abc'},
amount: 100},
{id:
{unitno: 'xyz'},
amount: 150}
]
var array =[]
data.forEach(function(item){
var tmpArray = []
tmpArray.push(item.id.unitno,item.amount)
array.push(tmpArray)
})
Now you will get your required data in the array.
I don't know how you got this below code, but that's invalid Array
[0:[0:"abc",1:100] 1:[0:"xyz",1:150]]
For your desired output you can do it with JavaScript, no need angular. Just run a simple for loop.
var temp1 = [{
id: {
unitno: 'abc'
},
amount: 100
}, {
id: {
unitno: 'xyz'
},
amount: 150
}];
var eee = [];
temp1.forEach(function(el) {
eee.push([el.id.unitno, el.amount])
});
console.log('eee:', eee);
Hope this is what you needed.
var arr = [{
id : { unitno: 'abc' },
amount: 100
},{
id : { unitno: 'xyz' },
amount: 150
}];
var result = [];
for (var i = 0; i < arr.length; i++) {
var newArray = [];
newArray.push( arr[i]["id"]["unitno"] );
newArray.push( arr[i]["amount"] );
result.push( newArray );
}
console.log( result );

How to filter an array of objects based on an array of a specific object propert?

Here is a small example of my html using ng-repeat:
<div ng-repeat="item in vm.templateList | filter: vm.myFilter">
<h3>{{item.Code}}</h3>
</div>
In Js file the vm.templateList is as followed(as an example):
vm.templateList = [{Code: 'a', ID: 1},
{code: 'a', ID: 2},
{code: 'b', ID: 3},
{code: 'c', ID: 4}];
Imagine I want to filter this list for all items that have ID 1 and also items that have ID 2.
What I originaly was doing was like this:
vm.filter = {ID: 1};
But this was I can only filter the list on 1 ID. Can anyone suggest a way?
You can add the following AngularJS filter to your application :
// add a custom filter to your module
angular.module('MyModule').filter('myFilter', function() {
// the filter takes an additional input filterIDs
return function(inputArray, filterIDs) {
// filter your original array to return only the objects that
// have their ID in the filterIDs array
return inputArray.filter(function (entry) {
return this.indexOf(entry.ID) !== -1;
}, filterIDs); // filterIDs here is what "this" is referencing in the line above
};
});
You then declare your filter array in the controller as such :
vm.IDs = [1, 2];
Then your view should look like this :
<div ng-repeat="item in vm.templateList | myFilter: vm.IDs">
<h3>{{item.Code}}</h3>
</div>
You can use something like:
html:
<section>
<div ng-repeat="item in vm.templateList | filter:checkFilterOptions">
<h3>{{item.Code}}</h3>
</div>
</section>
Js:
$scope.vm = {};
$scope.vm.templateList = [
{Code: 'a', ID: 1},
{Code: 'a', ID: 2},
{Code: 'b', ID: 3},
{Code: 'c', ID: 4}
];
$scope.filterOptions = [1,2,3];
$scope.checkFilterOptions = function(value, index) {
return value.ID && $scope.filterOptions.indexOf(value.ID) !== -1;
}

angularjs - Filter to hide from repeater

I am trying to write a small filter to hide from my repeater elements.
Let's say that my scope is:
$scope.test = [
{id: 1,name: "Test number 1"},
{id: 2,name: "Test number 2"},
{id: 3,name: "Test number 3"},
{id: 4,name: "Test number 4"},
{id: 5,name: "Test number 5"},
{id: 6,name: "Test number 6"},
{id: 7,name: "Test number 7"},
{id: 8,name: "Test number 8"},
{id: 9,name: "Test number 9"},
{id: 10,name: "Test number 10"}
]
and in my repeater I am doing something like this:
<div ng-repeat="t in test| hide:[1,6]"></div>
I started to write my filter but I got stuck. This is what I have so far:
filter('hideIDs', function() {
newArray= [];
function(zone, IDS) {
var containsObject, newArray;
containsObject = function(obj, list) {
var i;
i = void 0;
i = 0;
while (i < list.length) {
if (list[i] === obj) {
return true;
}
i++;
}
return false;
};
angular.forEach(IDS, function(hide) {
return angular.forEach(test, function(t) {
if (t.id === hide) {
return
} else {
if (containsObject(t, newArray)) {
return
} else {
newArray.push(t);
}
}
});
});
return newArray;
};
});
What I am trying to do with the filter is:
check if t.id should be hidden, and if yes, return without pushing it to newArray
The problem I've got is:
id to hide is 1 on the first loop, and then 6 gets pushed
on the second loop hovewer, the id to hide is 6 and then 1 gets pushed
And I end up having them both anyway.
Suggestions?
What would you achieve this in an efficient angular way?
thanks a lot
How about this?
https://plnkr.co/edit/hPiOnq7jp4kIP6h8Ox1d?p=preview
<div ng-init="data = [{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}]">
<div ng-repeat="test in data | hideByIds:[2, 4]">
{{ test.id }}
</div>
</div>
You should reuse what you can, in this case the filter filter:
var app = angular.module('app', []);
app.filter( 'hideByIds', function( $filter ) {
return function( input, ids ) {
return $filter('filter')( input, function( it ) {
return ids.indexOf( it.id ) == -1;
} );
}
} );
An alternative is to specify the predicate function on the scope, and simply pass it to the filter in the template.

I want to concatenate these two objects to get the following result $scope.all=[ lead value and contact value]

$scope.lead = [
{name: 'raj', email:'raj#gmail.com'}
];
$scope.contact=[
{name: 'James', email:'james#gmail.com'}
];
Looks like more related to JS than angular itself.
$scope.lead = [{name: 'raj', email:'raj#gmail.com'}];
$scope.contact =[{name: 'James', email:'james#gmail.com'}];
$scope.all = {};
$scope.all.lead = $scope.lead;
$scope.all.contact = $scope.contact;
console.log(JSON.stringify($scope.all))

Shorten an array or collection with an array of fields

Scenario:
you have an array,
you have an array of some ids (eg: [1,3]),
you want to get a shortened array.
I cannot find how to do so if not in the lengthy way:
_.filter(... function (id) { return id==1 || id==3 })
Question: does such someMethod1 exist in underscore.js?
var frdsArr =
[
{id: 1, name: 'moe', age: 40},
{id: 2, name: 'larry', age: 50},
{id: 3, name: 'curly', age: 60}
];
var goodFrdsArr = _.someMethod1( frdsArr, 'id', [1, 3] );
/*
goodFrdsArr is [
{id: 1, name: 'moe', age: 40},
{id: 3, name: 'curly', age: 60}
];
*/
The same question with backbone.js: what about someMethod2?
var Friend = Backbone.Model.extend({});
var FriendsCollection = Backbone.Collection.extend({
model: Friend
});
var friendsCollection = new FriendsCollection( frdsArr );
var goodFriendsCollection = friendsCollection.someMethod2( 'id', [1,3] );
/*
goodFriendsCollection contains 2 instances of Friend Models
*/
Once you have found a suitable function, you can extend Underscore with _.mixin to simplify later calls. For example, you could create _.restrict to match your _.someMethod1 signature:
_.mixin({
// filter a list on an attribute having to match a list of values
// _.indexOf accepts an optional isSorted argument so let's pass it
restrict: function(list, attr, values, isSorted) {
return _.filter(list, function(obj) {
return _.indexOf(values, obj[attr], isSorted) !== -1;
});
}
});
var goodFrdsArr = _.restrict(frdsArr, 'id', [1, 3]);
See http://jsfiddle.net/nikoshr/cf0qs5gh/ for a demo
With this setup, you can modify Backbone.Collection's prototype to give all your collections this new capacity (more or less taken from the Backbone source code):
Backbone.Collection.prototype.restrict = function() {
var args = [].slice.call(arguments);
args.unshift(this.models);
return _.restrict.apply(_, args);
};
var friendsCollection = new Backbone.Collection(frdsArr);
var goodfriends = c.restrict('id', [1, 3]); //an array with 2 models
http://jsfiddle.net/nikoshr/cf0qs5gh/1/
var frdsArr =
[
{id: 1, name: 'moe', age: 40},
{id: 2, name: 'larry', age: 50},
{id: 3, name: 'curly', age: 60}
];
var filteredIDs = [1,3];
var filteredFrnds = _.filter(frdsArr, function(frnd){
return _.contains(filteredIDs, frnd.id);
})
console.log(filteredFrnds);
Make use of filter and contains
FIDDLE

Resources