Livewire checkbox selecting items on previous page for a paginated page - php

Please help, I'm currently working on this livewire project. The page is paginated with checkboxes. When I check boxes on the next page, it reloads the data on the first page and selects another item of the same number in the row. So if try to select a checkbox of row 3 on the second page, data will refresh to show items of previous page, and select the checkbox of row 3
I'm unable to figure it out, someone please help.
Below is my code
{
$tracking_dates = Tracking::select(DB::raw('max(created_at) as created_at'))
->groupBy('tracking_number')
->get();
$this->trackings = Tracking::query()
->when(Auth::user()->hasRole('user'), function($query){
$query->whereHas('PurchaseRequest', function($sub_query) {
$sub_query->where('user_id', '=', Auth::id());
});
})
->whereIn('created_at', $tracking_dates)
->paginate(20);
return view('livewire.index-tracker', [
'trackings' => $this->trackings,
'trackingStatus' => TrackingStatus::all(),
]);
}
My view Code
<form>
<table id="datatable-buttons" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
<thead>
<tr>
#role('superadministrator')
<th>Multiple Action Select</th>
#endrole
<th>Item(s) Type</th>
<th>Tracking Number</th>
<th>Tracking Status</th>
<th>Invoice</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#if(count($trackings) > 0)
#foreach($trackings as $key=>$tracking)
<tr>
#role('superadministrator')
<td>
{{$tracking->id}}
<input type="checkbox" value="{{$tracking->id}}" wire:model="selected">
</td>
#endrole
<td>
{{$tracking->purchase_request_id != "" ? "Purchase Request" : NULL}}
{{$tracking->shipment_id != "" ? "Shipment" : NULL}}
</td>
<td>{{$tracking->tracking_number}}</td>
<td>{{$tracking->TrackingStatus->name}}</td>
<td>
<i class="mdi mdi-eye font-18"></i>
</td>
<td>
<i class="mdi mdi-eye font-18"></i>
{{-- #role('superadministrator')--}}
<i class="mdi mdi-pencil font-18"></i>
{{-- #endrole--}}
</td>
</tr>
#endforeach
#else
<tr>
<td colspan="5">No data available</td>
</tr>
#endif
</tbody>
</table>
#role('superadministrator')
<div class="form-group row">
<button wire:click.prevent="store()" class="btn btn-primary btn-md">Track</button>
</div>
</form>
#if(count($trackings) > 0)
{{ $trackings->links() }}
#endif```

You can call the javascript page.dt() event and store all ids you have checked into local storage.
Please refer below code for that.
var checkedData = [];
$('#tableID').on( 'page.dt', function () {
let allIds = localStorage.getItem("row-data-ids").split(",");
$('.checkboxes:checkbox:checked').each(function(index, rowId){
if($.inArray( $(this).val(), allIds) == -1){
checkedData.push($(this).val());
}
});
localStorage.setItem('row-data-ids', checkedData);
} );
You can also store local storage values to hidden variable to get into POST Request.

Related

Passing variable from index to controller using Laravel

I am new to using Laravel and I am currently trying to pass a variable from the index blade file to the controller in order to bring up all apparatus type fields associated with a specific apparatus code id.
I am able to access the apparatus type fields associated with the apparatus code id when I include a specific number which correlates to that apparatus code id (eg. inputting 2 brings up the apparatus type details for the apparatus code id 2, inputting 3 brings up the apparatus type details for the apparatus code id 3 etc).
However when I have tried to pass a variable relating to each individual apparatus code id using a for loop in the controller, the apparatus type loop appears to not be accessed and does not return any information. I will include snippets of my current code for clarity.
Any help or guidance with this issue would be greatly appreciated!
Controller file
public function index()
{
$apparatusCodes = ApparatusCodes::get();
foreach ($apparatusCodes as $apparatusCode){
$apparatusTypes = ApparatusTypes::where('apparatus_code_id', $apparatusCode->id)->get();
}
return view('apparatus_codes.index', compact('apparatusCodes', 'apparatusTypes'));
}
Index.blade file
<table id="datatable" class="stripe hover dt-responsive display nowrap" style="width:100%; padding-top: 1em; padding-bottom: 1em;">
<thead>
<tr>
<th>ID</th>
<th >Apparatus Code</th>
<th>Description</th>
<th>Rent</th>
<th></th>
</tr>
</thead>
<!--start of for loop-->
#foreach ($apparatusCodes as $apparatusCode)
<tbody>
<tr data-toggle="collapse" id="table{{ $apparatusCode->id}}" data-target=".table{{ $apparatusCode->id}}">
<td> {{ $apparatusCode->id}} </td>
<td>{{ $apparatusCode->apparatus_code}} </td>
<td> {{ $apparatusCode->description}}</td>
<td> {{ $apparatusCode->rent}}</td>
<td #click="isOpen = !isOpen" class="main-bg"><img class="mb-1 duration-300 h-6 w-6" :class="{'transform rotate-180' : isOpen}"
src="../img/Icon-arrow-dropdown-white.png" alt="arrow down">
</td>
<td><img class="mb-1 duration-300 h-6 w-6" :class="{'transform rotate-180' : isOpen}"
src="../img/edit-icon.svg" alt="Edit Icon">
</td>
</tr>
<tr x-show.transition.duration.300ms.origin.bottom="isOpen" x-cloak #click.away="isOpen = false" class="collapse table{{ $apparatusCode->id}}">
<td colspan="999">
<div>
<table id="datatable" class="table table-striped">
<thead>
<tr>
<th>Apparatus Code </th>
<th>Apparatus Type</th>
<th>Compensation </th>
<th>Created On </th>
<th>Created By </th>
<th></th>
#foreach ($apparatusTypes as $apparatusType)
</thead>
<tbody>
<tr>
<td>{{ $apparatusCode->apparatus_code}}</td>
<td>{{ $apparatusType->apparatus_type }}</td>
<td>{{ $apparatusType->compensation }}</td>
<td>{{ $apparatusType->created_at }}</td>
<td>{{ $apparatusType->users->name}}</td>
<td> <button type="button" class="btn btn-default btn-edit js-edit"><img class="mb-1 duration-300 ml-4 inset-0 h-6 w-6" src="/../../img/Icon-edit-gray.png" alt="edit"></button></td>
</tr>
#endforeach
</table>
</tr>
</tr>
</td>
</tbody>
#endforeach
</table>
If you have the relationships setup you could eager load the ApparatusTypes for each ApparatusCode:
$apparatusCodes = ApparatusCodes::with('appartusTypes')->get();
Then in the view you could iterate the appartusTypes of each ApparatusCode:
#foreach ($apparatusCodes as $apparatusCode)
...
#foreach ($apparatusCode->apparatusTypes as $type)
...
#endforeach
...
#endforeach
Without using the relationships you could get all the ApparatusTypes for the ApparatusCodes and then group them by apparatus_code_id:
$codes = ApparatusCodes::get();
$types = ApparatusTypes::whereIn('apparatus_code_id', $codes->pluck('id'))
->get()
->groupBy('apparatus_code_id');
Then in the view you could iterate the group that corresponds to the current Code's apparatus_code_id:
#foreach ($codes as $code)
...
#foreach ($types->get($code->id, []) as $type)
...
#endforeach
...
#endforeach
Laravel 8.x Docs - Eloquent - Relationships - Eeager Loading with
Laravel 8.x Docs - Collections - Available Methods - pluck
Laravel 8.x Docs - Collections - Available Methods - groupBy

i want to check all my checkbox from one checkbox using laravel 8 mvc

I want to check the first checkbox that have the id checkAll and it will check all my checkboxes and when I unchecked it, it will unchecked all too.
My blade index.blade.php:
<body>
<table class="table">
<thead>
<tr>
<th scope="col">
<input type="checkbox" id="checkAll">
</th>
<th scope="col">id</th>
<th scope="col">title</th>
<th scope="col">Paragraph</th>
</tr>
</thead>
#foreach ($categories as $categorie)
<tbody>
<tr>
<td>
<input type="checkbox" name="checkOne" value="{{$categorie->id}}">
</td>
<th scope="row">{{ $categorie->id }}</th>
<td>{{ $categorie->Title }}</td>
<td>{{ $categorie->Paragraph }}</td>
<form action={{ route('categorie.destroy', $categorie->id) }} method="POST">
#csrf
#method('DELETE')
<td>
<button class="btn btn-danger" type="submit">
delete
</button>
</form>
<button class="btn btn-warning"> <a href={{ route('edit', $categorie->id) }}>edit</a> </button>
</td>
</tr>
</tbody>
#endforeach
</table>
</body>
If you have multiple checkboxes with the same name then use array in your loop, as below:
<input type="checkbox" name="checkOne[]" value="{{ $categorie->id }}">
Now you need to use jQuery to select/unselect checkboxes, use below code:
$('#checkAll').change(function() { // main checkbox id
$('input[name="checkOne[]"]').prop('checked', $(this).is(":checked"));
});
Try this code -
<script type='text/javascript'>
function checkAll(e) {
const checkboxes = document.getElementsByName('checkOne');
for (let i = 0; i < checkboxes.length; i++) {
checkboxes[i].checked = e.checked;
}
}
</script>

hidden division is not appearing after operating click event for dynamic table

I am fetching data to a table from mysql database using php and jquery. But I want to keep the "details" column hidden, which I want should be slide down(appear ) in the corresponding table row after clicking a button named "show details". The following Image may clear my thought:
The jquery Code I am using for slide down is :
$(document).ready(function(){
$("#get_details").click(function(){
$("#get_details_button").slideToggle("slow");
});
});
The code I am using to fetch data is :
$sub_array = array();
$sub_array[] = $row["id"];
$sub_array[] = $row["product_name"].'<p id="get_details">'.$row["details"].'</p>';
$sub_array[] = '<button type="button" name="get_details_button" id="get_details_button" class="btn btn-success btn-xs get_details_button">Get Details</button>';
$data[] = $sub_array;
}
Please feel free to ask for any additional code if you need.
the html part is here :
<table id="user_data" class="table table-bordered table-striped">
<thead>
<tr>
<th width="30%">Image</th>
<th width="50%">Product Name</th>
<th width="20%">get Details</th>
</tr>
</thead>
</table>
You need to use class for p tag then whenever button is clicked use $(this).closest('tr').find(".get_details") to get p tag and show/hide same
Demo Code :
$(document).ready(function() {
//onclick of button
$(".get_details_button").click(function() {
//get p tag and show/hide it
$(this).closest('tr').find(".get_details").slideToggle("slow");
});
});
.get_details {
display: none
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="user_data" class="table table-bordered table-striped">
<thead>
<tr>
<th width="30%">Image</th>
<th width="50%">Product Name</th>
<th width="20%">get Details</th>
</tr>
</thead>
<tr>
<td>1
</td>
<td>Soemthig,,
<!--use class-->
<p class="get_details">Soemthing.....</p>
</td>
<td><button type="button" name="get_details_button" class="btn btn-success btn-xs get_details_button">Get Details</button>
</td>
</tr>
<tr>
<td>2
</td>
<td>Soemthig,,2
<p class="get_details">Soemthing2.....</p>
</td>
<td><button type="button" name="get_details_button" class="btn btn-success btn-xs get_details_button">Get Details</button>
</td>
</tr>
</table>
You don't need any ids to make this work. I don't know your markup but here is an example. You will need to work it out for your markup.
<div class="container">
<table>
<tr>
<td>{{ id }}</td>
<td>{{ product_name }}</td>
<td><button>details</button></td>
</tr>
</table>
<div class="product-details" style="display:none">...</div>
</div>
<div class="container">
<table>
<tr>
<td>{{ id }}</td>
<td>{{ product_name }}</td>
<td><button>details</button></td>
</tr>
</table>
<div class="product-details" style="display:none">...</div>
</div>
<div class="container">
<table>
<tr>
<td>{{ id }}</td>
<td>{{ product_name }}</td>
<td><button>details</button></td>
</tr>
</table>
<div class="product-details" style="display:none">...</div>
</div>
jquery
$('.container').find('button').on('click', function(e) {
e.preventDefault();
$(this).parent().parent().parent().parent().find('.product-details').slideToggle();
});

search for data in a table in laravel

im trying to search for name or id of students in the table but i do not know how to do that with router and controller. I already have the table and search form with button i need the list to show all data initially then display only the entered data when a name is searched
here is my search form
<!-- Search form -->
<form class="d-flex align-items-center flex-nowrap" action="/search">
<input name="q" class="form-control" type="text" placeholder="Search" aria-label="Search">
<button class="btn btn-sm btn-info">Search</button>
</form>
and this is the table
<table class="table">
<thead class="thead-light">
<tr>
<th scope="col">id</th>
<th scope="col">First name</th>
</tr>
</thead>
<tbody>
#foreach($students as $student)
<tr>
<td>{{ $student->cne }}</td>
<td>{{ $student->firstName }}</td>
<td>
Edit
</td>
</tr>
#endforeach
</tbody>
</table>
I would love the realtime search thingy so I can get rid of the search button lol
thank you
This is how I would do it (without the realtime thingy, check my comment for that)
In your controller that returns your students, you can grab the request and if it has a q parameter then you can filter your query.
public function index(Request $request)
{
$students = Student::query();
if($request->has("q") {
$students->where("cne", $request->get("q"))
->orWhere("firstName", $request->get("q")
}
return view("students.index", [
"students" => $students->get();
]);
}
You can use a LIKE operator if you'd prefer.

How to remain same pagination page on table after action

i have created a table (using datatables) and in the table a few actions. in the table i have a button to do a function "delete", also using datatables i get pagination.. So for example i am at page 5, and i want to delete a row when i click delete it refreshes the page and goes back to page no 1. How can i make it remain at page 5 after the action delete? Below are my table and controller function
viewStatistics.blade:
<table class="table table-hover demo-table-search table-responsive-block alt-table" id="tableWithSearch">
<thead>
<tr>
<th class="text-center" style="width:80px;">ID</th>
<th class="text-center">Date</th>
<th class="text-center">Question <br> Asked</th>
<th class="text-center">Low <br> Confidence</th>
<th class="text-center">No <br> Answer</th>
<th class="text-center">Missing <br> Intent</th>
<th class="text-center">Webhook <br> Fail</th>
<th class="text-center">Status</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tbody>
#foreach($data as $sessionID => $value)
<tr>
<td class="text-center">{{$value['identifier']}}</td>
<td class="text-center">{{date("d M", strtotime($value['dateAccess']))}}</td>
<td class="text-center">{{$value['total']}}</td> <!-- question asked -->
<td class="text-center">{{$value['low confidence']}}</td> <!-- low confidence -->
<td class="text-center">{{$value['no answer']}}</td> <!-- no answer -->
<td class="text-center">{{$value['missing intent']}}</td> <!-- missing intent -->
<td class="text-center">{{$value['webhook fail']}}</td> <!-- webhook fail -->
<td class="text-center" style="{{$value['status'] == 'Reviewed' ? 'color:green' : 'color:red'}}">
{{$value['status']}}
#if($value['status'] == 'Pending')
<input type="checkbox" name="check" class="check" value="{{$sessionID}}">
#endif
</td> <!-- status -->
<td class="text-center">
<div class="btn-group">
<button type="button" class="btn alt-btn alt-btn-black dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Manage</button>
<ul class="dropdown-menu">
<li>
View
</li>
<li>
Delete
</li>
#if($value['status'] == 'Pending')
<li>
Mark as Done
</li>
#endif
</ul>
</div>
</td> <!-- action -->
</tr>
#endforeach
</tbody>
</table>
Controller:
public function deleteStatistics($companyID, $sessionID)
{
DiraChatLog::where('company_id', $companyID)->where('sessionID', $sessionID)->delete();
return redirect(action('AltHr\Chatbot\TrackerController#viewStatistics', compact('companyID')))->with('notification',[
'status' => 'success',
'title' => 'Statistics Deleted',
'message' => 'The Statistics have been deleted.'
]);
}
There is an option named stateSave in jquery datatables. Please check the code below:
$('#example').dataTable({ stateSave: true });

Categories