Angular 5 - get list after sort header - sorting

I use Angular 5, Angular Material and Angular DataTable.
I have following table
HTML file :
<div class="material-datatables table-responsive">
<table id="example" datatable [dtOptions]="dtOptions" class="table table-striped table-no-bordered table-hover" cellspacing="0" width="100%" style="width:100%">
<thead>
<tr>
<th><mat-checkbox (change)="$event ? masterToggle(dataTable.dataRows) : null"
[checked]="selection.hasValue() && isAllSelected(dataTable.dataRows.length)"
[indeterminate]="selection.hasValue() && !isAllSelected(dataTable.dataRows.length)">
</mat-checkbox></th>
<th>{{ dataTable.headerRow[1] }}</th>
<th>{{ dataTable.headerRow[2] }}</th>
<th class="disabled-sorting text-right">Action</th>
</tr>
</thead>
<tfoot>
<tr>
<th>{{ dataTable.footerRow[0] }}</th>
<th>{{ dataTable.footerRow[1] }}</th>
<th>{{ dataTable.footerRow[2] }}</th>
<th class="text-right">Action</th>
</tr>
</tfoot>
<tbody>
<tr *ngFor="let row of dataTable.dataRows">
<td><mat-checkbox (click)="$event.stopPropagation()"
(change)="$event ? selection.toggle(row) : null"
[checked]="selection.isSelected(row)">
</mat-checkbox>
</td>
<td>{{row[0]}}</td>
<td>{{row[1]}}</td>
<td class="text-right">
<button (click)="openIndex(row)" class="btn btn-simple btn-info btn-icon" matTooltip="Indexer" [matTooltipPosition]="'left'">
<i class="material-icons">assignment</i>
</button>
<button (click)="onSubmitDel(row)" class="btn btn-simple btn-danger btn-icon" matTooltip="Supprimer" [matTooltipPosition]="'right'">
<i class="material-icons">delete</i>
</button>
</td>
</tr>
</tbody>
</table>
The different sorts work nice. The list is sorted on the screen.
I add function (button) that pass the list to another component. The problem is when I sort the list and I click on button, the list is not sorted for this other component.
How can I retrieve the sorted list ?
* **EDIT: this is image to show the case :
Initial list with new button :
After sorting:

This is solution : use dt.buttons.exportData().body
in dtOptions : add the following lines:
buttons: [
'print',
'pdf',
'excel',
{
text: 'Sélection',
className: 'btn btn-success',
action: function (e, dt, node, config) {
console.log(dt.buttons.exportData().body);
}
}
]
It keeps filters and sorts you use

Related

from 2 modals, open one modal base on if else condition

I have 2 modals (Modal1 & Modal2), in table after clicked on action button "edit" base on data, i need to open any one modal.
Say if age is less than 36 months then Modal 1 Else Modal 2 should open. Both modals have few data common and few are added newly.
This is my table code
<table id="cowcalfsummery" class="table table-bordered table-hover table" style="width:100%">
<thead>
<tr>
<th>ID</th>
<th>Breed Type</th>
<th>Gender</th>
<th>Birth Date</th>
<th>Weight (Kg)</th>
<th>Age (in Months)</th>
<th>Maturity Date</th>
<th>Health Status</th>
<th>Action</th>
</tr>
<tbody>
#foreach ($cowcalfdata as $item )
<tr id="item{{$item->id}}">
<td>{{$item->calfID}}</td>
<td>{{$item->breedtype}}</td>
<td>{{$item->gendertype}}</td>
<td>{{ date('d-m-Y', strtotime ($item->birthdate) ) }}</td>
<td>{{$item->weight}}</td>
<td>{{$item->age}}</td>
<td>{{ date('d-m-Y', strtotime ($item->maturitydate) ) }}</td>
<td>{{$item->health}}</td>
<td>
<i class="fas fa-eye" title="View"></i>
**<button type="button"** class="editcowcalfmodal btn btn-success" data-info="{{$item->id}},{{$item->calfID}},{{$item->birthdate}},{{$item->age}},{{$item->breedtype}},{{$item->gendertype}},{{$item->health}},{{$item->status}},{{$item->color}},{{$item->weight}},{{$item->height}},{{$item->previousvaccinedone}},{{$item->maturitydate}}" data-toggle="modal" data-target="#editcowcalfmodal"><i class="far fa-edit" title="Edit"></i></button>
<button type="button" class="btn btn-danger deletecowcalfdatabtn" data-cowcalfid="{{$item->calfID}}"><i class="fas fa-trash" title="Delete"></i></button>
</td>
</tr>
#endforeach
</tbody>
</thead>
</table>
Thanks in Advance.
You want to create a blade If statement and check if $item->age is below 36.
if it is you want to show the first button, else show the second button with a different data-target to your second modal.
#if($item->age < 36)
<button type="button"
class="editcowcalfmodal btn btn-success"
data-info="
{{$item->id}},
{{$item->calfID}},
{{$item->birthdate}},
{{$item->age}},
{{$item->breedtype}},
{{$item->gendertype}},
{{$item->health}},
{{$item->status}},
{{$item->color}},
{{$item->weight}},
{{$item->height}},
{{$item->previousvaccinedone}},
{{$item->maturitydate}}"
data-toggle="modal"
data-target="#editcowcalfmodal">
<i class="far fa-edit" title="Edit"></i>
</button>
#else
<button type="button"
class="editcowcalfmodal btn btn-success"
data-info="
{{$item->id}},
{{$item->calfID}},
{{$item->birthdate}},
{{$item->age}},
{{$item->breedtype}},
{{$item->gendertype}},
{{$item->health}},
{{$item->status}},
{{$item->color}},
{{$item->weight}},
{{$item->height}},
{{$item->previousvaccinedone}},
{{$item->maturitydate}}"
data-toggle="modal"
data-target="#MySecondModal">
<i class="far fa-edit" title="Edit"></i>
</button>
#endif
You also have your table wrong.
You first want to create a <thead> (table header). fill it with table titles, close the <thead> and after that you create the <tbody> (table body) with the content of the item.
This should create a structure like this:
<table>
<thead>
<tr>
<th>Age</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>12</td>
<td>Cow 1</td>
</tr>
</tbody>
</table>
More information about tables and how to use them

Laravel Livewire checkbox checked on edit page

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();
}

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.

Laravel 5.2 pagination: how to decrease distance from table

I have a Laravel 5.2 application. I'm using the default Laravel pagination. All works functionally but there is quite some empty space between the last row of the table and the pagination link. See picture for an example:
How can I make the empty space a bit less?
Here is the code I'm using:
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th class="hidden-xs">Auction id</th>
<th>Name</th>
<th>Description</th>
<th><em class="fa fa-cog"></em>Actions</th>
</tr>
</thead>
<tbody>
#foreach( $events as $event )
<tr>
<td align="center" class="hidden-xs">{{ $auction->id }}</td>
<td>{{ $event>name }}</td>
<td>{{ $event->description }}</td>
<td align="center">
<div>
<em class="fa fa-list-alt"></em>
</div>
</tr>
#endforeach
</tbody>
</table>
<div class="text-right">
{{ $events->render() }}
</div>
<div class="text-right">
<!-- Your pagination code here -->
</div>

Resources