"Interact with JSON data using AngularJS and JavaScript by clicking a button to edit

Here is my code in Plunker. Clicking on the Edit button should allow you to edit the details. Check out the full project here.

<title>Edit and Update JSON data</title>
  <div>
    {{myTestJson.name}}
     <table><tbody>
          <tr ng-repeat="(key, value) in myTestJson.MyTest[id].Main  track by $index"  >
            <td><label>{{key}}: </label> 
      <input placeholder="" type="text" ng-model="myTestJson[key]">
             </td>
              </tr>
          </tbody>
      </table>
      <button value="Update and Save" id="saveButtonId" ng-click="saveUpdate()">Update/Save</button>
  </div>

Answer №1

Give this a try:

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

myApp.controller('studentController', function($scope, $http) {
  let url = "Student.json";
  $http.get(url).then(function(response) {
    $scope.students = response.data;
  });

  $scope.removeElement = function(array, index) {
    array.splice(index, 1);
  };

});

 <div ng-controller="studentController">
   <h1>Student Information </h1>

   <table>
     <tr>
       <th>Name</th>
       <th>Roll No</th>
       <th>Percentage</th>
     </tr>
     <tr ng-repeat="student in students">
       <td>
         <input type="text" ng-model="student.Name" ng-show="student.edit">
         <span ng-hide="student.edit">{{ student.Name }}</span>
       </td>
       <td>
         <input type="text" ng-model="student.RollNo" ng-show="student.edit">
         <span ng-hide="student.edit">{{ student.RollNo }}</span>
       </td>
       <td>
         <input type="text" ng-model="student.Percentage" ng-show="student.edit">
         <span ng-hide="student.edit">{{ student.Percentage }}</span>
       </td>
       <td>
         <button id="button2" ng-hide="student.edit" ng-click="student.edit = true">Edit</button>
         <button id="button4" ng-hide="student.edit" ng-click="removeElement(students, $index)">Delete({{$index}})</button>
         <button id="button3" ng-show="student.edit" ng-click="student.edit = false">Submit</button>
       </td>
     </tr>
   </table>
 </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

What is the best way to execute a .js file with a Mocha suite repeatedly within a loop?

I have a collection of .js test files and I am looking for a way to execute the tests in each file multiple times. Ideally, I would like to run them 5 or 10 times consecutively. Even if it's just one file at a time, but running it multiple times. I a ...

Modifying the CSS Property of a Non-React Element within a React Application

I am currently in the process of developing a React-based application that will operate within a Wordpress page. The application is mostly self-contained, but I am interested in being able to toggle the visibility of a div element on the page that is not p ...

Modify the code to interpret a new JSON structure

I have a piece of code that is designed to read JSON data: $.getJSON("data.json", function(data){ var output = ''; $.each(data, function(index, value){ output += '<li>' + value.title + '</li>'; } ...

NodeJS error: The 'error' event was not properly handled, resulting in a throw er

I've developed a basic web application using React, node-postgres, expressJS, and PostgreSQL DB. The app includes two input fields and a button. Upon clicking the button, the values are saved in the database. While running the ExpressJS server with ...

Concerning Java's Map and Array functionalities

When working with Javascript, we have the ability to create statements like the one below. var f_names = { 'a' : 'Apple', 'b' : 'Banana' 'c& ...

Ensuring nested JSON key existence before rendering in React

When retrieving data from MarvelAPI, there is no certainty that the key being accessed will be available. Therefore, I am implementing if statements to check each key before accessing them. The current code below appears quite messy. Is there a more effic ...

Is it possible to view the code within the .js files located in the api directory of NextJS using web browsers?

Is it possible to view the code of ".js files" in the "api folder" of NextJS using browsers? I came across a post on Stack Overflow where someone asked if Next.js API is back-end, and one of the answers stated: The back-end or server-side of Next.js res ...

Increase the count for each category with the help of JavaScript

Currently, I am working on an application using PHP and have 180 vendors registered in the database. Each vendor has a unique ID stored in the database. I need to create a system where every time a user clicks on the "view details" button for a specific ...

Add a JQuery function to the button element in the HTML code

Currently, I am experimenting with AJAX to load a series of consecutive pages within a main page. The process is illustrated in the image below: Thanks to the guidance from this community, I have learned how to call content from other pages by utilizing t ...

Can we modify the styling of elements in Angular based on an object property?

I have a class named Task with the following properties: export class Task { name: string; state: number; } Within my component.ts file, I have an array of objects consisting of instances of the Task class (tasks). When displaying them in the tem ...

Troubleshooting Database Saving Problem with Vue.js and Ruby on Rails

Trying to configure a nested form with Vue.js in a Rails application to save ingredients for a recipe. While all other details of the ingredients are saving correctly, the ingredient name (from an input field) is not getting saved in the database. How can ...

Referencing an object by using a variable containing its name

I need a way to reference an object using a variable with its name in it. I've figured out how to do this for properties and sub-properties: var req = {body: {jobID: 12}}; console.log(req.body.jobID); //12 var subProperty = "jobID"; cons ...

Issue arises when Protractor is unable to compare a variable with a web element due to unresolved promises

My strategy for this examination was to extract the inner text of an element, modify it, and then verify that the change had taken place by comparing the element with the variable. var fixedValue = element(by.xpath('/html/body/section/div/section/sec ...

Leveraging TypeScript enums in conjunction with React to anticipate a string prop

Trying to enforce strict typing for a Button component in React. How can I ensure a prop has a specific string value? The current effort yields Type '"primary"' is not assignable to type 'ButtonLevel' enum ButtonLevel { Primary = ...

What's the reason Rails is not recognizing relative JavaScript files?

app/assets/javascripts/application.js: //= require jquery //= require jquery_ujs //= require_tree . //= require bootstrap.min app/assets/javascripts/economy.js: $(document).ready(function() { console.log("loaded file"); }); app/views/economy/index.ht ...

Align boxes in the center within a DIV container

Here is the box design that I have created: The green color boxes are generated dynamically inside a "col-md-10" div. If there are less than 3 boxes in the second row, I would like to center align them. For example, in this case, I want the "Bi-monthly" b ...

Jquery ajax is failing to achieve success, but it is important not to trigger an error

My jQuery ajax request seems to be stuck in limbo - it's not throwing an error, but it also never reaches the success function. This is how my code looks: function delete() { $("input[name='delete[]']:checked").each(function() { ...

I'm new to Angular, so could you please explain this to me? I'm trying to understand the concept of `private todoItems: TodoItem[] = []`. I know `TodoItem` is an array that

//This pertains to the todoList class// The property name is todoItems, which is an array of TodoItem objects fetched from the TodoItem file. I am unable to make it private using "private todoItems: TodoItem[] = []," is this because of Dependency Injectio ...

Storing AngularJS route components in the cache for optimal performance (keep-alive)

I'm looking for a way to cache the state of a component A so that it doesn't re-render every time I navigate away and back to it. This component also makes a slow API call in its constructor. I want to maintain this initial state throughout the u ...

Error: The term 'RequestCompleted' is not recognized by Microsoft JScript

Does anyone have any suggestions? The error above occurs when this code is executed: Sys.WebForms.PageRequestManager.getInstance().add_endRequest(RequestCompleted); Specifically within this section: <script language="javascript" type="text/javas ...