0

Hopefully someone can help.

I am developing an application using HTML AngularJs which uses ng-repeat,ng-options and ng-model and populates multiple rows based on the data in the database for a user. Each row has static data coming from DB (returned as object via restAPI) and dynamic select option for user selection. Select option is hardcoded in app.js and linked to model on HTML for DB update upon selection using update button. Each row has its own button and i can see update function is working at row level.

I want to set the default value of the drop down list dynamically as value of an element coming from database. Object is same as one being used to populate rows with static data .

Code is in the fiddle at https://jsfiddle.net/6j88L61y/4/

HTML below

<body>
<h1 align="center">User Tracker</h1>
<div ng-controller="MainController as main">
<div>
<p>Please Enter ID </p>
<p>
<input type="text" ng-model="main.model.userName"></input>
<button ng-click="main.model.getOrgList()">List State List</button>
</p>
</div>
<hr ng-show="main.model.formSubmitted"></hr>
<div>
<table class="table table-bordered" border="1">
<thead>
<tr>
<th>ID</th>
<th>User Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="org in main.model.orgList" id="{{org.id}}">
<td>{{org.id}}</td>
<td align="center">{{org.user}}</td>
<td align="center">
<select ng-model="main.model.selectedRecord.appCurrentStateSelected[$index]" ng-options="option.value  as option.value for option in main.model.appCurrentStateList" ></select>
</td>
<td>
<button ng-click="main.model.updateAppDetailsList({id:org.id,userName:org.name,appCrntState:main.model.selectedRecord.appCurrentStateSelected})">Update</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>

JS

"use strict";

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

angular.module('myApp').service('AppModel', function( $http) {
this.userId='';
this.userName ="";
this.formSubmitted="";
this. selectedRecord ={appCurrentStateSelected:''};
this.appCurrentStateList =[{name: 'New',value:'New',id:1}, {name: 'InUse',value:'InUse',id:2},{name: 'Inactive',value:'Inactive',id:3},{name: 'Deleted',value:'Deleted',id:4}];     
this.submittedAppDetailsList=[];
console.log(' json sample:'+this.submittedAppDetailsList);  
var path = 'home';
var currentProtocol = location.protocol;
var host =location.host;
var apiHost = currentProtocol+'//'+host+'/api/';
console.log('API url is : ' +apiHost);

// Get method

this.getOrgList = function() {

var path = 'home/userid';
console.log(this.userName);
console.log(this.selectedRecord);   
$http.get(apiHost+path+'/'+this.userName+'/')
.then(function(response) {
this.orgList =response.data;
this.formSubmitted = true;      
console.log(response.data);
}.bind(this),
function(response) {
console.log(response.data);
});
}

// Post method

this.updateAppDetailsList = function(appdetailsList) {

var path = 'home/update';

console.log(this.selectedRecord);   
$http.post(apiHost+'home/update/',appdetailsList)
.then(function(response) {
this.submittedAppDetailsList.push(response.data);
this.formSubmitted = false;     
console.log(response.data);
}.bind(this),
function(response) {
console.log('Error : '+response.data);
});
}   

});


angular.module('myApp').controller('MainController', ['AppModel', function (AppModel) {

this.model = AppModel;


}]);
vison
  • 59
  • 1
  • 9
  • Experts, Please help, thanks in advance – vison Nov 02 '17 at 19:00
  • Check this answer it may be use full to you to set default value to your drop down list. https://stackoverflow.com/questions/41376553/view-pending-status-data-in-table-by-default/41377985#41377985 – Divya Jain Dec 14 '17 at 11:48

0 Answers0