How to remain same pagination page on table after action - php

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

Related

How to show table content based on the foreignId in Laravel?

So I have a table called pekerjaan (work) and it is in an eloquent with another table called jeniskerja (type of work), jeniskerja hasMany pekerjaan, and pekerjaan belongsTo jeniskerja... I want to show the list of the work based on the type of the work which is the foreignId when the user click on the type shown below (if the type is A it will show list of works of type A), but the problem is I already shown the table based on another foreignId, in this case it is the penyedia (vendor)... So, at the moment the table in foreach section show all the list of works based on the penyedia_id (vendor_id)... How do I create another filter whereby when the user click on the type (in this case a card header) it will expand and show the list of works table based on the type they are clicking?
Here is what my code looks like:
AdminController
public function showpenyedia(Penyedia $penyedia){
$pekerjaan = $penyedia->pekerjaans; //show pekerjaan(work) based on penyedia(vendor)
$jeniskerja = Jeniskerja::all();
return view('admin.showpenyedia', compact('penyedia','pekerjaan','jeniskerja'));
}
detailpenyedia.php (this is where the table content is)
#foreach ($jeniskerja as $jeniskerjas)
<section class="content">
<div class="container-fluid">
<div class="card card-default collapsed-card">
<div class="card-header collapsed-card">
<h3 class="card-title">
<b>{{$jeniskerjas->nama_jenis}}</b> //show the name of the type
</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse">
<i class="fas fa-plus"></i>
</button>
</div>
</div>
<div class="card-body table-responsive">
<table id="tabelpekerjaan" class="table table-bordered">
<thead>
<tr>
<th style="width: 10px" rowspan="2">Tahun Anggaran</th>
<th rowspan="2">Tanggal Kontrak</th>
<th rowspan="2">Paket Pekerjaan</th>
<th rowspan="2">Nama Perusahaan</th>
<th rowspan="2">Lokasi Pekerjaan</th>
<th rowspan="2">Jenis Pekerjaan</th>
<th rowspan="2">HPS</th>
<th rowspan="2">Nilai</th>
<th rowspan="2">Dokumen</th>
<th colspan="2">Tanggal</th>
</tr>
<tr>
<td>Tgl Buat</td>
<td>Tgl Update</td>
</tr>
</thead>
<tbody>
#php $no = 1; /*$totalNilai = 0.0;*/ #endphp
#foreach ($pekerjaan as $pekerjaans)
<tr>
<td>{{$pekerjaans->tanggal->format('Y')}}</td>
<td>{{$pekerjaans->tanggal->format('d/m/Y')}}</td>
<td>{{$pekerjaans->pekerjaan}}</td>
<td>{{$pekerjaans->penyedia->nama}}</td>
<td>{{$pekerjaans->lokasi}}</td>
<td>{{$pekerjaans->jeniskerja->nama_jenis}}</td>
<td>Rp. {{number_format($pekerjaans->hps,0,',',',')}}</td>
#php
$pekerjaans->nilai_total = $pekerjaans->nilai_1 + $pekerjaans->nilai_2 + $pekerjaans->nilai_3 + $pekerjaans->nilai_4;
#endphp
<td>{{$pekerjaans->nilai_total}}</td>
<td><u>{{$pekerjaans->status}}</u></td>
<td>
</td>
<td>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</section>
#endforeach
Here is how my page look like atm:

Livewire checkbox selecting items on previous page for a paginated page

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.

use span to hide and show content

i am new in both php and jquery. i want to use span to hide and show content in html and php. can i use jquery? if yes, what should i do? i already put the jquery script to the bottom of page. this is my code :
<!-- reply -->
<span title="fas fa-reply" name="reply" data-id="1"
</span>
<!-- if span clicked show all these data... -->
<table class="table table-borderless" style="margin-top:50px;">
<thead class="bg-secondary pb-0">
<tr>
<th colspan="3" class="pb-0">
<p class="test text-light">Creating Reply <?php echo $rowAllPost["id"]; ?>
</p></th>
<th class="pb-0 text-right">
</th>
</tr>
</thead>
<th scope="row" style="width:15rem!important;">
<img src="<?php echo $rowViewUserNow["picture"];?>" class="rounded-circle" style=height:93px;width:81px;><br>
<p class="ml-3"> Online</p>
<p class="ml-2"> <?php echo $rowViewUserNow["username"];?>
</p>
<i class="fas fa-info-circle pr-2 pt-2"></i>Active
<td colspan="3" class="text-justify">
<form action="controller/doCheckPost.php" method="post">
<textarea name="description" class="form-control"></textarea>
</td>
</tr>
<tr class="bg-secondary pb-0" >
<th colspan="3" class="pb-0">
<button title="Cancel" class="btn" type="submit" name="cancel"><i class="fas fa-times"></i></th></button>
<th class= "pb-0 text-right">
<button title="Submit" class="btn" name="submit"><i class="fas fa-check"></i></button>
</form>
</th>
</th>
</tr>
</div>
</tbody>
</table>
The best way would be to first give an id to both the span and table elements. Then you can use an event handler in jQuery like this:
$('#spanId').on('click', function() {
$('#tableId').toggle();
});
Toggle will show the element if it's hidden and hide it if it's visible.

Update multiple rows in the index view Laravel

I am a beginner in laravel and I am trying to update all the records in the index view without having to enter to a different view to edit each item.
The idea is to click a button, and when I click on it the input data is enable to change the quantity of all data that is show in the index; and then have a submit button to update all the records.
At the moment when I click on the button it only enable the inputs of the first row, how can i do to enable all of them?
And how can I make a function in the controller to update all the records in my datatable at once, only clicking on a submit button??
I dont really know how to edit nd update the data in the same index page!!!
PLEASE HELP ME.
This is the default index view
This is what happens when i click the "Editar cantidades" button
View
#extends ('layouts.admin')
#section ('contenido')
<div class="panel-heading">
<h4>
Crear nuevo consumible
Listado de consumibles
</h4>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-9">
#include('inventlab.consumibles.search')
</div>
<div class="col-md-3">
<a id="enablebutton" class="pull-right btn btn-default" >Editar cantidades</a>
</div>
</div>
<table class="table table-hover nowrap" id="example" width="100%">
<thead class="thead-default">
<tr>
<th>Opciones</th>
<th>Nombre</th>
<th style="text-align: center">Sin abrir</th>
<th style="text-align: center">En uso</th>
<th style="text-align: center">Cantidad total</th>
</tr>
</thead>
#foreach ($consumibles as $cons)
<tbody>
<tr>
<td>
<i class="icmn-pencil5"> </i>
<i class="icmn-bin"> </i>
</td>
<td>{{ $cons->nom_cons }}</td>
<td style="text-align: center">
<input type='number' name='cant_sinabrir' id="sinabrir" value='{{ $cons->cant_sinabrir }}' disabled style="text-align: center;width: 3em;" min="0" />
</td>
<td style="text-align: center">
<input type='number' name='cant_enuso' id="enuso" value='{{ $cons->cant_enuso }}' disabled style="text-align: center;width: 3em;" min="0" />
</td>
<td style="text-align: center">
<input type='number' name='cant_total' id='total' value='{{ $cons->cant_total }}' disabled disabled style="text-align: center;width: 3em;" min="0" />
</td>
</tr>
<script type="text/javascript">
$(document).ready(function(){
$('#enablebutton').click(function(){
$('#sinabrir').removeAttr('disabled').focus();
$('#enuso').removeAttr('disabled').focus();
});
});
</script>
#include('inventlab.consumibles.modal')
#endforeach
</tbody>
</table>
</div>
#endsection
Controller
My index controller is ok, but I don't know how to make the update function run through all the records in the database and save the new values.
public function index(Request $request)
{
if ($request){
$query=trim($request->get('searchText'));
$consumibles=DB::table('consumibles')
->where('nom_cons','LIKE','%'.$query.'%')
->orwhere('alerta','LIKE','%'.$query.'%')
->orderBy('nom_cons','asc')
->get();
return view('inventlab.consumibles.index',["consumibles"=>$consumibles,"searchText"=>$query]);
}
}//End function index
public function update(ConsumibleFormRequest $request)
{
$data = $request->all();
$validate = $this->validateRegister($data);
if($validate['code'] == 200)
{
$register = $validate['data'];
$holder = Consumible::find($id);
$holder->update($register);
return Redirect::to('inventlab/consumibles');
}
else
{
return("No se pudo actualizar el registro");
}
return "Registro actualizado";
}//End function update
My current update function works when I have a different view and edit one per one record.

Jquery remove class not working on table rows

I posted another question for issue and it was partially solved. I have a table with a single link on each row. The link calls AJAX and outputs in another Bootstrap column. This works fine. When I click the link, the class "highlight" is applied which changes the background color of the row. When I click another link I want the previously clicked clink to have the "highlight" class removed and the new row to have the class applied.
Currently, the add class works but any other link click get the class added but the class is never removed.
<table class="table table-hover unpadded" id="mashed_tab">
<thead>
<tr>
<th class="artcl_hdr text-center" colspan="2">
<p class="big_date top_pad"><i class="fa fa-rss fa-lg concrete"></i> Articles/Keywords for
2016-07-09 - 2016-07-10</p>
</th>
<th><span class="badge bkgcol-wet-asphalt">55</span></th>
</tr>
</thead>
<tbody class="mashed_body">
<tr class="mashed_row">
<td class="bkgcol-sunflower wht-border">
<div class="date_cell">
<span class="asphalt long_date">Sat</span><span class="white day">Jul 09, 2016</span>
</div>
</td>
<td class="linked-title dark unpadded">
<div class="cell_link">
link 1
</div>
</td>
<td class="small-cell">
<i class="fa fa-arrow-right"></i>
</td></tr><tr class="mashed_row">
<td class="bkgcol-sunflower wht-border">
<div class="date_cell">
<span class="asphalt long_date">Sat</span>
<span class="white day">Jul 09, 2016</span>
</div>
</td>
<td class="linked-title dark unpadded">
<div class="cell_link">
Link 2
</div>
</td>
<td class="small-cell">
<i class="fa fa-arrow-right"></i>
</td>
</tr>
<tr class="mashed_row">
<td class="bkgcol-sunflower wht-border">
<div class="date_cell">
<span class="asphalt long_date">Sat</span>
<span class="white day">Jul 09, 2016</span>
</div>
</td>
<td class="linked-title dark unpadded">
<div class="cell_link">
Link 3
</div>
</td>
<td class="small-cell">
<i class="fa fa-arrow-right"></i>
</td>
</tr>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function() {
attachEvents();
});
function attachEvents() {
var selClass = 'highlight';
$('#mashed_tab tr a').click(function() {
$(this).parent().parent().removeClass(selClass);
$(this).parent().parent().addClass(selClass);
});
};
</script>
Try something like this in your on click function and let me know.
$('tr a').click(function() {
$("#mashed_tab tr").find("." + selClass).removeClass(selClass);
$(this).parent().parent().addClass(selClass); });

Categories