Laravel Combine results in one table - laravel-9

I managed to get the results with this code in the controller and blade file i wrote that to see the results, but i can't merge the two tables into one single table, i don't understand what i need to do to see everything in one single table, i tried different ways but i failed in all, only seperate currently i can see them but what i need is to see them all in the same table, can anyone help me to get the final result with only one table?
$startDate = Carbon::create(2023, 1)->startOfMonth();
$endDate = Carbon::create(2023, 12)->endOfMonth();
$uptimeData = Ticket::query()
->select(['ticket_Id','created_at'])
->orderBy('created_at', 'asc')
->whereBetween('created_at', [$startDate, $endDate])
->whereRaw("cast(date_format(created_at, '%H') as SIGNED) between 7 and 15")
->get();
$intervals = \Carbon\CarbonInterval::hours(1)->toPeriod($startDate, $endDate);
$uptimeDataTimeline = $uptimeData->groupBy(function ($item, $key) use ($intervals) {
$date = Carbon::parse($item->created_at);
foreach ($intervals as $key => $interval) {
if ($date->hour == Carbon::parse($interval)->addHours(1)->hour) {
$actualHour1 = Carbon::parse($interval)->hour;
if (strlen($actualHour1) == 1) $actualHour1 = "0$actualHour1";
return $date->format("Y-m-d $actualHour1:00:00");
}
else if ($date->hour == Carbon::parse($interval)->addHours(1)->hour) {
$actualHour2 = Carbon::parse($interval)->subHours(1)->hour;
if (strlen($actualHour2) == 1) $actualHour2 = "0$actualHour2";
return $date->format("Y-m-d $actualHour2:00:00");
}
}
return $date->format('Y-m-d H:00:00');
});
$uptimeDataTimeline = $uptimeDataTimeline->map(function($checksInPeriod, $key) {
$down = 0;
$up = 0;
$total = 0;
$uptime = 0;
$fill = '#1fc777';
foreach($checksInPeriod as $key => $value){
$total++;
if ($total <= 4) {
$up++;
} else {
$down++;
}
}
$uptime = floatval(number_format(round($up / $total, 5) * 100, 2, '.',','));
if ($uptime < 100) $fill = '#9deab8';
if ($uptime < 99) $fill = '#fbaa49';
if ($uptime < 98) $fill = '#e0465e';
return [
'total_ticket_Id' => $total,
'down_ticket_Id' => $down,
'up_ticket_Id' => $up,
'uptime' => $uptime,
'fill' => $fill,
];
});
$dataset = [
'uptimeData' => $uptimeData,
'uptimeDataTimeline' => $uptimeDataTimeline,
];
<thead>
<tr>
<th>
Total
</th>
<th>
Up
</th>
<th>
Down
</th>
<th>
Uptime
</th>
</tr>
</thead>
<tbody>
#foreach ($dataset['uptimeDataTimeline'] as $data)
<tr>
<td>
{{ $data['total_ticket_Id'] }}
</td>
<td>
{{ $data['up_ticket_Id'] }}
</td>
<td>
{{ $data['down_ticket_I'] }}
</td>
<td>
<div class="progress-bar bg-success" style="width: {{ $data['uptime'] }}%">{{ $data['uptime'] }} %</div>
</td>
</tr>
#endforeach
</tbody>
</table>
<thead>
<tr>
<th>
Ticket
</th>
<th>
Data
</th>
</tr>
</thead>
<tbody>
#foreach ($dataset['uptimeData'] as $data)
<tr>
<td>
{{ $data['ticket_Id'] }}
</td>
<td>
{{ $data['created_at'] }}
</td>
</tr>
#endforeach
</tbody>
</table>

Related

ASP.NET CORE LINQ GROUP BY

I have little problem with group by, i want achieve this
Nobukti : 001/des/2019
1000
2000
3000
4000
in My controller
var transaction = await (from a in _context.Transaction group a by a.Nobukti into pg
join b in _context.ChartAccount on pg.FirstOrDefault().Kodeakun
equals b.Kodeakun
select new Transaksi
{
Nobukti = pg.First().Nobukti,
Kodeakun = pg.FirstOrDefault().Kodeakun + b.Namaakun
}).ToListAsync();
In my View
<table class="table">
#foreach (var item in Model)
{
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.Nobukti)
</th>
<td>
#Html.DisplayFor(modelItem => item.Nobukti)
</tr>
</thead>
<tbody>
<tr>
#foreach (var book in item.Nobukti)
{
<td>
#Html.DisplayFor(modelItem => item.Kodeakun)
</td>
}
</tr>
</tbody>
}
</table>
Can anyone help me? i dont know which wrong, my controller or my view,
sorry for english

Update Partial view MVC4

I have this controller:
public ActionResult PopulateTreeViewModel()
{
MainModelPopulate mainModelPopulate = new MainModelPopulate();
// populate model
return View(mainModelPopulate);
}
That has a view like this:
#model xxx.xxx.MainModelPopulate
<table>
#foreach (var item2 in Model.CountryList)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item2.CountryName);
</td>
</tr>
foreach (var item3 in item2.BrandList)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item3.BrandName);
</td>
</tr>
foreach (var item4 in item3.ProductList)
{
<tr>
<td>
#Html.ActionLink(item4.ProductName, "FunctionX", new { idLab = item3.BrandID, idDep = item4.ProductID });
</td>
</tr>
}
}
}
</table>
The FunctionX controller is like this :
public ActionResult FunctionX(int idBrand=1 , int idProd=1)
{
List<ListTypeModel> typeModelList = new List<ListTypeModel>();
// populate typeModelList
return PartialView(typeModelList);
}
}
with this partial view:
#model IEnumerable<TControl.Models.ListTypeModel>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
</tr>
}
</table>
I want to add this partial view in my main view (PopulateTreeViewModel) and update the table with the relative type of product contained in Function X.
I tried also to substitute #Html.ActionLink with #Ajax.ActionLink and it performs the same way.
Have you tried #Html.RenderAction(item4.ProductName, "FunctionX", new { idLab = item3.BrandID, idDep = item4.ProductID });
There are other options too..! Pls refer http://www.dotnet-tricks.com/Tutorial/mvc/Q8V2130113-RenderPartial-vs-RenderAction-vs-Partial-vs-Action-in-MVC-Razor.html

Jquery DataTable date filter not working in client side

I am using Jquery DataTable in my MVC Application. I want to filter data in DataTable using Date-Range. when the page loads i am getting all the data from DB. so i need to implement this date range filter in one column of DataTable in client side. I have tried below coding but the date is not getting filtered.
HTML Table:
<table border="0" cellspacing="5" cellpadding="5">
<tbody>
<tr>
<td>From Date:</td>
<td><input type="text" id="min" name="min"></td>
</tr>
<tr>
<td>To Date:</td>
<td><input type="text" id="max" name="max"></td>
</tr>
</tbody>
</table>
<table id="Billing">
<thead>
<tr>
<th>Select</th>
<th>Company Name</th>
<th>Amount</th>
<th>Invoice Date</th>
<th>Status</th>
<th>Comments</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td><input type="checkbox" value=#item.companyId /></td>
<td> #item.CompanyName </td>
<td> #item.TotalAmount </td>
<td> #item.InvoiceDate </td>
<td> #item.Status </td>
<td> #item.Notes </td>
</tr>
}
</tbody>
</table>
jquery DataTable Initialize:
<script>
$(document).ready(function () {
$("#min").datepicker();
$("#max").datepicker();
var table = $('#Billing').dataTable(
{
"aLengthMenu": [[5, 10, 20, -1], [5, 10, 20, "All"]],
"iDisplayLength": 10
}).columnFilter({
sPlaceHolder: "head",
aoColumns: [null, null, null, { "type": "date-range" }, null, null]
});
// Event listener to the two range filtering inputs to redraw on input
$('#min, #max').keyup(function () {
table.draw();
});
});
$.fn.dataTable.ext.search.push(
function (settings, data, dataIndex) {
var dateMin = $('#min').attr("value");
var dateMax = $('#max').attr("value");
dateMin = dateMin.substring(0, 4) + dateRange.substring(5, 7) + dateRange.substring(8, 10);
dateMax = dateMax.substring(13, 17) + dateRange.substring(18, 20) + dateRange.substring(21, 23);
var date = aData[3];
// remove the time stamp out of my date
// 2010-04-11 20:48:22 -> 2010-04-11
date = date.substring(0, 10);
// remove the "-" characters
// 2010-04-11 -> 20100411
date = date.substring(0, 4) + date.substring(5, 7) + date.substring(8, 10)
// run through cases
if (dateMin == "" && date <= dateMax) {
return true;
}
else if (dateMin == "" && date <= dateMax) {
return true;
}
else if (dateMin <= date && "" == dateMax) {
return true;
}
else if (dateMin <= date && date <= dateMax) {
return true;
}
// all failed
return false;
}
);
</script>

How to get around when an image doesn't exist for a model?

I have a problem to display the ImageNotFound when my database doesn't have an image set for an article in my webstore. I got the error Value cannot be null or empty.
The imageArticle is null when I debug it. I don't know but is seems the ImageArticle from the DB is causing this error. Any way I can't bypass it ?
I added an #if condition to check the null but it doesn't see to like it.
The database is containing a description, price and quantity but the image path has not been store. It is set to null. (this is because I want to allow the user to create new article in the webstore but maybe he doesn't have any picture yet for this new added item.
Here is the Browse.html
#model IEnumerable<MyWebStore.Models.Product>
#{
ViewBag.Title = "Browse";
}
<h2>Make your Selection</h2>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.ImageArticle)
</th>
<th>
#Html.DisplayNameFor(model => model.Description)
</th>
<th>
#Html.DisplayNameFor(model => model.Price)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#if (item.ImageArticle == null)
{
<img class="img_set_size" src="~/Images/ImageNotFound.jpg" />
} <==== the error occurs here
Else
{
<img class="img_set_size" alt="Image" src="#Url.Content(item.ImageArticle)"
/>
}
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#Html.DisplayFor(modelItem => item.Price)
</td>
<td>
#Html.ActionLink("Add to Cart", "AddToCart", "Cart", new { id =
item.ProductId }, null)
</td>
</tr>
}
</table>
try like this:
#if (item.ImageArticle == null)
{
<img class="img_set_size" src="#Url.Content("~/Images/ImageNotFound.jpg")" />
}
else
{
<img class="img_set_size" alt="Image" src="#Url.Content(item.ImageArticle)"/>
}

how to retrieve data from dynamically created table in asp.net MVC 4

I am new to MVC 4 and I am stuck in a Problem, I have bound a table dynamically with some data from database as following on cshtml page
<table id="tblFeature">
<tr>
<th>
Include
</th>
<th>
Facilities
</th>
<th>
Description
</th>
</tr>
#foreach (var feature in Model)
{
<tr>
<td>#Html.EditorFor(item => feature.Checked)
</td>
<td>#Html.DisplayFor(item => feature.Name)
</td>
<td>#Html.TextAreaFor(item => feature.Description, 2, 70, null)
</td>
</tr>
}
</table>
Now I want to update the values like ,check or uncheck the checkbox,update the description in text area etc., in order to achieve that i need to add the updated values in the list in controller class and then I will update those values in database. but i don't know how to achieve that, it is something like following in plain english
foreach(feature in tblFeature)
{
if(feature is checked)
{
featureList.add(feature)
}
}
any help will be appreciated. thanks.
Change View for correct binding:
#using (Html.BeginForm("Save", "Controller"))
{
<table id="tblFeature">
<tr>
<th>
Include
</th>
<th>
Facilities
</th>
<th>
Description
</th>
</tr>
#for (int i = 0; i < Model.Count(); i++)
{
<tr>
#Html.HiddenFor(m => m[i].ProductSizeID)
<td>#Html.EditorFor(m => m[i].Checked)
</td>
...
</td>
</tr>
<input type="submit" value="save"/>
}
Post in Controller and save change
[HttpPost]
public ActionResult Save(IEnumerable<feature> ViewModels)
{
foreach (var feature in ViewModels)
{
if(feature.Checked)
{
featureList.add(feature)
}
}
}