Hi,
Since in real time already i have some existing price values for some dates
By default based upon price value need to display total at particular date
Can you please help me in below url code
so could you please help in below shared code
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', []);
app.controller('MyController', function ($scope) {
$.post("/Home/GetData/", function (r) {
$scope.items = r;
$scope.columns = r[0].Dates.length;
$scope.$apply();
});
});
</script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
<div class="container" id="printarea">
<table class="table table-bordered">
<tr class="success">
<th>Id</th>
<th>Item</th>
<th colspan="{{columns}}" class="text-center">Date</th>
</tr>
<tr>
<td></td>
<td></td>
<td ng-repeat="i in items[0].Dates">{{i.Date}}</td>
</tr>
<tr ng-repeat="item in items">
<td>{{item.Id}}</td>
<td>{{item.Item}}</td>
<td ng-repeat="i in item.Dates">
<input type="text" ng-model="i.Price" style="width: 100px" />
</td>
</tr>
</table>
</div>
</body>
</html>
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public JsonResult GetData()
{
List<Item> itemLists = new List<Item>();
itemLists.Add(new Item { ItemId = 1, ItemName = "a" });
itemLists.Add(new Item { ItemId = 2, ItemName = "b" });
List<ItemDate> itemDates = new List<ItemDate>();
itemDates.Add(new ItemDate { Id = 1, ItemId = 1, Date = Convert.ToDateTime("02/11/2020"), Price = 10 });
itemDates.Add(new ItemDate { Id = 1, ItemId = 1, Date = Convert.ToDateTime("06/11/2020"), Price = 8 });
itemDates.Add(new ItemDate { Id = 1, ItemId = 2, Date = Convert.ToDateTime("04/11/2020"), Price = 1 });
itemDates.Add(new ItemDate { Id = 1, ItemId = 1, Date = Convert.ToDateTime("08/11/2020"), Price = 5 });
DateTime? Fdate = itemDates.Min(x => x.Date);
DateTime? tdate = itemDates.Max(x => x.Date);
List<ItemEntryDetail> itemEntryDetails = new List<ItemEntryDetail>();
for (int row = 0; row < itemLists.Count; row++)
{
List<DatePrice> dt = new List<DatePrice>();
for (DateTime? i = Fdate; i <= tdate; i = i.Value.AddDays(1))
{
dt.Add(new DatePrice { Date = i.Value.ToString("dd-MM-yyyy") });
}
ItemEntryDetail itemEntryDetail = new ItemEntryDetail();
itemEntryDetail.Id = itemLists[row].ItemId;
itemEntryDetail.Item = itemLists[row].ItemName;
for (int column = 0; column < dt.Count; column++)
{
ItemDate itemDate = itemDates.Where(x => x.ItemId == itemLists[row].ItemId && x.Date == Convert.ToDateTime(dt[column].Date)).FirstOrDefault();
dt[column].Price = itemDate != null ? itemDate.Price.ToString() : "";
itemEntryDetail.Dates = dt;
}
itemEntryDetails.Add(itemEntryDetail);
}
return Json(itemEntryDetails, JsonRequestBehavior.AllowGet);
}
public class Item
{
public int ItemId { get; set; }
public string ItemName { get; set; }
}
public class ItemDate
{
public int Id { get; set; }
public int ItemId { get; set; }
public DateTime Date { get; set; }
public int Price { get; set; }
}
public class ItemEntryDetail
{
public int Id { get; set; }
public string Item { get; set; }
public List<DatePrice> Dates { get; set; }
}
public class DatePrice
{
public string Date { get; set; }
public string Price { get; set; }
}
}