Laravel Livewire checkbox checked on edit page - laravel

I'm facing an issue with checking the checkbox on the edit page using Livewire. I've tried many ways but still not able to check the old selected value
View Code:
<table id="branches" class="table table-bordered table-sm" width="100%" cellspacing="0">
<thead>
<tr>
<th width="10%">
</th>
<th>Branch ID</th>
</tr>
</thead>
<tbody>
#foreach ($propertiesOptions as $key => $property)
<tr>
<td>
<input class="branchCheckbox"
type="checkbox"
wire:model="propertyIds.{{$property->id}}"
value="{{$property->id}}" #if(in_array($property->id,$propertyIds)) checked #endif>
</td>
<td>{{$property->id}}</td>
</tr>
#endforeach
</tbody>
</table>
Backend Code:
public $propertyIds, $propertiesOptions;
public function mount($record)
{
$this->propertiesOptions = Properties::where('merchant_id',$record->id)->get()
$this->propertyIds = $record->properties->pluck('property_id')->toArray();
}```

updated
And can you check if this correct? wire:model="propertyIds.{{$property->id}}"? Try to delete the wire:model .
<input class="branchCheckbox" type="checkbox"
value="{{$property->id}}"
#if(in_array($property->id,$propertyIds)) checked #endif>

`<table id="branches" class="table table-bordered table-sm" width="100%" cellspacing="0">
<thead>
<tr>
<th width="10%">
</th>
<th>Branch ID</th>
</tr>
</thead>
<tbody>
#foreach ($propertiesOptions as $key => $property)
<tr>
<td>
<input class="branchCheckbox"
type="checkbox"
wire:model="propertyIds"
value="{{$property->id}}" >
</td>
<td>{{$property->id}}</td>
</tr>
#endforeach
</tbody>
</table>`
BACKEND
public $propertyIds, $propertiesOptions;
public function mount($record)
{
$this->propertiesOptions = Properties::where('merchant_id',$record->id)->get()
$this->propertyIds = $record->properties->pluck('property_id')->toArray();
}

Related

Laravel 5.7 simple pagination

In our application, we have modules called Reports where in this report will show you the summarize of the module.
Example
General Journal Report - in this report will show you the summarize of the general journal module.
Now in our reports need to implement the pagination, having a problem applying the pagination in query builder.
Controller
$general_journals = GeneralJournalReportModel::getGeneralJournals();
$data['defined_gj'] = GeneralJournalReportItemsController::getDefinedGJById($general_journals);
I fetch all the general journal records and stored it in the variable general_journals then I passed it in another controller to do something else(such as putting some error handling when the column is null).
Model
public static function getGeneralJournals()
{
return DB::table('general_journals as gj')
->select('gj.id as id',
'gj.number as number',
'gj.posting_date as posting_date',
'gj.remarks as remarks',
'gj.document_reference as document_reference',
'gji.debit_amount as debit_amount',
'gji.credit_amount as credit_amount')
->leftJoin('general_journal_items as gji', 'gj.id', '=', 'gji.general_journal_id')
->where('gj.company_id', Auth::user()->company_id)
->where('gj.approval_status', 3)
->orderBy('gj.id', 'desc')
->simplePaginate(5);
}
View
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12 col-lg-12">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover general-journal-report-table" id="gj-report">
<thead class="thead-global">
<tr>
<th id="pj_sequence">Sequence No.</th>
<th id="pj_date">Posting Date</th>
<th id="pj_op">Transaction No.</th>
<th id="4">Document Reference</th>
<th id="5">Remarks</th>
<th id="6">Amount Due</th>
</tr>
</thead>
<tbody class="general-journal-report-details">
#if($defined_gj)
<?php $counter = 0; ;?>
<?php $total_debit = 0; ?>
#foreach($defined_gj as $key => $value)
<?php $counter++;?>
<?php $total_debit += $value->debit ;?>
<tr>
<td class="pj_sequence">{{$counter}}</td>
<td class="pj_date">{{$value->posting_date}}</td>
<td class="pj_op">{!! $value->number !!}</td>
<td>{{$value->doc_ref}}</td>
<td>{{$value->remarks}}</td>
#if($value->debit == '0')
<td></td>
#else
<td align="right"> {{number_format($value->debit,2)}}</td>
#endif
</tr>
#endforeach
<tr>
<td><b>Total</b></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td align="right"> {{number_format($total_debit)}}</td>
</tr>
#endif
</tbody>
</table>
</div>
{{ $general_journals->links() }}
</div>
When I used the links() my button doesn't have any class.
Question: How do I put some class on the button of links?
NOTE: I tried this {{ $general_journals->links() }} but it only worked on the previous button(I want to also apply the class in the next button)
Try this
{{ $general_journals->links('pagination::bootstrap-4') }}
Replace with your laravel bootstrap version.
If you want to customize the pagination view, you can follow to this link https://laravel.com/docs/5.7/pagination#customizing-the-pagination-view

Why <td th:each="i : ${#numbers.sequence(0, table_width)}" th:text="${rows[${i}]}"></td> not working

I am trying to Iterate through a list which contains a list of objects, i.e. List I am wondered why this is not working, tried with simply "i", but no luck.
List<Object[]> lists; // logic
model.addObject("lists", lists);
model.addObject("table_width", lists.get(0).length);
Thymeleaf Code Snippet
<table class="table table-responsive table-stripped table-collapsed table-bordered">
<tr th:each="rows,rowStat : ${lists}">
<td th:text="${rowStat.count}"></td>
<td th:each="i : ${#numbers.sequence(0, table_width)}" th:text="${rows[${i}]}"></td>
</tr>
</table>
I have found a way,
<td th:each="i : ${#numbers.sequence(0, table_width-1)}" th:text="${rows[__${i}__]}"></td>
This does the tricks
You can simply iterator over both lists. No need to use the #numbers helper.
<table class="table table-responsive table-stripped table-collapsed table-bordered">
<tr th:each="rows, rowStat : ${lists}">
<td th:text="${rowStat.count}"></td>
<td th:each="value: ${rows}" th:text="${value}"></td>
</tr>
</table>
If you are iterating a collection(list) of objects, try below example:
HTML:
<div th:if="${not #lists.isEmpty(counts)}">
<h2>Counts List</h2>
<table class="table table-striped">
<tr>
<th>Id</th>
<th>Name</th>
</tr>
<tr th:each="count : ${counts}">
<td th:text="${count.id}"></td>
<td th:text="${count.name}"></td>
</tr>
</table>
</div>
Java:
public List<Count> listAll() {
List<Count> counts = new ArrayList<>();
countRepository.findAll().forEach(counts::add);
return counts;
}
Read more info in Thymeleaf Documentation - Iteration Basics section.

Dompdf LoadView variable passing

i can't load able to pass a parameter to the dompdf function LoadView.
this is my controller:
public function pst_affluiti(){
$data=DB::all('Anagrafica')->get();
$pdf = PDF::loadView('output',['data' => $data]);
return $pdf->stream('output.pdf');
}
my output.blade.php
<body>
<div class="container">
<table class="table table-sm table-bordered" >
<thead>
<tr>
<th scope="col-sm-1 col-6">Data Affl.</th>
<th scope="col-sm-2">Cron.</th>
<th scope="col-sm-2">Cognome</th>
<th scope="col-sm-2">Nome</th>
<th scope="col-sm-2">Data nac.</th>
</tr>
</thead>
<tbody>
#foreach($data as $row)
<tr>
<td>{{$row->Cron}}</td>
<td>{{$row->Surname}}</td>
<td>{{$row->Name}}</td>
<td>{{$row->Date}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</body>
if i load a normal view of my query it works well.
Any suggest?
Thanks in advance
you can use
compact('data')
instead of
$data

MVC3 ASP Replace null value with empty space on the view

I have the following view which returns some text if the POnumber is null.
What I think I need to have instead of the if(Model.Invoice.PONumber == null) is a check mechanism ( maybe multiple if statements ) that will check the fields LineNumber, Description, UnitOfMeasure, QtyOrdered and if any of them is null it will replace it with N/A or empty space but it will still allow the user to see the rest of information available.
Do you have any sugestions? I am new to MVC and any help will be apreciated.
Thank you in advance for your time and help,Bobby
<div class="contentWrapper2">
<div class="content2">
<div class="clr lfl w100">
<h1>Invoice Detail</h1>
<div class="return-btn">
<a class="btn btnStyleC btn-back-invoice" href="#Url.Action("InvoiceHistory", "Account")">
Back to Invoice List</a>
</div>
</div>
#if (Model.ErpError.Length > 0)
{
<div class="clr lfl w100 error">
#Html.Raw(Model.ErpError)
</div>
}
else
{
if(Model.Invoice.PONumber == null)
{
<div class="lfl w100 clr messaging">
<p>No information available at the moment for current invoice.
Please call our sales department for further assistance.
</p>
</div>
}
else
{
<div class="clr lfl w100">
<div class="order-number-date">
<table>
<tr>
<th class="col-1">
<h3>Invoice #:</h3>
</th>
<td class="col-2">
<h3>#Model.Invoice.InvoiceNumber</h3>
</td>
</tr>
<tr>
<th class="col-1">
<h3>Invoice Date:</h3>
</th>
<td class="col-2">
<h3>#Model.Invoice.InvoiceDate.ToShortDateString()</h3>
</td>
</tr>
</table>
</div>
<div class="order-number-date">
<table>
<tr>
<th class="col-1">
<h3>Order #:</h3>
</th>
<td class="col-2">
<h3>#Model.Invoice.OrderNumber</h3>
</td>
</tr>
<tr>
<th class="col-1">
<h3>PO #:</h3>
</th>
<td class="col-2">
<h3>#Model.Invoice.PONumber</h3>
</td>
</tr>
<tr>
<th class="col-1">
<h3>Due Date:</h3>
</th>
<td class="col-2">
<h3>#Model.Invoice.DueDate.ToShortDateString()</h3>
</td>
</tr>
</table>
</div>
</div>
<div class="clr lfl w100">
<div class="bill-ship">
<table>
<tr>
<th>
<h4>Billing Information</h4>
</th>
</tr>
<tr>
<td>#Model.Invoice.BTDisplayName
</td>
</tr>
<tr>
<td>
<#Html.Raw(Model.Invoice.BTAddress1)
</td>
</tr>
#if (!string.IsNullOrEmpty(Model.Invoice.BTAddress2))
{
<tr>
<td>#Html.Raw(Model.Invoice.BTAddress2)
</td>
</tr>
}
<tr>
<td>#Html.CityCommaStateZip(Model.Invoice.BTCity, Model.Invoice.BTState, Model.Invoice.BTZip)</td>
</tr>
<tr>
<td>#Model.Invoice.BTCountry
</td>
</tr>
<tr>
<td>#Model.Invoice.BTPhone1</td>
</tr>
<tr>
<td>#Model.Invoice.BTEmail
</td>
</tr>
</table>
</div>
</div>
if (Model.Invoice.InvoiceLines.Count > 0)
{
<div class="clr lfl w100 line-item-detail">
<table class="info-tbl">
<tr>
<th class="vid-item">Item #</th>
<th class="vid-desc">Description</th>
<th class="vid-um">
U/M
</th>
<th class="vid-qty">
Qty
</th>
<th class="vid-ship">
Ship Date
</th>
#if (Model.ShowPackslip)
{
<th class="vid-pack">Pack Slip</th>
}
<th class="vid-unit">Unit Price</th>
<th class="vid-ext">Ext Price</th>
</tr>
#foreach (var invoiceLine in Model.Invoice.InvoiceLines)
{
<tr>
<td class="vid-line">#invoiceLine.LineNumber</td>
<td class="vid-desc">#invoiceLine.Description</td>
<td class="vid-um">#invoiceLine.UnitOfMeasure</td>
<td class="vid-qty">#invoiceLine.QtyOrdered</td>
<td class="vid-ship">
#if (invoiceLine.ShipDate.ToShortDateString() == "1/1/0001")
{
}
else
{
#invoiceLine.ShipDate.ToShortDateString()
}
</td>
#if (Model.ShowPackslip)
{
<td class="vid-pack">
#invoiceLine.PackSlip
</td>
}
<td class="vid-unit">#invoiceLine.UnitPrice.ToCurrency()
</td>
<td class="vid-ext">#invoiceLine.ExtendedPrice.ToCurrency()
</td>
</tr>
}
</table>
</div>
}
<div class="clr lfl w100">
<table class="tbl-total">
<tr class="subtotal">
<th class="col-1">Subtotal</th>
<td class="col-2">#Model.Invoice.OrderSubTotal.ToCurrency()
</td>
</tr>
#if (Model.Invoice.DollarOffOrder > 0)
{
<tr>
<th class="col-1">Order Discount</th>
<td class="col-2">#Model.Invoice.DollarOffOrder.ToCurrency()</td>
</tr>
}
#if (Model.Invoice.ShippingAndHandling > 0)
{
<tr>
<th class="col-1">Shipping</th>
<td class="col-2">#Model.Invoice.ShippingAndHandling.ToCurrency()
</td>
</tr>
}
#if (Model.Invoice.MiscCharges > 0)
{
<tr>
<th class="col-1">Misc. Charges</th>
<td class="col-2">#Model.Invoice.MiscCharges.ToCurrency()</td>
</tr>
}
<tr>
<th class="col-1">Sales Tax</th>
<td class="col-2">#Model.Invoice.TotalTax.ToCurrency()</td>
</tr>
<tr>
<th class="col-1">Invoice Total</th>
<td class="col-2">#Model.Invoice.InvoiceTotal.ToCurrency()</td>
</tr>
</table>
</div>
<div class="clr lfl w100">
<a class="btn btnStyleB btn-print" href="javascript:window.print();">Print</a>
</div>
}
}
</div>
</div>
You could create a template called for example "nullcheck.cshtml" like:
#if (ViewBag.ValueToCheck == null) {
<div class="lfl w100 clr messaging">
<p>
No information available at the moment for #(ViewBag.Field).
Please call our sales department for further assistance.
</p>
</div>
}
else {
#Html.Partial(ViewBag.TargetTemplate, Model)
}
Then you call it from your main view:
#{
ViewBag.TargetTemplate = "okModel";
ViewBag.Field = "P.O.Number";
ViewBag.ValueToCheck = Model.Invoice.PONumber;
Html.RenderPartial("nullCheck", Model, ViewBag);
}
okModel.cshtml should be the part of your template you will display when the value is not null...
I haven't tested this myself but it should give you some ideas... contact me if things go wrong XD
Cheers!
This seems like something you should take care of in your controller.
public ActionResult YourControllerAction()
{
var myViewModel = SomeService.GetMyViewModel();
if (myViewModel.Invoice.PONumber == null)
{
myViewModel.Invoice.PONumber = "N/A";
}
//etc
}
This leaves your view clearer (my personal preference)
However in the view you could simply use the null coalescing operator like so:
#Model.Invoice.PONumber ?? "NA"

Grid generated with JQuery template need to reset using Ajax not working

Sometime working and sometime not.
I am trying to generate Grid with the help of JQuery Template via Ajax once record is added or deleted. In js file
$('.gridRow').remove();
is not working properly. Someone tell me how to reset grid to fill it again. Below is the code.
JS File
var ReloadGrid = (function(){
$.getJSON("/HeaderMenu/GetHeaderGrid", function(data) {
$('.gridRow').remove();
(data.length <= 0) ? $("#gridBtn").hide() : $("#gridBtn").show();
for (var i=0; i<data.length; i++) { data[i].num = i+1; }
$('#gridTemplate').tmpl(data).appendTo('table.gridTable > tbody');
});
});
on MVC3 cxhtml page
<script id="gridTemplate" type="text/x-jquery-tmpl">
<tr class="gridRow">
<td class="cellTd ">
<input type="checkbox" id="deleteCb" />
<input type="hidden" id="Id_ + ${num}" class="idField" value="${Id}" />
</td>
<td class="cellTd">
<input id="index" name="index" class="numberField" type="text" value="${IndexOrder}" />
</td>
<td class="cellTd">${DisplayName}</td>
<td class="cellTd ">${UrlName}</td>
<td class="cellTd ">
<input type="checkbox" id="activeCb" {{if Active}} checked{{/if}} />
</td>
</tr>
</script>
<div class="gridDiv">
<table class="gridTable" cellspacing="0" cellpadding="0">
<tbody>
<tr class="gridTitleRow">
<td class="iconLink width36">Delete</td>
<td class="iconLink width60">Sort Order</td>
<td class="iconLink widthAuto">Display Name</td>
<td class="iconLink widthAuto">Url Name</td>
<td class="iconLink widthAuto">Active</td>
</tr>
</tbody>
</table>
</div>
I usually empty the wrapper instead of the row.
$('table.gridTable > tbody').empty();
But for that to work you'd have to change your table to use thead
<table class="gridTable" cellspacing="0" cellpadding="0">
<thead>
<tr class="gridTitleRow">
<th class="iconLink width36">Delete</th>
<th class="iconLink width60">Sort Order</th>
<th class="iconLink widthAuto">Display Name</th>
<th class="iconLink widthAuto">Url Name</th>
<th class="iconLink widthAuto">Active</th>
</tr>
<thead>
<tbody>
</tbody>
</table>

Resources