Angular JS orderBy not working using dropdown - angularjs

i am trying to do orderBy using dropdown value, but its not working :(
CODE:
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
$scope.datas = [{
"id": "1",
"name": "name area 1"
}, {
"id": "2",
"name": "name area 2"
}, {
"id": "3",
"name": "name area 3"
}, {
"id": "4",
"name": "name area 4"
}];
$scope.dropdown = [{
"title": "Newest first",
"value": "true"
}, {
"title": "Oldest first",
"value": "reverse"
}];
});
HTML:
<div ng-app="myApp">
<div ng-controller="myCtrl">
<ul ng-repeat="rows in datas | orderBy: 'id':orderByData">
<li>{{rows.id}} : {{rows.name}}</li>
</ul>
<select ng-model="orderByData" ng-options="x.value as x.title for x in dropdown"></select>
</div>
</div>
DEOM JS BIN

Why you use reverse?
Please use false instead reverse and it should work fine.
$scope.dropdown = [{
"title": "Newest first",
"value": "true"
}, {
"title": "Oldest first",
"value": "false"
}];

You don't need reverse parameter here. Instead you can do something like this:
<ul ng-repeat="rows in datas | orderBy: getSort()">
<li>{{rows.id}} : {{rows.name}}</li>
</ul>
Where getSort is defined as:
$scope.getSort = function() {
return $scope.orderByData ? 'id' : '-id';
};
Basically you want sort part to look like orderBy: 'id' or orderBy: '-id'.
Demo: http://jsbin.com/pepureta/1/edit?html,js,output

Related

Angular NG-Repeat - Built In Filter Not working

I'm developing an app using Angular 1.5.5. I'm trying to filter an array of Location objects by ID. The Locations array has 10 objects in it. The filter is returning 2 objects instead of the of the one:
Locations Array:
My html template looks like:
<div class="col-md-3" ng-repeat="location in offerings.Locations | filter : {'ID':event.LocationID}">
{{location.Name}}<br />
{{location.Address}}, {{location.City}}, {{location.State}} {{location.Zip}}<br />
Loc ID = {{location.ID}} eventLocID = {{event.LocationID}}
</div>
It's returning 2 results instead of the filter where 2 = 2:
You could try write own filter function or use that construction:
ng-repeat="location in offerings.Locations | filter : {'ID':event.LocationID} : true"
Also check this topic Filter array of objects by attribute with integer value in ng-repeat
DEMO
var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl', function($scope) {
$scope.event = {"LocationID" : 2}
$scope.locations = [
{
"Address": "address1",
"city": "city1",
"ID": 2,
"Name": "name1",
"State": "state1",
"Zip": 243435
},
{
"Address": "address2",
"city": "city2",
"ID": 3,
"Name": "name2",
"State": "state2",
"Zip": 243435
},
{
"Address": "address3",
"city": "city3",
"ID": 12,
"Name": "name3",
"State": "state3",
"Zip": 243435
}
];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<div class="col-md-3" ng-repeat="location in locations | filter:{'ID' : event.LocationID}:true">
{{location.Name}}<br />
{{location.Address}}, {{location.City}}, {{location.State}} {{location.Zip}}<br />
Loc ID = {{location.ID}} eventLocID = {{event.LocationID}}
</div>
</div>

Complicated sorting with tabs in ng-repeat?

I have the following JSON object:
$scope.projects = [
{
"_id": "58ab1cb6edf5430e9014e2bc",
"name": "Project 1",
"issues": [
{
"status": 0,
"name": "Hart Cummings"
},
{
"status": 1,
"name": "Kinney Leach"
},
{
"status": 2,
"name": "Blake Mclaughlin"
}
]
},
{
"_id": "58ab1cb6d87456482e0eec4a",
"name": "Project 2",
"issues": [
{
"status": 0,
"name": "Vargas Gordon"
},
{
"status": 1,
"name": "Bobbie Church"
},
{
"status": 2,
"name": "Pennington Fry"
}
]
},
{
"_id": "58ab1cb6d87456482e0eec4a",
"name": "Project 3",
"issues": [
{
"status": 0,
"name": "WWWWW"
},
{
"status": 1,
"name": "SFSF"
},
{
"status": 2,
"name": "Pennington Fry"
}
]
}
];
I use ng-repeat to display this object in template:
<div ng-app="app">
<div ng-controller="TodoListController">
<div ng-repeat="project in projects">
<br>
<br>
<div><b> {{project.name}}</b></div>
<div class="tabs">
<div style="display:inline" ng-click="sort(1, $index);">Active</div> |
<div style="display:inline" ng-click="sort(0, $index);">Deactivated</div>
</div>
_____________________________________
<div ng-repeat="issue in project.issues | filter: (filterObject.index | filterObject.status)">
<div>{{issue.name}}</div>
</div>
</div>
</div>
</div>
So, as you can see I have two nested ng-repeat.
Issue is:
When I click on any tab and call method sort() I need to sort issue in project.issues by status field.
This is real sample that I want to reach.
https://jsfiddle.net/at6kw67g/
Problem is in this code:
<div ng-repeat="issue in project.issues | filter:filterObject.index : {status : filterObject.status}">
Since the links should sort the issues of a specific project, you should pass the project as argument to your sort() function.
And JavaScript arrays have a sort() method, so just use it.
Since one link should sort issues in ascanding order, and the other one in descending order, you should also pass this information to your sort() function.
So it boils down to
$scope.sortIssuesByStatus = function(project, descending) {
project.issues.sort(function(issue1, issue2) {
var result = issue1.status - issue2.status;
if (descending) {
result = -result;
}
return result;
});
}
and
<div ng-repeat="project in projects">
<div class="tabs">
<button ng-click="sortIssuesByStatus(project, true)">Active</button>
<button ng-click="sortIssuesByStatus(project, false)">Deactivated</button>
</div>
<div ng-repeat="issue in project.issues">
<div>{{issue.name}} - {{ issue.status }}</div>
</div>
</div>
Here's a plunkr implementing it.

AngularJS Filed nested array of objects with array of objects

I'm trying to filter a nested array of objects with my own objects, by idSubject. But I'm not getting the right result.
I have articles (which have subjects)
And a array of objects (which are the subjects I want to filter the articles with)
Data looks like this:
So I'm trying to filter the array of articles by its subjects.
I tried the following:
<div class="panel panel-default"
ng-repeat="searchArticle in searchArticles | filter: {subjects: filterSubjects} as articleSearchResult">
So filterSubjects is the second screenshot and SearchArticles is the first screenshot.
Without much luck.
Hope you can help, please tell me if things are still unclear.
This custom filter will help you.
Example : http://plnkr.co/edit/jMizCLxPH6DtDA5wL15Q?p=preview
HTML:
<body ng-app="myApp">
<div ng-controller="MainCtrl">
<h2>Select Subjects</h2>
<div ng-repeat="subject in subjects">
<label>
<input type="checkbox" ng-model="filterSubjects[subject.id]" ng-true-value="'{{subject.id}}'" ng-false-value="''">{{subject.name}}</label>
</div>
<h2>Filtered Articles</h2>
<div ng-repeat="searchArticle in searchArticles | subjectFilter:filterSubjects">{{searchArticle.name}}</div>
</div>
</body>
JS:
var app = angular.module('myApp', []);
app.controller('MainCtrl', function($scope) {
$scope.searchArticles = [{
"name": "Article1",
"sid": "1"
}, {
"name": "Article2",
"sid": "1"
}, {
"name": "Article3",
"sid": "2"
}];
$scope.subjects = [{
"name": "Subject1",
"id": "1"
}, {
"name": "Subject2",
"id": "2"
}];
$scope.filterSubjects = [];
});
app.filter('subjectFilter', function() {
return function(articles, filterSubjects) {
filtered = articles.filter(function(e){return filterSubjects.indexOf(e.sid) >= 0},filterSubjects);
return filtered;
}
});
if you want to filter based on object :
var app = angular.module('myApp', []);
app.controller('MainCtrl', function($scope) {
$scope.searchArticles = [{
"name": "Article1",
"sid": "1"
}, {
"name": "Article2",
"sid": "1"
}, {
"name": "Article3",
"sid": "2"
}];
$scope.subjects = [{
"name": "Subject1",
"id": "1"
}, {
"name": "Subject2",
"id": "2"
}];
$scope.filterSubjects = [{
"name": "Subject1",
"id": "1"
}, {
"name": "Subject1",
"id": "2"
}];
});
app.filter('subjectFilter', function() {
return function(articles, filterSubjects) {
var sFiltered = [];
for (var i = 0; i < filterSubjects.length; i++) {
sFiltered.push(filterSubjects[i].id);
}
var filtered = articles.filter(function(e) {
return sFiltered.indexOf(e.sid) >= 0;
}, sFiltered);
return filtered;
}
});

Not able to get select value in scope

I have code like one attached in js fiddle. It is very basic, I am trying to get value of the select when it changes using scope variable. But i am not able to.Is there any specific reason why it is not working.
<div ng-controller="myCtrl">
<select
ng-model="myOption"
ng-options="value.id as value.label group by value.group for value in myOptions" ng-change="selectedValue()">
<option>--</option>
</select>
<div>
ng-model value: {{myOptionnew}}
</div>
</div>
var app = angular.module('myExample', []);
function myCtrl($scope) {
$scope.myOptions = [{
"id": 106,
"group": "Group 1",
"label": "Item 1"
},{
"id": 107,
"group": "Group 1",
"label": "Item 2"
},{
"id": 110,
"group": "Group 2",
"label": "Item 3"
}];
$scope.selectedValue = function()
{
alog($scope.myOption);
$scope.myOptionnew = $scope.myOption;
}
};
http://jsfiddle.net/jm6of9bu/1107/
In the fiddle the function alog is not defined. Therefore the assignment that follows doesn't occur.
Remove alog or replace it with $log.log : fiddle
As a side note, you also need to add value="" to the default empty option if you want it to be displayed:
<option>--</option>
should be:
<option value="">--</option>
HTML:
<div ng-controller="myCtrl">
<select
ng-model="myOption"
ng-options="value.id as value.label group by value.group for value in myOptions">
<option>--</option>
</select>
<div>
ng-model value ID: {{myOptionnew}}
</div>
JS :
var app = angular.module('myExample', []);
function myCtrl($scope) {
$scope.myOptions = [{
"id": 106,
"group": "Group 1",
"label": "Item 1"
},{
"id": 107,
"group": "Group 1",
"label": "Item 2"
},{
"id": 110,
"group": "Group 2",
"label": "Item 3"
}];
$scope.$watch('myOption', function(result){
if (result)
$scope.myOptionnew = result;
});
};
I used $watch try this!

AngularJS displaying hierarchical data

I'm trying to display a list of data in a hierarchical view.
My data looks something like this:
items:[
{
"model_id": "1",
"model_type_id": "1",
"name": "Parent 1",
"model_parent_id": ""
},
{
"model_id": "2",
"model_type_id": "1",
"name": "Parent 2",
"model_parent_id": ""
},
{
"model_id": "3",
"model_type_id": "2",
"name": "Child 1",
"model_parent_id": "1"
},
{
"model_id": "4",
"model_type_id": "2",
"name": "Child 2",
"model_parent_id": "2"
}
]
My controller looks like:
myApp.controller('ModelController', ['$scope', 'ModelFactory',
function ($scope, ModelFactory) {
$scope.init = function (id) {
$scope.brandId = id;
getModels($scope.brandId);
};
function getModels(brandId) {
ModelFactory.GetModels(brandId)
.success(function (mdls) {
$scope.models = mdls;
console.log($scope.mdls);
})
.error(function (error) {
$scope.status = 'Unable to load model data: ' + error.message;
console.log($scope.status);
});
};
}
]);
My HTML looks like:
<div ng-controller="ModelController" ng-init="init(brand.ID)">
<ul ng-sortable class="block__list block__list_words">
<li ng-repeat="model in models | filter: {model_type_id:1} ">{{model.model_name}} - {{model.model_id}}
<div ng-controller="ModelController" ng-init="init(brand.ID)">
<ul ng-sortable class="block__list block__list_words">
<li ng-repeat="m in models | filter: {model_type_id:2} | filter:{model_parent_id:model.model_id}">
{{m.model_name}} - {{m.model_parent_id}}
</li>
</ul>
</div>
</li>
</ul>
</div>
The filter isn't working where I'm trying to filter on the inner controller with the outer controller. I'm getting both children displayed below each parent. How can I get it so the parent is displayed, and only the children are displayed where the childs model_parent_id equals the model_id of the parent?
While I'm not sure whether there is a way to achieve this using filter, the normal way to display nested data is to reorganize the data structure to reflect what you want to display.
items:[
{
"model_id": "1",
"model_type_id": "1",
"name": "Parent 1",
"children": [{
"model_id": "3",
"model_type_id": "2",
"name": "Child 1"
}]
},
{
"model_id": "2",
"model_type_id": "1",
"name": "Parent 2",
"children": [{
"model_id": "3",
"model_type_id": "2",
"name": "Child 2"
}]
}
]
And then display them using nested ng-repeat
<ul>
<li ng-repeat="parent in items">
{{parent.name}} - {{parent.model_id}}
<ul>
<li ng-repeat="child in parent.children">
{{child.name}} - {{child.model_id}}
</li>
</ul>
</li>
</ul>
Note: There is no need to use nested controllers, just one on the top layer should be enough. If you need to use some shared logic recursively, use a custom directive to replace the li.
To reorganize the data you can do either on the server side or client side.
The following shows how to do in client side as we might not have the permission to change the server side API.
$scope.data = [];
angular.forEach(items, function(item) {
if (item.model_parent_id == "") {
$scope.data.push(item);
}
});
// add all parents first in case some children comes before parent
angular.forEach(items, function(item) {
if (item.model_parent_id == "") continue;
// search for parent
angular.forEach($scope.data, function(parent) {
if (parent.model_id == item.model_parent_id) {
if (!parent.children) parent.children = [];
parent.children.push(item);
}
}
});

Resources