Display and conceal specific raw details data dynamically upon clicking?

I am looking to display and hide table row details dynamically with every button click. However, whenever I click on the span glyphonic icon, all details data in the tr's are opened by default. What can I do to only make this data visible for the elements that have been clicked on? (in my scenario, I only want to show certain data when the user clicks on it):

<form role="form">
<script  cam-script type="text/form-script">
$scope.selectedRow = null;
   var  persons = $scope.persons = [
        {
           "id": 1,
           "name": "name1",
           "age":"454",
           "description":"this is a simple name"

       } ,
       {
           "id": 2,
           "name": "name2",
            "age":"4543",
           "description":"this is a simple name"

       },
      {
           "id": 3,
           "name": "name2",
            "age":"4543",
           "description":"this is a simple name"

       }
       ];
   camForm.on('form-loaded', function() {
          camForm.variableManager.createVariable({
            name: 'persons',
            type: 'Array',
            value: persons
          });
 });



$scope.pageNumber = 0;
$scope.IsHidden=true;
    $scope.setPageNumber = function(index) {
        $scope.pageNumber = index;
          $scope.IsHidden = $scope.IsHidden ? false : true;
    }
</script>


<div class="container">
<table class="table table-condensed" style="border-collapse:collapse;">

    <thead>
        <tr>
            <th>id</th>
            <th>name</th>
            <th>age</th>
            <th>&nbsp;</th>
        </tr>
    </thead>

    <tbody ng-repeat="item in persons " >
       <tr >
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.age}}</td>
            <td><button class="btn btn-default btn-xs"  ><span class="glyphicon glyphicon-eye-open" ng-class="{$index == pageNumber }" ng-click="setPageNumber($index)($index)"></span></button></td>
        </tr>
         <tr ng-hide="IsHidden">
            <td>

    <label for="id" class="control-label">details</label>
     <div class="controls">
      <input id="description" class="form-control" type="text" ng-model="item.description" required  readonly/>
    </td>
        </tr>
    </tbody>
</table>
</div>
</form>  

Answer №1

Here is the requested code snippet:

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

myApp.controller('myController', ['$scope', function($scope) {
  $scope.people = [
        {
           "id": 1,
           "name": "John",
           "age":"30",
           "description":"John is a web developer"
       } ,
       {
           "id": 2,
           "name": "Jane",
            "age":"28",
           "description":"Jane is a designer"
       },
      {
           "id": 3,
           "name": "Alex",
            "age":"35",
           "description":"Alex is a project manager"
       }
       ];
    $scope.toggleInfo = function (index){
    if($scope.people[index].showDetails == undefined){
    $scope.people[index].showDetails = true;
    }else{
    $scope.people[index].showDetails = !$scope.people[index].showDetails;
    }    
    }
}]);
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form ng-app="myApp" ng-controller="myController">
<table class="table table-condensed" style="border-collapse:collapse;">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Age</th>
            <th>&nbsp;</th>
        </tr>
    </thead>

    <tbody ng-repeat="person in people">
       <tr>
            <td>{{person.id}}</td>
            <td>{{person.name}}</td>
            <td>{{person.age}}</td>
            <td><button class="btn btn-default btn-xs" ng-click="toggleInfo($index)">Toggle Details</button></td>
        </tr>
         <tr id="{{details + $index}}" ng-show="person.showDetails">
            <td>
    <label for="id" class="control-label">Details:</label>
     <div class="controls">
      <input id="description" class="form-control" type="text" ng-model="person.description" required readonly/>
      </div>
    </td>
        </tr>
    </tbody>
</table>
</form>

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

Combining an unlimited amount of elements in an array using JavaScript

Currently, I am facing a challenge while trying to develop an application using Javascript. What I aim to achieve is a webpage that collects input values from a textbox and stores them in an array. Subsequently, I need to create a function that can add t ...

Include a quantity dropdown menu on the cart page of your Shopify store's debut theme

Hey there! I'm currently customizing the Shopify Debut theme and I'm trying to incorporate a quantity selector as a dropdown on the cart page. Unfortunately, I'm encountering some issues with this implementation. I've managed to succes ...

Unable to showcase drop-down menu as ajax output on WordPress platform

I am facing an issue with displaying the output of two drop-down boxes - one with custom taxonomy and the other with its custom post under the currently selected taxonomy. To address this, I have implemented an on-change function. However, the output is no ...

Using Three.js to ensure that child object3d expands within the boundaries of its parent object

In this scenario, I have 2 Object3Ds with one nested inside the other: var obj1 = new THREE.Object3D(); var obj2 = new THREE.Object3D(); obj1.add(obj2); Assuming obj1 is AxB units wide, how can I instruct obj2 to match that size? Essentially, how can I m ...

Looking to display a JSON-based menu using AngularJS?

Having trouble displaying a dropdown menu when trying to print a menu from a JSON file. https://i.sstatic.net/fNLxw.png JSON Data [{ "Option": "Request", "Name": "<a href=\"#/request\"><i class=\"material-icons left\ ...

React class cannot be exported at this time

Here is the content of two files: Product.jsx: class Product extends React.Component{ render(){ return ( <div className='item'> <div className='image'> <img src ...

Creating Functional Tabs Using CSS and JavaScript

I've been experimenting with this code snippet, trying to get it to work better. It's still a work in progress as I'm new to this and have only customized it for my phone so far. The issue can be seen by clicking on the Projects and Today ta ...

Making an "associated" route the active route within Aurelia

In my Aurelia application, I have implemented two routes: a list route called Work and a detail route called WorkDetail. Currently, only the list route is visible in the navigation menu: Home | *Work* | Contact | . . . When users navigate to the W ...

The horizontal axis displays the hours from 12:00 to 12:55 instead of specific calendar dates

This script creates a highchart that includes 2 Y axes and 1 X axis: $('#main_chart').highcharts({ chart: { zoomType: 'xy' }, title: { text: 'Overview' }, xAxis: [{ ...

Can Reactjs handle passing various properties to a single function using identical buttons?

[Newbie with React]: I'm facing an issue with my code that seems simple. I have two number counters with buttons to add a number to them. I want these buttons to perform the same math function but with the property assigned to them. However, it seems ...

Using the prompt command in HTML is not supported

I am having trouble with incorporating the javascript prompt command into my HTML website. I need some assistance in identifying where I went wrong: <head> <script type="javascript"> var pass=prompt("What is the password") ...

What is the best way to attach an event again in jQuery?

My JavaScript/jQuery code initiates a listener to highlight DOM elements when a button is clicked. By clicking the button, the listener event starts (e.g. highlight : function()). If I click on any part of the webpage, the listener stops. However, if I ...

Dynamic sorting of columns

Looking to dynamically reorder columns after loading data with the DataTables plugin for jQuery. Usually, columns are reordered upon table creation, but I need to do so after data is fetched from the server. After drawing the table and fetching data, I wa ...

Tips for moving an element to the end of an array

Patients' data is stored in the MongoDB database, and all patients are mapped through on the frontend to display a list. An additional boolean value indicates whether a patient is archived or not. If a patient is archived, it should be displayed at th ...

Having difficulty passing a function as a parameter from a NextJS component

I have a code snippet like this in a NextJS component: const [currentGPS, setCurrentGPS] = useState({coords:{latitude:0.0,longitude:0.0}}) useEffect(() => { utl.getGPSLocation( (v:{coords: {latitude:number; longitude:n ...

Utilize JavaScript to trigger a div pop-up directly beneath the input field

Here is the input box code: <input type='text' size='2' name='action_qty' onmouseup='showHideChangePopUp()'> Along with the pop-up div code: <div id='div_change_qty' name='div_change_qty&ap ...

Error encountered upon initializing Node-RED due to the presence of an unexpected token while incorporating the NPM module "file-exists" into the

Currently, I'm in the process of developing an application using Node-RED and I'm looking to incorporate some NPM modules into my project. One particular module from James Thom caught my attention, called node-red-contrib-npm, which automates the ...

Alter the functionality of the input element that has a tag of 'multiple'

I am encountering an issue with a form that allows users to upload multiple photos. When selecting multiple files at once, everything works as expected. However, if a user wants to add images one by one, each new addition deletes the previous file due to t ...

AngularJS directive: Cookie value not being applied to data binding operation

I've developed a directive that includes a dropdown list of organizations to which the user is assigned: .directive('orgList', ['$rootScope', '$state', 'Auth', 'ipCookie', function ($rootScope, $state ...

CORS problem arises when making an Ajax API request

I am attempting to retrieve an API response from Sprout Video using their Javascript API. However, I am encountering a CORS issue with the request. While I can successfully receive a response in Postman, my website is not able to do so. Despite searching ...