filter data according to class names in angularjs












0















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?










share|improve this question























  • Share a jsfiddle.

    – Chinmoy Samanta
    Nov 19 '18 at 10:29
















0















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?










share|improve this question























  • Share a jsfiddle.

    – Chinmoy Samanta
    Nov 19 '18 at 10:29














0












0








0








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?










share|improve this question














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|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 10:12









pravin navlepravin navle

507418




507418













  • 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





Share a jsfiddle.

– Chinmoy Samanta
Nov 19 '18 at 10:29












1 Answer
1






active

oldest

votes


















0














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>








share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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>








    share|improve this answer




























      0














      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>








      share|improve this answer


























        0












        0








        0







        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>








        share|improve this answer













        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>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 10:41









        Just codeJust code

        10.4k53066




        10.4k53066
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini