Display multiple values using ng-show

This is a Table Example

 <table class="table table-bordered table-responsive table-hover add-lineheight table_scroll">
        <thead>
            <tr>
                <th ng-hide="hidecolumn == key" ng-repeat="(key, value) in localNew[0]">
                    {{key}}
                </th>
            </tr>
      </thead>
      <tbody>
            <tr ng-repeat="test in localNew">
                <td ng-hide="hidecolumn == key" ng-repeat="(key, value) in test">
                    {{value}}
                </td>
            </tr>
        </tbody>
    </table>

In this example, the hidecolumn function hides specific columns that are not wanted to be displayed. For instance, in the controller data is defined as follows:

$scope.localNew = [{ 'name': 'name1', 'age': 24, "salary": 2500, "s": 5 }, { 'name': 'name2', 'age': 26, "salary": 2600, "s": 5 }];

The variable used to hide a column is:

$scope.hidecolumn = "salary";

This functionality works correctly.

If there is a need to hide multiple columns, the scope variable can be defined like this:

$scope.hidecolumn = "name,salary";

Instructions on managing the HTML table to hide multiple columns would be greatly appreciated.

Answer №1

It is recommended to utilize an array instead of a string in this scenario:

$scope.hiddenColumns = ['name', 'salary'];

Implement a function to determine whether the current column should be hidden or not:

$scope.shouldHideColumn = function(column) {
  if ($scope.hiddenColumns.includes(column.salary)) {
    return true;
  }

  return false;
};

Then, incorporate the following code snippet into your HTML:

<th ng-hide="shouldHideColumn(value)" ng-repeat="(key, value) in localNew[0]">
  {{key}}
</th>

Answer №2

Utilizing the Array with its handy indexOf functions is an effective way to achieve this:

$scope.hidecolumn = [ "name", "salary" ];

// Displaying Content

<td ng-hide="hidecolumn.indexOf(key) !== -1" ng-repeat="(key, value) in test">
  {{value}}
</td>

Answer №3

If you prefer, another way to accomplish this is as follows:

<td ng-hide="hidecolumn.indexOf(key) !== -1" ng-repeat="(key, value) in test">
    {{value}}
</td>

Answer №4

Have you given this a shot?

<tr ng-repeat="(index, item) in data" ng-show="index !== 'name' && index !== 'age'">
  {{item}}
</tr>

Answer №5

To use the function, you can do this:

ng-hide="hidecolumn(key)"

Here is the JavaScript code:

  var hideColumn = function(key) {
    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    if (fruits.indexOf(key)) === 0 return false;
  }

The ng-hide attribute will hide the element if the function returns true.

Answer №6

If you want to ensure consistency in your code and avoid repeating the same logic, consider creating a custom method for arrays called contains that checks if an element is present:

Array.prototype.contains = function contains(obj) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
};

Once you have defined this method, you can create your array like so:

$scope.hiddencolumns = [ "name", "salary" ];

Now you can easily check if an element exists in the array using the contains method:

<td ng-hide="hiddencolumns.contains(key)" ng-repeat="(key, value) in test">
    {{value}}
</td>

To convert a comma-separated string into an array, you can use the following code:

var array = $scope.hidecolumn.split(',');

Answer №7

After transforming my string into an array using the code var array = string.split(',');, I was able to achieve the outcome I wanted.

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

Guide on how to synchronously update a value following an Ajax request

Having an issue fetching data from my Node (Express) backend server to React frontend. Whenever I try to access the values at specific array indexes, they come out as undefined. I am sure there is a mistake in my approach. Can someone please provide me wit ...

Utilize AJAX to fetch additional data upon clicking the ID row in PHP

I am looking to display additional PHP data when a row with $row->student id is clicked. Upon clicking the row->studentid, the data should be shown as an AJAX response in the showmore div. Below is a section of the index.php code. <div class ...

Utilizing NodeJS to Schedule Emails based on User Input within React Application

In my ReactJS application, users have the ability to submit time, date and text values that are then sent to a NodeJS backend. Check out the current NodeJS code below: app.post("/send", function (req, res) { let mailOptions = { from: `${req.b ...

Discovering the most efficient route on a looped set of items

My question may not be fully comprehensible, but essentially it involves the following scenario: I have created an interactive presentation that displays static images in a sequence to generate an animation. By clicking the Play button, the animation prog ...

An algorithm designed to categorize data points and display their frequency

Enter the data into the system [ { “Product”: “Notebook”, “Action”: “Add” }, { “Product”: “Pen”, “Action”: “Sell” }, { “Product”: “Pen”, “Action ...

PHP form headaches: issues with submitting and posting

I'm having trouble with the submit button in my PHP code. Here's what I have so far (it's for a website where users can rate things): <form method="POST> <input type="radio" name="person" value="1" /> <input type="radio" name ...

Update component state based on input value

Trying to update the state of an input within a component and utilize it in app.js. How can this be achieved using props? Attempted onChangeText={(tips) => this.setState({comment})} but only receiving undefined results.. const Comments = (props) => ...

"Adding the Whats app share button underneath your Blogger post: A step-by-step guide

Can you help me with adding a Whats App share button below my post? Here is the CSS code on my website: .tl-postShare{text-align:right;} .tl-postShare ul{display:inline-block;} .tl-postShare ul li{margin:0;} .tl-tagnShare{position:relative;overflow:hidde ...

What is the process for transmitting data using an Ajax Query to a Controller and subsequently displaying a new JSP page returned by the Controller?

One issue I am facing involves sending data to a Spring Boot Controller through an AJAX Query and then loading a new JSP page. When I send data to the url in the AJAX Query, which matches the URL in my controller class, it executes the code within that met ...

Can Bootstrap 4 CSS be used to display a yellow triangle with text in the bottom right corner of a card?

I'm having difficulty modifying the CSS to display a yellow triangle containing text in the bottom right corner of a Bootstrap 4 card. Is there a way to achieve this effect using CSS? This is how the current CSS output looks in a Bootstrap 4 card: h ...

Bootstrap: utilizing a layout where text appears on two lines rather than one

Hey there, I'm currently utilizing Bootstrap 4.5 and attempting to create an aesthetically pleasing footer for my university website. I've implemented the following code: <div class="container-fluid d-flex justify-content-center"> ...

Enhance your Ionic 2 application with a versatile barcode scanner featuring multiple fields

https://i.sstatic.net/YtwcZ.jpg Exploring the Barcode Scanner Plugin in Typescript scanBarCode() { this.barcodeScanner.scan().then(barcodeData => { this.scannedbarCode = barcodeData.text; }, (err) => { con ...

Pre-task for Angular's $resource request

Is there a way to customize a $resource function so that it can modify the data before sending a POST async request? $resource(env.SERVER + '/res/:id', { id: '@id' }, { test: { method: 'POST', beforeRequest: functio ...

Integrating AJAX feedback messages into Formcarry

I'm currently diving into the world of React as I work on my inaugural website using this framework. My main hurdle right now is creating a contact form. To simplify the process, I opted to use Formcarry and integrate it with my React App. However, I ...

Error encountered in React JS: Anticipated input was "CALC", "dimension", "number", but received unexpected end of input. CompileError originates from undefined CSS selector

After completing my entire project, I encountered an error while running yarn build. The error message from Yarn is as follows: Parse error on line 1: ^ Expecting "CALC", "LPAREN", "ADD", "SUB", "FUNCTION" ...

Present data outside of a traditional table format

Looking to improve the layout of my page with a title, header, and 3 links. Is there a way to have them displayed next to each other with some space in between? I've tried using tables but it didn't work as they ended up too close together. Open ...

Scaling boxes responsively according to screen size utilizing Bootstrap

Creating a portfolio website with a carousel section to display completed projects in a 2x2 grid layout, transitioning to a 1x4 on smaller screens is proving to be quite challenging. After trying various methods from Bootstrap documentation and YouTube tut ...

Exploring the ins and outs of console interactions in JavaScript

I have come across a problem on Hacker Rank that seems quite simple. The task involves working with N strings, each of which has a length no more than 20 characters. Additionally, there are Q queries where you are provided a string and asked to determine ...

Displaying HTML tags stored in a PHP variable correctly within OpenCartOREffect

I am facing an issue where HTML content stored in my MySQL database is being displayed as plain text in the browser instead of rendered as HTML. My project is based on OpenCart, and I suspect that there might be references in other pages affecting the ren ...

"Upon setting the state in React, the Full Calendar refreshes and retrieves events

My current setup involves using the FullCalendar API to fetch events as shown below: <FullCalendar ref={calendarRef} plugins={[listPlugin, bootstrap5Plugin]} initialView='listMonth' ...