Sorting information by class titles in AngularJS

Working with angularjs, take a look at my view code :

<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>

The snippet above will output HTML like the following within 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 applied based on the online status of each user. By default, it displays all elements with the "available" and "unavailable" classes. I aim to implement a checkbox as a filter to selectively display elements by their class name (checking the box should only show "available" users). When unchecked, both "available" and "unavailable" elements should be visible. I am familiar with filtering data in angularjs but unsure how to filter by classname using a checkbox. How can I achieve this?

Answer №1

If you want to display the available users, consider adding a new variable called showAvailable to your scope. I also recommend including an additional parameter in the object named isOnline to avoid repetitive checks.

To implement this, add the following code snippet to your HTML:

ng-show="!showAvailable || user.isOnline"

Check out this demonstration:

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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Next.js developer encounters an issue where the build fails due to a ReferenceError on a client component, stating that "window

Just starting out with nextjs, I'm sticking to using only the basic features without diving into any advanced functionalities. During the next build process, I encountered an issue where 6 paths failed because of a ReferenceError: window is not defin ...

Having difficulty launching a new window within an app built with Electron and Nexjs (Nextron)

Attempting to launch a (Nextjs-generated) page in a new window is causing an error message to appear: Uncaught ReferenceError: global is not defined Here is the full error: react-refresh.js?ts=1665849319975:10 Uncaught ReferenceError: global is not de ...

Trigger an Ajax function using a button within a Bootstrap modal

I need to trigger an ajax function after selecting an option from a bootstrap confirmation modal. The modal will appear by calling the remove(parameter) function. Any assistance would be greatly appreciated function remove(parameter){ // $("#remove-mod ...

Encountering the issue: receiving an error message stating that shuffle(...) is undefined within

Whenever I try to add it into the app.js file, it keeps coming up as undefined. app.js (function () { var tkcApp = angular.module('TKC', []); var shuffle = function (o) { for (var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = ...

Execute a node script file at random intervals

I've been utilizing forever to maintain the continuous execution of a node script: forever start script1.js However, my requirement is to run these files in a random order... For instance: Execute node script1.js Execute node script2.js Run script ...

displaying the local path when a hyperlink to a different website is clicked

fetch(www.gnewsapi.com/news/someID).then(response => newsurl.href = JSON.stringify(data.articles[0].url) fetch('https://gnews.io/api/v3/search?q=platformer&token=642h462loljk').then(function (response) { return response.json(); }).th ...

Using Gmail in conjunction with Heroku for email delivery

After completing an order in my web app, I want to automatically send a confirmation email. I decided to use Nodemailer as it is a popular npm package for this purpose. I successfully coded the functionality and tested it in my local environment. Howeve ...

Preventing the form from being enabled by default when using 'ng-model'

I have an input field labeled name. I have set a default value of 'Enter your Name' using the ng-model, but this causes the form submit button to become enabled. How can I prevent the submit button from being enabled by the default value? Here i ...

SOLVED: NextJS restricts plugins from modifying HTML to avoid unnecessary re-rendering

My current scenario is as follows: I am in the process of developing a website using NextJS (SSR) I have a requirement to load a script that will locate a div element and insert some HTML content (scripts and iframes) within it. The issue at hand: It se ...

Step-by-step guide to showing the contents of every file in a directory on the console using node.js or express

Having trouble retrieving file contents from a folder using Node.js I can successfully retrieve the contents of one file using the read function, but unable to retrieve contents of all files at once. ...

In AngularJS, the process of replacing text using a filter can be achieved by following

Currently in the process of learning Angular, but I've hit a roadblock and can't seem to find a solution. I have a website that provides USPS shipping options, and I need to replace the default text with my own. The vendor's option shows &ap ...

Discovering a specific property of an object within an array using Typescript

My task involves retrieving an employer's ID based on their name from a list of employers. The function below is used to fetch the list of employers from another API. getEmployers(): void { this.employersService.getEmployers().subscribe((employer ...

Troubleshooting the Autocomplete Problem in Kendo UI AngularJS with ngMap

My current challenge involves attempting to combine the autocomplete feature of ngmap with Kendo UI. However, I am encountering an issue where the placeChanged event is not triggering and the autocomplete widget is not displaying. The input placeholder i ...

When attempting to submit form data, req.file returns as undefined

I'm currently facing an issue with my file upload functionality. I have a form that sends data to the controller in my routes file, but when the data is posted, the req.file appears as undefined. My goal is to upload the image to the public/images dir ...

Is there a way to capture the input from the text box and store it in the local storage?

I'm confused about why the data entered into the input box is not being saved to local storage. <body> <input id="name"> <button onclick="bob()"> save </button> </body> <script> const user = document.getElementByI ...

Restricting the Vue for-loop results to display only the current item in use

Currently, I am utilizing a for-loop to retrieve all of my posts, followed by employing a partial to obtain a list of all the usersThatUpvoted on that post. <div v-for="p in posts" style="padding: 16px"> <div> &l ...

I am currently experiencing difficulties with loading files in Magento even though they are present on the server

I am experiencing difficulties with a Magento 1.5.1 installation that was not a fresh setup, but rather one that was transferred to another server (files and database copied over). The issue I am facing is related to the failure of my Javascript files to ...

Is there a way to transfer the chosen maximum and minimum price values to a JavaScript function within a select tag in HTML?

I have a search form that includes select options with two values. However, I want to have two select options for both the Max and Min price values. <input type="hidden" id="budget_min" name="filter_budget_min" value="0" /> <select onchange="upda ...

Suggestions for organizing files

I am in search of an optimal file structure for a website that incorporates an Angular front-end and a Php Slim API. My current setup is as follows: index.html = Starting point for Angular api/ index.php = Starting point for Slim .htaccess = redirects ...

Conceal specific pages within the DataTable without deleting them

Currently, I am facing an issue where the dataTable paginates and removes the DOM pages along with the data. My goal is to extract all the data from the dataTable and convert it to JSON without losing access to the DOM when pagination occurs. I want to m ...