Display data in an HTML table based on user search input using Angular

I am currently working on integrating a JSON file into my project, which will eventually be accessed through an API from a server. Right now, I am pulling data directly from an object.

My goal is to build an HTML file that features a table with navigation tabs for each header element. Each tab should also include a search box and a submit button.

The initial state of the table should be empty. When a user searches for a specific value (such as ID, nickname, email, etc) within one of the headers, the table should populate with the JSON data that matches or contains part of the search query.

As a newcomer to Angular syntax, I am struggling to conceptualize how this functionality should be implemented within the framework.

(function() {

    var app = angular.module('tool', []);

    app.controller('searchController', function() {
      this.info = data.people;

      this.findId = function(idInput) {
        angular.forEach(that.id, function(value, key) {
          if (value.contains(idInput)) {
            // Unsure about what code needs to go here.
          }
        });
      };

    });

    var data = {
      "people": [{
          "id": "2245231",
          "nickname": "heyyman",
          "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8be2e5ede4cbece6eae2e7a5e5eeff">[email protected]</a>",
          "lastIp": "127.0.0.1",
          "regIp": "127.0.0.1",
        }, {
          "id": "2245232",
          "nickname": "heyyman2",
          "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9b0b7bfb6eb99beb4b8b0b5f7b7bcad">[email protected]</a>",
          "lastIp": "127.0.0.2",
          "regIp": "127.0.0.2",
        }
      };

    })();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<main>
  <table ng-controller="searchController as search">
    <thead>
      <tr id="tableNavigation">
        <td></td>
        <td>ID
          <input type="text" ng-model="idInput">
          <input type="submit" ng-click="findId(idInput)">
        </td>
        <td>Nickname</td>
        <td>Login / Email</td>
        <td>Last IP</td>
        <td>Registration IP</td>
      </tr>
    </thead>
    <tbody id="tableCanvas">
      <tr ng-repeat="people in search.info" ng-class-even="'even'">
        <td></td>
        <td>{{people.id}}</td>
        <td>{{people.nickname}}</td>
        <td>{{people.email}}</td>
        <td>{{people.lastIp}}</td>
        <td>{{people.regIp}}</td>
      </tr>
    </tbody>
  </table>
</main>

This is the progress I have made so far, along with a link to the JSFiddle sandbox where you can see the implementation.

http://jsfiddle.net/ho00cLkk/

If you find any confusion or lack of clarity in my explanation, please let me know.

Answer №1

Upon reviewing your inquiry, it appears to contain two distinct sections:

  1. How can I apply filters to the displayed items?
  2. Is there a way to conceal all outcomes until the initial search is conducted?

Given that the second question does not pique my interest (due to numerous potential solutions and lack of a universal issue), I will focus on addressing the first query. I recommend referring to the official documentation available at: https://docs.angularjs.org/api/ng/filter/filter. By following the guidelines outlined in the documentation, achieving this task is straightforward and does not necessitate the use of submit buttons.

<table>
    <thead>
        <tr>
            <th>
                Name
                <input type="text" ng-model="searchProps.name">
            </th>
            ...
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="person in people | filter:searchProps">
            ...
        </tr>
    </tbody>
</table>

(in this scenario, ensure that people corresponds to your search.info). The table rows will automatically undergo filtration processes, displaying only those items with properties that partially match the values identified within searchProps.

An illustrative CodePen demonstration can be accessed here: http://codepen.io/anon/pen/jEEZaa

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

"Automating the process of toggling the edit mode of a contenteditable element – how is

Is there a specific event I can use to programmatically start or stop editing a contenteditable element, similar to when the user focuses or blurs it? I have tried using .focus(), .click(), and even setting the selection to its content, but none of them se ...

Troubleshooting KuCoin API: Dealing with Invalid KC-API-SIGN Error and FAQs on Creating the Correct Signature

I want to retrieve open orders for my account using the following code snippet: import { KEY, PASSWORD, SECRET } from "./secrets.js"; import CryptoJS from "crypto-js"; const baseUrl = 'https://api.kucoin.com' const endPointOr ...

How to modify a value in a document within a MongoDB collection

I'm having an issue with updating the 'panel' field in both the cards collection and projects collection. Here is my code snippet: const project = await Project.findOne({"code":currentUser.groupcode}); // this works const ...

Enhance a link using jQuery to allow it to expand and collapse

I am currently working on an MVC view that features a 3-column table showcasing a list of products. The first column, which contains the product names, is clickable and directs users to a specific product page. I am looking to implement functionality where ...

Sending PHP output data to jQuery

Trying to implement this code snippet /* Popup for notifications */ $(document).ready(function() { var $dialog = $('<div></div>') .html('message to be displayed') .dialog({ ...

sending data to Google Maps and updating it

OPERATING SMOOTHLY My requirement is to display only the items that match the entered username and password. I am unable to pass PHP variables to JavaScript in order to query the database and update the map. The following code is functioning correctly (p ...

What is the best way to make text appear as if it is floating in Jade or HTML?

Currently, I am facing an issue with a Jade file that displays an error message when a user tries to log in with incorrect credentials. The main problem is that this error message disrupts the alignment of everything else on the page, as it is just a regul ...

Execute JavaScript code once all the AngularJS template directives are fully loaded

On my HTML page, I am utilizing AngularJS template directives. Here is an example of how it looks: <div class="row"> <div class="col-sm-12"> <div logo></div> <div login-card></div> ...

What is the most effective way to delete a single value from a key with multiple values stored in local storage?

This question has come up before, but the solutions provided didn't work for me, which is why I am asking it again. Currently, I am storing values in an array that is then stored in local storage. Here is the object: data.items - 0: {id: 190217270, ...

Ways to incorporate conditional checks prior to running class methods

Seeking input on handling async data retrieval elegantly. When initializing a class with asynchronous data, I have been following this approach: class SomeClass { // Disabling strictPropertyInitialization private someProperty: SomeType public asy ...

Creating a Consistent Look for Italic Font Formatting in Tailwind

In an effort to establish consistent typography on a website being developed by my team, we have devised custom utility classes in Tailwind. One of these classes, small-italicized, also requires the text to be italicized. However, it seems that the fontSiz ...

Execute multiple JavaScript files dynamically by utilizing the same npm run command

My directory structure: app Model user.js post.js Contents of my package.json file: "scripts": { "migrate": "node ./app/Model/" } I am looking to execute JavaScript files via command line in a dynamic manner. For example: npm run migr ...

What is the best approach to setting up dynamic Vue routing?

I am working on implementing dynamic routing for the website that relies on changes in agreements. Here is the path tree I have set up: const routes = [ { path: "/", redirect: "/home", component: DefaultLayou ...

Enhancing code branch coverage using Istanbul

The code snippet provided has a branch coverage of only 50% (refer to the coverage report below). I am unsure how to enhance this as there are no if statements present. I suspect that Istanbul must utilize some form of measurement that I have yet to grasp ...

When navigating to a new route using history.push in React, it's important to ensure that the correct state is

My goal is to implement a smooth exiting animation with framer motion based on the user's current route and next destination. I specifically want the background to slide away only when transitioning from route A to route D. To achieve this, I decided ...

problem with saving session data

I am attempting to access data from another page using session storage. On my initial page, named home.html function go_to_faq(qnum){ window.open('FAQ.html', '_blank'); sessionStorage.setItem('key2', qnum); } <a s ...

Tips for troubleshooting the error "Cannot locate module mp3 file or its associated type declarations"

Seeking guidance on resolving the issue with finding module './audio/audio1.mp3' or its type declarations. I have already attempted using require('./audio/audio1.mp3'), but continue to encounter an error. ...

In JavaScript using Vue.js, you can compare various objects within an array and calculate the total sum of their values based on a shared

This task may pose a challenge for those new to programming, but seasoned developers should find it relatively simple. I've been searching for a solution without success so far, hence turning to this platform for help. Here's the scenario at han ...

When attempting to convert large objects into JSON using JSON.stringify, a RangeError is thrown due to

Trying to convert a massive JavaScript Object into a string using JSON.stringify in my Node.js application. The objects are extremely large (tens of mega bytes) and do not contain any functions. I need to save the serialized objects to a file. However, I a ...

AngularJS interprets expressions in the 'action' attribute

This afternoon I encountered a rather peculiar behavior with AngularJS. If "//" is present in an expression within the "action" attribute of a form, Angular will throw an interpolate error. Take a look at the code snippet below. When you run this code, t ...