filter data according to class names in angularjs
I'm using angularjs and here is my view :
<div style="width:70px;">
Show Online <input type="checkbox" ng-model="showonline" />
</div>
<div ng-repeat="user in users|filter:showonline">
<span type="button" class="{(isOnline()) ? 'available' : 'unavailable'}">user.id</span>
</div>
above code will generate html like this inside the div containing ng-repeat:
<span type="button" class="available">111</span>
<span type="button" class="available">121</span>
<span type="button" class="unavailable">88</span>
<span type="button" class="available">878</span>
the classes are assigned according to the online status of the user. by default the above code displays all the elements having class available and unavailable. I want to use a checkbox as a filter which will display the based on the class name(if checkbox is clicked then display with classname available which will show only the online users). And when I uncheck the filter checkbox then all the having classes available and unavailable should be displayed.
I know how to filter data in angularjs but I have no idea how to filter data by classname using a checkbox.
How do I do it?
javascript angularjs
add a comment |
I'm using angularjs and here is my view :
<div style="width:70px;">
Show Online <input type="checkbox" ng-model="showonline" />
</div>
<div ng-repeat="user in users|filter:showonline">
<span type="button" class="{(isOnline()) ? 'available' : 'unavailable'}">user.id</span>
</div>
above code will generate html like this inside the div containing ng-repeat:
<span type="button" class="available">111</span>
<span type="button" class="available">121</span>
<span type="button" class="unavailable">88</span>
<span type="button" class="available">878</span>
the classes are assigned according to the online status of the user. by default the above code displays all the elements having class available and unavailable. I want to use a checkbox as a filter which will display the based on the class name(if checkbox is clicked then display with classname available which will show only the online users). And when I uncheck the filter checkbox then all the having classes available and unavailable should be displayed.
I know how to filter data in angularjs but I have no idea how to filter data by classname using a checkbox.
How do I do it?
javascript angularjs
Share a jsfiddle.
– Chinmoy Samanta
Nov 19 '18 at 10:29
add a comment |
I'm using angularjs and here is my view :
<div style="width:70px;">
Show Online <input type="checkbox" ng-model="showonline" />
</div>
<div ng-repeat="user in users|filter:showonline">
<span type="button" class="{(isOnline()) ? 'available' : 'unavailable'}">user.id</span>
</div>
above code will generate html like this inside the div containing ng-repeat:
<span type="button" class="available">111</span>
<span type="button" class="available">121</span>
<span type="button" class="unavailable">88</span>
<span type="button" class="available">878</span>
the classes are assigned according to the online status of the user. by default the above code displays all the elements having class available and unavailable. I want to use a checkbox as a filter which will display the based on the class name(if checkbox is clicked then display with classname available which will show only the online users). And when I uncheck the filter checkbox then all the having classes available and unavailable should be displayed.
I know how to filter data in angularjs but I have no idea how to filter data by classname using a checkbox.
How do I do it?
javascript angularjs
I'm using angularjs and here is my view :
<div style="width:70px;">
Show Online <input type="checkbox" ng-model="showonline" />
</div>
<div ng-repeat="user in users|filter:showonline">
<span type="button" class="{(isOnline()) ? 'available' : 'unavailable'}">user.id</span>
</div>
above code will generate html like this inside the div containing ng-repeat:
<span type="button" class="available">111</span>
<span type="button" class="available">121</span>
<span type="button" class="unavailable">88</span>
<span type="button" class="available">878</span>
the classes are assigned according to the online status of the user. by default the above code displays all the elements having class available and unavailable. I want to use a checkbox as a filter which will display the based on the class name(if checkbox is clicked then display with classname available which will show only the online users). And when I uncheck the filter checkbox then all the having classes available and unavailable should be displayed.
I know how to filter data in angularjs but I have no idea how to filter data by classname using a checkbox.
How do I do it?
javascript angularjs
javascript angularjs
asked Nov 19 '18 at 10:12
pravin navlepravin navle
507418
507418
Share a jsfiddle.
– Chinmoy Samanta
Nov 19 '18 at 10:29
add a comment |
Share a jsfiddle.
– Chinmoy Samanta
Nov 19 '18 at 10:29
Share a jsfiddle.
– Chinmoy Samanta
Nov 19 '18 at 10:29
Share a jsfiddle.
– Chinmoy Samanta
Nov 19 '18 at 10:29
add a comment |
1 Answer
1
active
oldest
votes
You can add one more variable showAvailable
to your scope to show the available users.
Also, I added one more parameter to object called isOnline
which should be added because you don't need to check again and again.
Here is the check I added to HTML : ng-show="!showAvailable || user.isOnline"
Demo:
function TodoCrtl($scope) {
$scope.users = [{
userid: 1,
isOnline: true
}, {
userid: 2,
isOnline: false
}, {
userid: 3,
isOnline: true
}]
this.name = "AngularJS";
$scope.showAvailable = false;
}
h1,
p {
font-family: Lato;
}
.available {
background-color: green;
width: 100px;
height: 100px;
}
.unavailable {
background-color: red;
width: 100px;
height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<div ng-app>
<div ng-controller="TodoCrtl">
<div ng-repeat="user in users|filter:showonline">
<span type="button" ng-show="!showAvailable || user.isOnline" ng-class="user.isOnline ? 'available' : 'unavailable'">{{user.userid +' '+ (user.isOnline ? 'available' : 'unavailable')}}</span>
</div>
Show Available only: <input type="checkbox" ng-model="showAvailable" />
</div>
</div>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372388%2ffilter-data-according-to-class-names-in-angularjs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can add one more variable showAvailable
to your scope to show the available users.
Also, I added one more parameter to object called isOnline
which should be added because you don't need to check again and again.
Here is the check I added to HTML : ng-show="!showAvailable || user.isOnline"
Demo:
function TodoCrtl($scope) {
$scope.users = [{
userid: 1,
isOnline: true
}, {
userid: 2,
isOnline: false
}, {
userid: 3,
isOnline: true
}]
this.name = "AngularJS";
$scope.showAvailable = false;
}
h1,
p {
font-family: Lato;
}
.available {
background-color: green;
width: 100px;
height: 100px;
}
.unavailable {
background-color: red;
width: 100px;
height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<div ng-app>
<div ng-controller="TodoCrtl">
<div ng-repeat="user in users|filter:showonline">
<span type="button" ng-show="!showAvailable || user.isOnline" ng-class="user.isOnline ? 'available' : 'unavailable'">{{user.userid +' '+ (user.isOnline ? 'available' : 'unavailable')}}</span>
</div>
Show Available only: <input type="checkbox" ng-model="showAvailable" />
</div>
</div>
add a comment |
You can add one more variable showAvailable
to your scope to show the available users.
Also, I added one more parameter to object called isOnline
which should be added because you don't need to check again and again.
Here is the check I added to HTML : ng-show="!showAvailable || user.isOnline"
Demo:
function TodoCrtl($scope) {
$scope.users = [{
userid: 1,
isOnline: true
}, {
userid: 2,
isOnline: false
}, {
userid: 3,
isOnline: true
}]
this.name = "AngularJS";
$scope.showAvailable = false;
}
h1,
p {
font-family: Lato;
}
.available {
background-color: green;
width: 100px;
height: 100px;
}
.unavailable {
background-color: red;
width: 100px;
height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<div ng-app>
<div ng-controller="TodoCrtl">
<div ng-repeat="user in users|filter:showonline">
<span type="button" ng-show="!showAvailable || user.isOnline" ng-class="user.isOnline ? 'available' : 'unavailable'">{{user.userid +' '+ (user.isOnline ? 'available' : 'unavailable')}}</span>
</div>
Show Available only: <input type="checkbox" ng-model="showAvailable" />
</div>
</div>
add a comment |
You can add one more variable showAvailable
to your scope to show the available users.
Also, I added one more parameter to object called isOnline
which should be added because you don't need to check again and again.
Here is the check I added to HTML : ng-show="!showAvailable || user.isOnline"
Demo:
function TodoCrtl($scope) {
$scope.users = [{
userid: 1,
isOnline: true
}, {
userid: 2,
isOnline: false
}, {
userid: 3,
isOnline: true
}]
this.name = "AngularJS";
$scope.showAvailable = false;
}
h1,
p {
font-family: Lato;
}
.available {
background-color: green;
width: 100px;
height: 100px;
}
.unavailable {
background-color: red;
width: 100px;
height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<div ng-app>
<div ng-controller="TodoCrtl">
<div ng-repeat="user in users|filter:showonline">
<span type="button" ng-show="!showAvailable || user.isOnline" ng-class="user.isOnline ? 'available' : 'unavailable'">{{user.userid +' '+ (user.isOnline ? 'available' : 'unavailable')}}</span>
</div>
Show Available only: <input type="checkbox" ng-model="showAvailable" />
</div>
</div>
You can add one more variable showAvailable
to your scope to show the available users.
Also, I added one more parameter to object called isOnline
which should be added because you don't need to check again and again.
Here is the check I added to HTML : ng-show="!showAvailable || user.isOnline"
Demo:
function TodoCrtl($scope) {
$scope.users = [{
userid: 1,
isOnline: true
}, {
userid: 2,
isOnline: false
}, {
userid: 3,
isOnline: true
}]
this.name = "AngularJS";
$scope.showAvailable = false;
}
h1,
p {
font-family: Lato;
}
.available {
background-color: green;
width: 100px;
height: 100px;
}
.unavailable {
background-color: red;
width: 100px;
height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<div ng-app>
<div ng-controller="TodoCrtl">
<div ng-repeat="user in users|filter:showonline">
<span type="button" ng-show="!showAvailable || user.isOnline" ng-class="user.isOnline ? 'available' : 'unavailable'">{{user.userid +' '+ (user.isOnline ? 'available' : 'unavailable')}}</span>
</div>
Show Available only: <input type="checkbox" ng-model="showAvailable" />
</div>
</div>
function TodoCrtl($scope) {
$scope.users = [{
userid: 1,
isOnline: true
}, {
userid: 2,
isOnline: false
}, {
userid: 3,
isOnline: true
}]
this.name = "AngularJS";
$scope.showAvailable = false;
}
h1,
p {
font-family: Lato;
}
.available {
background-color: green;
width: 100px;
height: 100px;
}
.unavailable {
background-color: red;
width: 100px;
height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<div ng-app>
<div ng-controller="TodoCrtl">
<div ng-repeat="user in users|filter:showonline">
<span type="button" ng-show="!showAvailable || user.isOnline" ng-class="user.isOnline ? 'available' : 'unavailable'">{{user.userid +' '+ (user.isOnline ? 'available' : 'unavailable')}}</span>
</div>
Show Available only: <input type="checkbox" ng-model="showAvailable" />
</div>
</div>
function TodoCrtl($scope) {
$scope.users = [{
userid: 1,
isOnline: true
}, {
userid: 2,
isOnline: false
}, {
userid: 3,
isOnline: true
}]
this.name = "AngularJS";
$scope.showAvailable = false;
}
h1,
p {
font-family: Lato;
}
.available {
background-color: green;
width: 100px;
height: 100px;
}
.unavailable {
background-color: red;
width: 100px;
height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<div ng-app>
<div ng-controller="TodoCrtl">
<div ng-repeat="user in users|filter:showonline">
<span type="button" ng-show="!showAvailable || user.isOnline" ng-class="user.isOnline ? 'available' : 'unavailable'">{{user.userid +' '+ (user.isOnline ? 'available' : 'unavailable')}}</span>
</div>
Show Available only: <input type="checkbox" ng-model="showAvailable" />
</div>
</div>
answered Nov 19 '18 at 10:41
Just codeJust code
10.4k53066
10.4k53066
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372388%2ffilter-data-according-to-class-names-in-angularjs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Share a jsfiddle.
– Chinmoy Samanta
Nov 19 '18 at 10:29