I have the following code that makes a PUT request and displays the
modified data returned from the server in a table. Right now, when
the page is loaded, an empty table is displayed until the button is clicked.
What I require is that the table be hidden until the button is clicked. How
do I accomplish this?
<div ng-app="myApp" ng-controller="formController">
<script type="text/ng-template" id="tableTemplate.html">
<table>
<thead>
<tr>
<th>id</th>
<th>email</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in names">
<td>{{ x.id }}</td>
<td>{{ x.email }}</td>
<tr>
</tbody>
</table>
</script>
<form ng-submit="processForm()">id :
<input type="text" name="id" ng-model="formData.id">email :
<input type="email" name="email" ng-model="formData.email">
<input type="submit" value="go">
</form>
<div my-directive></div>
</div>
<script>
var app = angular.module('myApp',[]);
app.directive('myDirective', function() {
return {
replace: true,
restrict: 'EA',
templateUrl: 'tableTemplate.html'
}
});
app.controller("formController", function($scope,$http) {
$scope.formData = {};
$scope.processForm = function(){
// PUT /users/id
var url = 'http://www.example.com/users/' + $scope.formData.id;
var data = '{"email":"' + $scope.formData.email}';
$http({method: 'PUT', url: url, data: data}).success(function(response){
$scope.names = response;
});
}
});
</script>
In the HTML:
<table ng-show="formSubmitted">
In the controller $http success() function:
$scope.formSubmitted = true;
Related
I am new to angularjs and unable to figure out why ng-click on a button is not working. I am pasting code below. I am using a django server to render the page.
HTML CODE:
<body ng-controller="myController">
<div class="container-fluid" >
<table class="col-xs-12-ls-12 mfPortfolioTable" width="100%">
<tbody>
<tr ng-repeat="x in records">
<td class="search_table_cell col-xs-1-ls-1">{% verbatim %}{{ x.units | number:3 }}{% endverbatim %}</td>
</tr>
<tr>
<td><button class="btn btn-primary" ng-click="updateMfPortfolio()" id="updateFolio">Update</button></td>
</tr>
</tbody>
</table>
</div>
</body>
CONTROLLER CODE:
var app = angular.module('searchApp', []);
app.controller('myController', function($scope, $http) {
$http.get('/somelink/').then(function(response){
$scope.records = response.data.data;
});
//This is the function which I expect to get triggered by ng-click
$scope.updateMfPortfolio = function(){
console.log('Updating MF Portfolio...');
$http.get('/somelink/').then(function(response){
console.log('Code reached here...');
$scope.records = response.data.data;
});
};
I want simply fetch the list of items using angular js .I am using angular code and using $http. but its not working. I hit on action method and try to return json. but when i am trying it. it return full json on view.
#section scripts{
<script src="~/Scripts/ang.js"></script>
}
<div ng-app="StudentApp" class="container">
<br/>
<br/>
<input type="text" placeholder="Search Student" ng-model="searchStudent" />
<br />
<div ng-controller="StudentController">
<table class="table" ng-init="initData()">
<tr ng-repeat="r in data | filter : searchStudent" >
<td>{{ r.Col_Title }}</td>
<td>{{ r.Col_Descrp }}</td>
</tr>
<table>
</div>
</div>
public ActionResult GetCollections()
{
var lst = eShoppingEntitiesContetxt.Collections.Where(s => s.IsActive == true).ToList();
var result = JsonConvert.SerializeObject(lst, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
return Json(result, JsonRequestBehavior.AllowGet);
// return View();
}
var myApp = angular.module('StudentApp', []);
myApp.controller('StudentController', function ($scope, $http) {
$http.get('/User/GetCollections') // added an '/' along with deleting Controller portion
.then(function (response) {
$scope.data = response.data
})})
<div ng-app="StudentApp" class="container">
<br/>
<br/>
<input type="text" placeholder="Search Student" ng-model="searchStudent" />
<br />
<div ng-controller="StudentController">
<table class="table">
<tr ng-repeat="r in data | filter : searchStudent" >
<td>{{ r.name }}</td>
<td>{{ r.country }}</td>
</tr>
<table>
</div>
</div>
Try this:
var myApp = angular.module('StudentApp', []);
myApp.controller('StudentController', function($scope, $http) {
$scope.initData = function() {
$http.get('/User/GetCollections') // added an '/' along with deleting Controller portion
.then(function(response) {
$scope.data = response.data;
});
}
});
In your view (notice the ng-init="initData()" in the table)
<div ng-app="StudentApp" class="container">
<br/>
<br/>
<input type="text" placeholder="Search Student" ng-model="searchStudent" />
<br />
<div ng-controller="StudentController">
<table class="table" ng-init="initData()">
<tr ng-repeat="r in data | filter : searchStudent" >
<td>{{ r.name }}</td>
<td>{{ r.country }}</td>
</tr>
<table>
</div>
As your question says : it return full json on view....
So I hope you forgot to convert your stringfy json to object. Like :
$scope.data = angular.fromJson(response.data);
Update
Your not calling REST api method, rather MVC controller, which will ofcourse render view with json.
you need to create a REST API method. like :
[Route("User/GetCollections")]
public JsonResult GetCollections()
{
// compute your data.
return new Json(data, JsonRequestBehavior.AllowGet);
}
and remove ng-init="initData()" from html. Since your controller will bind $scope.data.
I am a beginner in anular.js. I'm trying to write a code which add and removes rows in a table on button click and once done ad user clicks on createdelivery button I want to send the data as a JSON object to server.
But when am injecting $http as follows,
var app = angular.module("createDelivery", []);
app.controller("MyController", ['$scope', 'http', function($scope, $http)
it doesn't create the table also.
Please help me to solve the issue.
<html>
<head>
<title>Add Rows</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<script>
var app = angular.module("createDelivery", []);
app.controller("MyController", ['$scope', 'http', function($scope, $http) {
$scope.deliveryDetails = [
{
'fname':'Muhammed',
'lname':'Shanid',
'email':'shanid#shanid.com'
},
{
'fname':'Roy',
'lname':'Mathew',
'email':'roy#roy.com'
}];
$scope.addNew = function(cdDetails){
$scope.deliveryDetails.push({
'fname': "",
'lname': "",
'email': "",
});
};
$scope.remove = function(){
var newDataList=[];
$scope.selectedAll = false;
angular.forEach($scope.deliveryDetails, function(selected){
if(!selected.selected){
newDataList.push(selected);
}
});
$scope.deliveryDetails = newDataList;
};
$scope.checkAll = function () {
if (!$scope.selectedAll) {
$scope.selectedAll = true;
} else {
$scope.selectedAll = false;
}
angular.forEach($scope.deliveryDetails, function(cdDetails) {
cdDetails.selected = $scope.selectedAll;
});
};
$scope.getData = function(cdDetails)
{
alert("Check");
var jsonString;
jsonString = angular.toJson(cdDetails);
//JSON.stringify(jsonString);
$http
({
url: "/scheduler/createDelivery",
dataType: 'json',
method: 'POST',
data: jsonString
}).success(function(response) {
alert("success : : "+jsonString);
$scope.value = response;
}).error(function(error) {
alert("error::"+error);
});
//window.location.reload();
//document.write("\nString value "+jsonString);
};
}]);
</script>
</head>
<body ng-app="createDelivery" ng-controller="MyController">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-body">
<form ng-submit="addNew()">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th><input type="checkbox" ng-model="selectedAll" ng-click="checkAll()" /></th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="cdDetails in deliveryDetails">
<td>
<input type="checkbox" ng-model="cdDetails.selected"/></td>
<td>
<input type="text" class="form-control" ng-model="cdDetails.fname" required/></td>
<td>
<input type="text" class="form-control" ng-model="cdDetails.lname" required/></td>
<td>
<input type="email" class="form-control" ng-model="cdDetails.email" required/></td>
</tr>
</tbody>
</table>
<div class="form-group">
<input ng-hide="!deliveryDetails.length" type="button" class="btn btn-danger pull-right" ng-click="remove()" value="Remove">
<input type="submit" class="btn btn-primary addnew pull-right" value="Add New">
<button type="button" name = "createDelivery"class="col-sm-2 btn btn-primary" style="width:25%" ng-click="getData(cdDetails);">Create Delivery</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
app.controller("MyController", ['$scope', 'http'
One mistake in code is above. Change it and try. you must inject $http
I have a form as given below:
<form name="my-test-suite" ng-submit=submit() ng-controller="createTestSuite">
<div ng-controller="showTests">
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>Select Test</th>
<th>Test ID#</th>
<th>Description</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="test in testcases">
<td>
<input ng-model='ctrl.test.selected' type="checkbox" value="">
</td>
<td ng-model='test.id' >{{ test.id }}</td>
<td ng-model='test.resourceId'>{{ test.resourceId }}</td>
<td ng-model='test.allGraphs'>{{ test.allGraphs }}</td>
</tr>
</tbody>
</table>
</div>
<div align=center>
<button class="btn btn-default" type="submit">Submit</button>
</div>
</div>
</form>
and controllers as:
var app = angular.module('myApp', []);
app.controller('showTests', function($scope, $http) {
$http.get("http://localhost:4567/config-manager/tests").then(
function(response) {
$scope.testcases = response.data;
});
});
app.controller('createTestSuite', ['$scope', function($scope, $http) {
$scope.submit = function() {
console.log($scope.test);
};
}]);
It shows the grid properly with showTests but when I want to see the table data using createTestSuite Controller I cant see $scope at all. This is the problem. I want to read entire table data and use with post request. Please help.
One got below json for get request:
[ {
"id" : 55,
"allGraphs" : null,
"resourceId" : "126"
}, {
"id" : 56,
"allGraphs" : null,
"resourceId" : "125"
}, {
"id" : 58,
"allGraphs" : null,
"resourceId" : "140"
} ]
You're using ng-repeat="test in testcases" but you're never defining $scope.test so you're not able to access it.
A quick way to pass your table data from the showTests to the createTestSuite controller through ng-click="submit(testcases).
Following changes would be needed:
Update the view:
<form name="my-test-suite" ng-controller="createTestSuite">
// Other things stay the same
<button class="btn btn-default" ng-click="submit(testcases)" type="submit">Submit</button>
</form>
Update controller:
app.controller('createTestSuite', ['$scope', function($scope, $http) {
$scope.submit = function(data) {
console.log(data);
// Filter through the selected items if needed
};
}]);
var app = angular.module("myapp", ["ngSanitize"]);
app.controller("controller", ['$scope', function($scope) {
$scope.tasks = [{
item: "Shopping",
status: true
}, {
item: "Cleaning",
status: false
}];
}]);
and html
<div ng-app="myapp">
<div ng-controller='controller'>
<table border=1 ng-repeat='task in tasks'>
<tr>
<td>{{task.item}}</td>
<td>{{task.status}}</td>
</tr>
</table>
</div>
</div>
I tried as above but i could not able to attach header to the table.And at the place of Done I am tring to attach a checkbox...I mean if status is true the chexk box must be checked or else unchecked.
Output:
Item Done
Shopping checkbox with ticked
Cleaning checkbox without ticked
See documentation for checkbox input
Not exactly the answer, but why do you repeat table instead of rows?
<div ng-app="myapp">
<div ng-controller='controller'>
<table border=1 >
<tr>
<th>Name</th>
<th>Status</th>
</tr>
<tr ng-repeat='task in tasks'>
<td>{{task.item}}</td>
<td><input type="checkbox" ng-model="task.status"></td>
</tr>
</table>
</div>
</div>
But I don't see any sense in table in your case.
<span ng-repeat="task in tasks">
<input type="checkbox" ng-model="task.status"> {{task.item}}
</span>
That would be much cleaner.
Do changes as follows in the HTML.
<body ng-app="myapp" ng-controller="ListController">
<table border="1" ng-repeat='task in tasks'>
<tr>
<td>
{{task.item}}
</td>
<td>
<input type="checkbox" ng-model="task.status">
</td>
</tr>
</table>
</body>
Do changes in the Angular js as follows.
var app = angular.module("myapp", []);
app.controller("ListController", ['$scope', function($scope) {
$scope.tasks = [{
item: "Shopping",
status: true
}, {
item: "Cleaning",
status: false
}];
}]);