Select all elements when all checkboxes unchecked - angularjs

I am working on a webpage that would display a list of products with several attributes (color, size, style). I would need that when all checkboxes are unchecked, the page would show all the products, and just when I start checking one the categories will it start filtering the products. Is that possible with checklist-model? Thanks in advance

Yes you can use checklist-model (http://vitalets.github.io/checklist-model/ if you are refreeing this)
Have the code below which will tell you how can you do it just add the filtering logic to it.
Controller code
myApp.controller('MyCtrl', function ($scope) {
//On controller load show all the products
$scope.filters = ['color','style','size'];
$scope.allproducts = [];
$scope.selectedFilters = {
filters: []
};
$scope.applyFilter = function (filters)
{
//Add filtering logic to filter objects from allproducts
//according to filter values in the filters array passed in the function.
//the data will be changed in if filter applied to allproducts array as allproducts in binded in view.
}
$scope.$watchCollection('$scope.selectedFilters.filters', function (newVal, oldVal) {
//If all values are unchecked
if($scope.selectedFilters.filters.length == 0)
{
//Show All products
}
else {
//Show the filtered products
applyFilter($scope.selectedFilters.filters) ;
}
}); });
View code:
<div ng-app="MyApp" ng-controller="MyCtrl">
<label ng-repeat="filter in filters">
<input type="checkbox" checklist-model="selectedFilters.filters" checklist-value="filter"> {{filter}}
</label>
<label ng-repeat="product in allproducts">{{product}}</label>
</div>

Related

Angularjs two way data binding - ngChange

I am working on a project which is having a web form using html and angularjs
the backend using java/spring and db oracle.
My form contains few lists say list1,list2...
list1 get its items from db. As soon as user selects the item in list1 ng-change gets trigger and data for list2 geenrates.That means list2 values depends on list1 ng-change directive.After filling all required fields I am saving the form in db.
Now I am giving user a provison that they can see there filled details . So once they click on "edit" they can see all their details. I am using ng-model to bind the data .All fields are working and binding values form db to the html webform except for list2. Can anyone show how can we achieve this.
I am having some issues with hide/show functionality when user wants to see his request in editable mode. Please suggest some workaround.
<div ng-app="myApp" ng-controller="myCtrl">
<label>List1:</label>
<select ng-model="selectedName" ng-change="getList()" ng-options="item for item in names">
</select>
<br>
<label>List2:</label>
<select ng-model="selectedNcomany" ng-options="item for item in comany">
</select>
<button ng-click="editText()">
Edit
</button>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.names = ["Emil", "Tobias", "Linus"];
var db = {};
$scope.getList = function(){
if($scope.selectedName == "Linus"){
$scope.comany = ["!","2"];
}
else{
$scope.comany =["0"]
}
}
$scope.editText = function(){
$scope.selectedName = "Linus";
$scope.selectedNcomany = "1";
}
});
</script>
https://jsfiddle.net/31d8cv92/12/
I work in the similar environment we created a service show field which looks like this
angular.module('test').service('showField',showField);
function showField(){
this.showfield = function(fieldData,pagemode){
if(angular.isDefined(fieldData) && fieldData.hasOwnProperty('hide')){
if (fieldData.hide) {
if (fieldData.npi) {
if (pagemode == 'view') {
return false;
}else {
return true;
}
} else {
return false;
}
} else {
return true;
}
}
}
}
We invoke this in our required controllers for view mode and update mode.
**(New to AngularJS)

how to store a result from mutl ng-repeat

I am using ng-repeat with filter and I am trying to store the ng-repeat list .
Here is my code.
<div ng-repeat="itemDate in itemList()|filter:'dispatch'|unique:'date'">
<div>{{(itemDate.dateTimeStamp)}}</div>
<div ng-repeat="item in itemList() |filter:'dispatch'">
<div ng-show="itemDate.date === item.date">
<div>
{{item.desc}}{{item.code}}{{item.price}}
</div>
</div>
</div>
</div>
itemList() i sthe list coming from service.I am trying to store the filterd list in an array. How do I keep the filtered list?
A filter that you are referring to is a view filter for the ng-repeat's rendering loop.
Therefore, the filter is only applied to the data that is being displayed in the DOM. What you are looking to do is update the original data based on the filters provided in your controller, or potentially use that filter as a provider.
controller:
angular.module('myRandomModule')
.controller('SomeControllerWithAFilterInjected', ['$filter', '$scope', SomeControllerWithAFilterInjectedFn]);
function SomeControllerWithAFilterInjectedFn($filter, $scope) {
$scope.arrayOfDataUsedInMyNgRepeat = ['foo', 'bar', 'so', 'many', 'elements'];
$scope.objectWhichHasABunchOfPropsBoundToNgModelsForFilteringThatIsUsedInYourFilter = {};
$scope.filteredArray = function() {
return $filter('nameOfThatFilterIMadeOneDay')($scope.arrayOfDataUsedInMyNgRepeat, $scope.objectWhichHasABunchOfPropsBoundToNgModelsForFilteringThatIsUsedInYourFilter);
}
}
Here is an excellent link to some examples of filters used in controllers.

saving and retrieving saved checkbox values from local storage in angularjs

I am new to angularjs and phonegap. I am trying to build an app where on a page, a user can select a category from say 9 categories. I am putting a checkbox beside each category name for the user to select the categories. After selecting a few categories(by checking the respective checkboxes), the user can press the save button for the categories to be saved. My question is how do I save all those categories and how do I retrieve them?
This is the code I have so far
<!--html code --> <!-- the items here the titles of the categories pulled from a data.js file-->
<div ng-repeat="row in items | partition:3">
<div ng-repeat="item in row"">
<input type="checkbox" ng-model="(item.title)">
<div>{{item.title}}</div>
<ons-button modifier="large" ng-click="savecategories()">Ready</ons-button>
/*angularjs code */
app.controller('categoryController', function($scope) {
$scope.savecategories = function() {
if ($scope.category1)
{
localStorage.setItem('category1', true);
}
else {
localStorage.setItem('category2', false);
if ($scope.category2)
{
localStorage.setItem('category2', true);
}
else {
localStorage.setItem('category2', false);
}
/*and so on */
}
});
I tried to see if it is working by putting this line of code so that I would get an alert but no luck.
var national = localStorage.getItem('category1');
window.alert(national);
I am not sure what I am doing wrong. Any help will be greatly appreciated.
Thank you everyone. I figured it out finally. I just added a function which is called everytime a checkbox is checked or unchecked.
<input type="checkbox" ng-model="item.value" name="item.title" ng-change="savecategories()">
In my js file, I did this:
app.controller('HomeController', function($scope, Data, localStorageService) {
$scope.items = Data.items;
if (localStorageService.get('items')) {
$scope.items = localStorageService.get('items');
}
$scope.SaveCategories = function () {
localStorageService.clearAll();
localStorageService.add('items',$scope.items);
}

AngularJS search filter with other filters

I have a code below:
<input type='text' ng-model="search.$">
<li ng-repeat="item in items" |filter:isExpired | filter:search>{{item.name}}</li>
Currently, items has a boolean attribute to indicate whether it is expired or not and the page displays all items that are NOT expired.
On search, I want to show ALL items including expired ones. Is there a way to toggle isExpired filter on search? Or would I need to develop my own custom filter? if so, can anyone give me an idea of an approach?
Any help would be very much appreciated!
You could do the filtering in the controller:
.controller('YourController', function($scope, $filter){
$scope.getItems = function() {
var filtered;
if($scope.search) {
filtered = $filter('filter')($scope.items, $scope.search);
}
else{
filtered = $filter('filter')($scope.items, {isExpired: false});
}
return filtered;
}
});
and then, your ng-repeat would look like this:
<li ng-repeat="item in getItems()">{{item.name}}</li>

Save the state of array after custom filter in AngularJs

I have two arrays of data which is being shown in a tree form and a relation between them.
$scope.types=['odd','prime','square','even'];
$scope.items=['1','4','3','8'];
div ng-repeat="item in items| customFilter">item
And
div ng-repeat="type in types| customFilter">type
Both are displayed in an ng-repeat and according to the selected item the other list adjusts.
eg: 1 is selected, the state becomes
$scope.type=['odd','prime','square','even'];
$scope.items=['1','4','3','8'];
Elements from array items related to type moves to the top.
Similarly, eg: even is selected , the state becomes
$scope.type=['square','even','odd','prime'];
$scope.items=['4','8','1','3'];
Bold elements are highlighted on page. I use a custom filter in ng-repeat for showing and sorting according to the relation.
What I want is after it's sorted, the same state is stored in the original arrays. Now when I select an element from $scope.items the $scope.types returns to the original state and accordingly the list on page re-adjusts which looks bad.
Track the changes to the collections either by ng-change or $watch and use the $filter service in controller to get the filtered values and store it in the same array.
Sample Demo: http://plnkr.co/edit/eW48QL2j30ZAKnwGzs3g?p=preview
<body ng-controller="MainCtrl">
<select ng-model='opt1' ng-options='option for option in options1' ng-change='filter(options2, opt1)'></select>
<select ng-model='opt2' ng-options='option for option in options2 | filter:opt1'></select>
<button ng-click='clear()'>Clear</button>
</body>
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $filter) {
$scope.options1 = ['A','C','J'];
$scope.options2 = ['Andrew','Chris Martin', 'Jeeva'];
$scope.filter = function(collection, value){
$scope.newoptions2 = $filter('filter')(collection, value);
console.log($scope.newoptions2);
$scope.options2 = $scope.newoptions2;
$scope.options1 = [value];
};
$scope.clear = function(){
$scope.options1 = ['A','C','J'];
$scope.options2 = ['Andrew','Chris Martin', 'Jeeva'];
}
});
Replace the 'filter' with your 'customFilter'. Let me know if these helps.

Resources