angularjs - Append Data to existing json array through Angular-Bootstrap dialog modal -
i facing issue while adding data through angular dialog modal
here plunker http://plnkr.co/edit/ejpkmxqnacun3gjilval?p=preview
<table class="table table-bordered"> <thead> <tr> <th> <input type="checkbox" ng-model="isall" ng-click="selectallrows()"/>all </th> <th> id </th> <th> name </th> </tr> </thead> <tbody> <tr ng-repeat="row in data" ng-class="{'success' : tableselection[$index]}"> <td> <input type="checkbox" ng-model="tableselection[$index]" /> </td> <td>{{row.id}}</td> <td>{{row.name}}</td> </tr> </tbody> </table> <script type="text/ng-template" id="mymodalcontent.html"> <div class="modal-header"> <h3 class="modal-title">im modal!</h3> </div> <form name = "addfriendform"> <input ng-model = "user.id"class="form-control" type = "text" placeholder="id" title=" id" /> <input ng-model = "user.name"class="form-control" type = "text" placeholder="name" title=" name" /> </form> <div class="modal-footer"> <button class="btn btn-primary" ng-click="ok()">ok</button> <button class="btn btn-warning" ng-click="cancel()">cancel</button> </div> </script>***strong text***
here script.js
while trying data dialog modal not coming can please 1 me out of problem
var app = angular.module('myapp', ['ui.bootstrap']); app.controller('mainctrl', function($scope,$modal,$log) { $scope.user = {id: "",name:""} $scope.data = [{ id: 1, name: 'name 8' }, { id: 2, name: 'name 7' }]; $scope.tableselection = {}; $scope.isall = false; $scope.selectallrows = function() { //check if selected or not if ($scope.isall === false) { //set row selected angular.foreach($scope.data, function(row, index) { $scope.tableselection[index] = true; }); $scope.isall = true; } else { //set row unselected angular.foreach($scope.data, function(row, index) { $scope.tableselection[index] = false; }); $scope.isall = false; } }; $scope.removeselectedrows = function() { //start last index because starting first index cause shifting //in array because of array.splice() (var = $scope.data.length - 1; >= 0; i--) { if ($scope.tableselection[i]) { //delete row data $scope.data.splice(i, 1); //delete rowselection property delete $scope.tableselection[i]; } } }; $scope.addnewrow = function() { //set row selected if checked $scope.tableselection[$scope.data.length] = $scope.isall; $scope.data.push({ id: $scope.data.length, name: 'name ' + $scope.data.length }); }; $scope.open = function () { var modalinstance = $modal.open({ templateurl: 'mymodalcontent.html', controller: 'modalinstancectrl', resolve: { data: function () { return $scope.data; } } }); modalinstance.result.then(function (data) { $scope.user = data; $scope.data.push($scope.user); // $scope.data.push({'id':$scope.data.id,'name':$scope.data.name}); }, function () { $log.info('modal dismissed at: ' + new date()); }); }; }); angular.module('myapp').controller('modalinstancectrl', function ($scope, $modalinstance, data) { $scope.ok = function () { $modalinstance.close($scope.user); }; $scope.cancel = function () { $modalinstance.dismiss('cancel'); }; });
you should create $scope.user
object in modalinstancectrl
, add $scope.user in $modalinstance.close
this:
angular.module('myapp').controller('modalinstancectrl', function ($scope,$modalinstance) { $scope.user = {}; $scope.ok = function () { $modalinstance.close($scope.user); }; $scope.cancel = function () { $modalinstance.dismiss('cancel'); }; });
i've checked in plunker, works
Comments
Post a Comment