Update angular table input values

I am currently facing an issue with ng-table while displaying my data. I noticed that when I enter data on the first page and then navigate to the second page, the values from the first page get cleared. Is there a way to retain the previously entered values? Please review my work files. Thank you.

var electricityApp = angular.module("myApp", ["ngTable"]);

electricityApp.controller(
'electricityCtrl',
function($scope, $http,NgTableParams) {
$scope.dmydata=[
{ name: "Sam", age: "35"},
{ name: "Ravi", age: "40"},
{ name: "Kumara", age: "50"},
{ name: "James", age: "60"},
{ name: "Sam2", age: "70"},
{ name: "Ravi2", age: "75"},
{ name: "Kumara2", age: "80"},
{ name: "James2", age: "85"},
{ name: "kamal", age: "90"},
{ name: "Sunil", age: "95"},
];


$scope.getDatas = function(){
$scope.tableParams = new NgTableParams({count:5}, { dataset:$scope.dmydata});
}
});
 <!DOCTYPE html>
<html lang="en-US">

<script src="angular.js"></script>
<script src="ng-table.min.js"></script>
<link rel="stylesheet" href="ng-table.min.css">
<link rel="stylesheet" href="bootstrap.min.css">
<script>

</script>
<body>
<div ng-app="myApp"> 
<div ng-controller="electricityCtrl">
<table ng-table="tableParams" ng-init="getDatas()" class="table table-condensed table-bordered table-striped" show-filter="true">
    <tr ng-repeat="user in $data">
        <td title="'Name'" sortable="'name'">{{user.name}} <input type="text" ng-model="mytxt[$index]"/> </td>
        <td title="'Age'" sortable="'age'">{{user.age}} <select ng-model="myselect[$index]"> <option> 1</option> <option> 2</option> <option> 3</option></select></td>
    </tr>
</table>
</div>
</div>
</body>
</html>

Answer №1

The connection between the model and the input field is not set up correctly.

Solution

  • Replace mytxt[$index] with user.name in the input field
  • Replace myselect[$index] with user.age in the select field

var electricityApp = angular.module("myApp", ["ngTable"]);

electricityApp.controller(
'electricityCtrl',
function($scope, $http,NgTableParams) {
$scope.dmydata=[
{ name: "Sam", age: "35"},
{ name: "Ravi", age: "40"},
{ name: "Kumara", age: "50"},
{ name: "James", age: "60"},
{ name: "Sam2", age: "70"},
{ name: "Ravi2", age: "75"},
{ name: "Kumara2", age: "80"},
{ name: "James2", age: "85"},
{ name: "kamal", age: "90"},
{ name: "Sunil", age: "95"},
];


$scope.getDatas = function(){
$scope.tableParams = new NgTableParams({count:5}, { dataset:$scope.dmydata});
}
});
 <!DOCTYPE html>
<html lang="en-US">


<link data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d1b3bebea5a2a5a3b0a191e2ffe2ffe6">[email protected]</a>" data-semver="3.3.7" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />


<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ng-table/1.0.0/ng-table.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ng-table/1.0.0/ng-table.min.css">
<script>

</script>
<body>
<div ng-app="myApp"> 
<div ng-controller="electricityCtrl">
<table ng-table="tableParams" ng-init="getDatas()" class="table table-condensed table-bordered table-striped" show-filter="true">
    <tr ng-repeat="user in $data">
        <td title="'Name'" sortable="'name'">{{user.name}} <input type="text" ng-model="user.name"/> </td>
        <td title="'Age'" sortable="'age'">{{user.age}} <select ng-model="user.age"> <option> 1</option> <option> 2</option> <option> 3</option></select></td>
    </tr>
</table>
</div>
</div>
</body>
</html> 

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

Issue encountered when utilizing ngResource in factory: unable to choose JSON index

ngResource in a factory is functioning properly, but unfortunately, it is only able to select the index of the JSON. However, it is also possible to bind the same variable $scope.resultItems. The console log appears as follows ...

Can PHP encode the "undefined" value using json_encode?

How can I encode a variable to have the value of undefined, like in the JavaScript keyword undefined? When I searched online, all I found were results about errors in PHP scripts due to the function json_encode being undefined. Is there a way to represent ...

Incorporate an HTML code string into an Angular 2 template

I am working with HTML code stored in a Component variable, shown below: import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: `Hi <div [innerHTML]="name"></div>`, styleUrls: [' ...

AngularJS: Unable to preserve the data

I'm currently working on an issue with saving updated functions using angularJS. I've managed to edit the data and update it on the database side, but the changes aren't reflecting on the frontend side unless I logout and login again. I need ...

"Watching is not good, so what can we do to improve this situation

In my Angular project, I have been utilizing the $watch function even though it's recommended to avoid doing so. Below is an example of how I've implemented it: $scope.$watch('mysevice.function()', function(tilda) { $scope.valu ...

Having trouble with the error "Cannot GET /" in your Angular2 and Express

I've been working on customizing this GitHub example application to utilize Express instead of KOA. When I enter gulp serve in the CentOS 7 terminal, the app launches successfully. However, upon typing http : // localhost : 8080 in the browser, a 404 ...

ajaxStart event does not trigger when making an Ajax POST request

My ajaxStart event functions properly for ajax loads, but it does not work at all when I do a POST request. The following code is consistently rendered on all pages: $(document).ajaxComplete(function () { hideIcon(); if (stepState !== &a ...

Invert the order of the array in the JavaScript variable containing the JSON data

I have a JSON array stored in a variable in the following format: {"info": [ {"typeid": "877", "recid": "10", "repeaterid": "0", "pageid": "26966", "maxrecords": "1"}, {"typeid": "877", "recid": "11", "repeaterid": "0", "pageid": "26966", "maxrecords ...

Transforming a JavaScript chained setter into TypeScript

I have been utilizing this idiom in JavaScript to facilitate the creation of chained setters. function bar() { let p = 0; function f() { } f.prop = function(d) { return !arguments.length ? p : (p = d, f); } return f; } ...

Having difficulty with building a basic module in Node JS, it's just not cooperating

As a newcomer to Node JS, this platform, and the English language, I apologize in advance for any linguistic errors. I seem to be encountering a "return" error within my code. Specifically, when I include the hi.myFunc(); function, I receive the ...

Finding items in the database using their identification numbers

I have a scenario where I am accepting a list of IDs in my request, for example [1,2,3]. How can I use typeorm and querybuilder to retrieve only objects with these IDs from my database? I attempted the following: if(dto.customersIds){ Array.prototype. ...

JavaScript for Acrobat

I am currently creating a form in Adobe Acrobat and incorporating additional functionality using JavaScript. I have been searching for information on the control classes for the form, such as the member variables of a CheckBox, but haven't found any c ...

What could be causing the misalignment of the Datepicker calendar in Material UI?

I have integrated a datepicker using the library "@mui/x-date-pickers/DatePicker". import { DatePicker } from "@mui/x-date-pickers/DatePicker"; import { AdapterMoment } from "@mui/x-date-pickers/AdapterMoment"; import { Locali ...

Reorganizing an array using a custom prioritized list

Is it possible to sort an array but override precedence for certain words by placing them at the end using a place_last_lookup array? input_place_last_lookup = ["not","in"]; input_array = [ "good", "in", "all&qu ...

The functions firebase.auth().currentUser and onAuthStateChanged consistently return null even when the user is logged in

Despite the fact that my front end Swift application indicates that the user is signed in, I am encountering an issue where firebase.auth().currentUser and the onAuthStateChanged listener in my node.js code return null. The backend service is called from t ...

Prevent users from clicking by using a CSS class in HTML and JavaScript

,hey there buddy 1° Can you help me figure out how to prevent click behavior using the CSS class? 2° I'm unable to add an ID to the HTML element, so I need to use the Class to achieve this. 3° None of my attempts have been successful so far. El ...

Combine the remaining bars by stacking the highest one on top in Highchart

Making use of stacking to display the highest value as the longest column/bar, with smaller values being merged within the highest one, can create a more visually appealing stack chart. For example, when looking at Arsenal with values of 14 and 3, ideally ...

An alternative to PHP's exec function in Node.js

I am interested in developing a piece of software using C (such as prime number factorization) and hosting it on my web server with Node.js. Following that, I would like to create an HTML document containing a form and a button. Upon clicking the button, ...

Ensure that data is accurately confirmed using an alternative mode within react-hook-form

Currently utilizing react-hook-form with the primary validation mode set to "onChange": const formMethods = useForm({ mode: 'onChange' }) I am looking to modify the mode property for a specific input nested within a Controller, but I am unsure ...

Unraveling Complex JSON Structures in React

Having trouble reading nested JSON data from a places API URL call? Look no further! I've encountered issues trying to access all the information and nothing seems to be coming through in the console. While unnested JSON is not an issue, nested JSON p ...