Here I'm getting data from the database and I also trace that data from Fiddler but when I'm trying to bind data in an angular table it's not binding
Linq Query
public JsonResult GetSalesData()
{
using (Ctxdb dc = new Ctxdb())
{
var v = (from a in dc.SalesRecords
group a by a.SalesDate.Year into g
select new
{
Year = g.Key,
Electronics = g.Sum(a => a.Electronics),
BookAndMedia = g.Sum(a => a.BookAndMedia),
HomeAndKitchen = g.Sum(a => a.HomeAndKitchen)
});
if (v != null)
{
var chartData = new object[v.Count() + 1];
chartData[0] = new object[]{
"Year",
"Electronics",
"Book and Media",
"Home and KitchenKFC"
};
int j = 0;
foreach (var i in v)
{
j++;
chartData[j] = new object[] { i.Year.ToString(), i.Electronics, i.BookAndMedia, i.HomeAndKitchen };}
return new JsonResult { Data = chartData, JsonRequestBehavior = JsonRequestBehavior.AllowGet };}}
return new JsonResult { Data = null, JsonRequestBehavior = JsonRequestBehavior.AllowGet };}
Fidller Data
[["Year","Electronics","Book and Media","Home and KitchenKFC"],["1947",99489,46000,26833],["1995",25555,45000,99000],["2011",1584,5685,5566],["2012",12121,1500,1200],["2014",600,5132,2730],["2015",1789,1255,1258]]
Angular Service
app.service('MyGraphService', function ($http) {
this.GetJanu = function () {
alert('in Service')
var Sert = $http({
url: '/Department/GetSalesData',
method: 'GET',
data: JSON.stringify(),
content:{'content-type' :'application/Json'}
})
return Sert;
}
})
Angular Controller
app.controller('GraphCntrls', function ($scope, MyGraphService) {
$scope.Btnclick = function () {
alert('in cntr')
var tt = MyGraphService.GetJanu();
tt.then(function (d) {
$scope.DataGraphf = d.data;
})
}
})
Cshtml Code
<div>
<div ng-controller="GraphCntrls">
<input type="button" ng-click="Btnclick()" value="Click" class="btn btn-success btn-sm"/>
<table class="table table-striped table-bordered">
<tr>
<th><b>Year</b></th>
<th><b>Electronics</b></th>
<th><b>Book and Media</b></th>
<th><b>Home and KitchenKFC</b></th>
</tr>
@*<tr ng-repeat="datf in DataGraphf">*@
<tr>
<td>{{Year}}</td>
<td>{{datf.Electronics}}</td>
<td>{{datf.BookAndMedia}}</td>
<td>{{datf.HomeAndKitchen}}</td>
</tr>
</table>
</div>
</div>
Change the table row to this:
<tr ng-repeat="datf in DataGraphf" ng-if="!$first">
<td>{{datf[0]}}</td>
<td>{{datf[1]}}</td>
<td>{{datf[2]}}</td>
<td>{{datf[3]}}</td>
</tr>
since you are returning an array of arrays, and the first index being table headers. you want to hide the first results element with ng-if="!$first"
. Then your subsequent elements are arrays with 4 values, so you have to use array index to access the properties vs obj.Name.
To use obj.Name, you have to return an object instead of an array, eg
{
"Year": 1230,
"Electronics": 123,
"BookAndMedia" : 1234,
"HomeAndKitche" : 515
}
the response json would be:
[{...},{...},{...}]
update: had extra tr tag in original response, shifted ng-if to tr instead
If that's the actual code you're running, it's not going to render the line with your ng-repeat in it because you have it commented out server-side.
Edit:
Change it to this:
public JsonResult GetSalesData()
{
using (Ctxdb dc = new Ctxdb())
{
var v = (from a in dc.SalesRecords
group a by a.SalesDate.Year into g
select new
{
Year = g.Key,
Electronics = g.Sum(a => a.Electronics),
BookAndMedia = g.Sum(a => a.BookAndMedia),
HomeAndKitchen = g.Sum(a => a.HomeAndKitchen)
});
if (v != null)
{
var chartData = new object[v.Count()];
int j = 0;
foreach (var i in v)
{
j++;
chartData[j] = new object { Year = i.Year, Electronics = i.Electronics, BookAndMedia = i.BookAndMedia, HomeAndKitchen = i.HomeAndKitchen };
}
return new JsonResult { Data = new {
Fields = new string[] {"Year", "Electronics", "Books and Media", "Home and KitchenKFC"},
Data = chartData
} , JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
}
return new JsonResult { Data = null, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
and your Angular code to this:
<tr ng-repeat="datf in DataGraphf.Fields">
<th>{{datf}}</th>
</tr>
<tr ng-repeat="datf in DataGraphf.Data">
<td>{{datf.Year}}</td>
<td>{{datf.Electronics}}</td>
<td>{{datf.BookAndMedia}}</td>
<td>{{datf.HomeAndKitchen}}</td>
</tr>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.