Using DataTables in Laravel without aJax - No data available in table - php

I have a regular Laravel view that renders a table. When I try to use Datatables on it, it does render the table but it seems to want to format it before the table is actually rendered. I'm confused because, by the time the view is rendered, the server-side work is already done. The only thing it's doing is to render the table with the object passed from the controller.
<table id="stockmovement" class="table table-hover" style="width:100%">
<tbody>
<thead>
<th>Material</th>
<th>Tipo</th>
<th>Quantidade</th>
<th>Valor total</th>
<th>Data</th>
<th>Ordem</th>
<th colspan="3"></th>
</thead>
#foreach($ordercontents as $ordercontent)
<tr>
<td>{{ $ordercontent->material_name }}</td>
<td>{{ $ordercontent->type }}</td>
<td>{{ $ordercontent->oc_weight }}</td>
<td>{{ number_format($ordercontent->oc_weight * $ordercontent->material_price, 2) }}</td>
<td>{{ $ordercontent->order_date }}</td>
<td>{{ $ordercontent->order_name }}</td>
</tr>
#endforeach
</tbody>
</table>
$(document).ready(function() {
$('#stockmovement').DataTable();
})
This is what I end up with:
I don't want to use AJAX to form my table. I've used plenty of vanilla PHP and DataTables instances before where it works just fine. I would appreciate any pointers on what I may be missing.

You have placed <tbody> in the wrong place. <tbody> should use under the </thead>.
Here is the code example:
<table id="stockmovement" class="table table-hover" style="width:100%">
<thead>
<th>Material</th>
<th>Tipo</th>
<th>Quantidade</th>
<th>Valor total</th>
<th>Data</th>
<th>Ordem</th>
</thead>
<tbody>
#foreach($ordercontents as $ordercontent)
<tr>
<td>{{ $ordercontent->material_name }}</td>
<td>{{ $ordercontent->type }}</td>
<td>{{ $ordercontent->oc_weight }}</td>
<td>{{ number_format($ordercontent->oc_weight * $ordercontent->material_price, 2) }}</td>
<td>{{ $ordercontent->order_date }}</td>
<td>{{ $ordercontent->order_name }}</td>
</tr>
#endforeach
</tbody>
</table>

Add tr tag inside the thead tag.
The foreach loop should be in the opening and closing tbody tag.
It should look like this:
<table id="stockmovement" class="table table-hover" style="width:100%">
<thead>
<tr>
<th>Material</th>
<th>Tipo</th>
<th>Quantidade</th>
<th>Valor total</th>
<th>Data</th>
<th>Ordem</th>
</tr>
</thead>
<tbody>
#foreach($ordercontents as $ordercontent)
<tr>
<td>{{ $ordercontent->material_name }}</td>
<td>{{ $ordercontent->type }}</td>
<td>{{ $ordercontent->oc_weight }}</td>
<td>{{ number_format($ordercontent->oc_weight * $ordercontent->material_price, 2) }}</td>
<td>{{ $ordercontent->order_date }}</td>
<td>{{ $ordercontent->order_name }}</td>
</tr>
#endforeach
</tbody>
</table>

Related

How to group rows that have same Group IS as a foreign key?

I just trying to make a group of students who have the same group ID and calculate the CGPA of only those students who have belong to the same Group.
Refrence Example
Here's is the code for Controller
public function View_GroupStudent()
{
$allot_app = AllotmentApps::select(
"AllotmentApps.grpID",
"AllotmentApps.sname",
"AllotmentApps.cgpa"
)
->orderBy('grpID')
->get();
return view('deny', compact('allot_app'));
}
Views:
<table class="table table-bordered">
<tr>
<th>Group Id</th>
<th>Student Name</th>
<th>Individuals CGPA</th>
<th>Group CGPA</th>
</tr>
<tr>
#foreach ($allot_app as $allot_app)
<td>{{ $allot_app->grpID }}</td>
<td>{{ $allot_app->sname }}</td>
<td>{{ $allot_app->cgpa }}</td>
<td>{{ $allot_app->sum('cgpa') }}</td>
</tr>
#endforeach
</table>
Also help me in creating a proper view for that.
I was confused by your image because the red numbers weren't the averages at all. You should be able to achieve what you're trying to do with the following:
<table class="table table-bordered">
<tr>
<th>Group Id</th>
<th>Student Name</th>
<th>Individuals CGPA</th>
<th>Group CGPA</th>
</tr>
#foreach ($allot_app->groupBy('grpID') as $grouped)
#foreach ($grouped as $allot_app)
<tr>
<td>{{ $allot_app->grpID }}</td>
<td>{{ $allot_app->sname }}</td>
<td>{{ $allot_app->cgpa }}</td>
<td>{{ number_format($grouped->avg('cgpa'), 2) }}</td>
</tr>
#endforeach
#endforeach
</table>

How to create dynamic rowspan in table in laravel

I am newbie in laravel, i'm trying to show the table like image below
https://i.stack.imgur.com/l52Vo.jpg
#foreach($report_data as $index => $item)
<tbody class="table-content">
<tr>
<td>{{ $index+1 }}</td>
<td style="text-align:left;">{{ $item->customer_fullName }}</td>
<td>{{ $item->customer_phone }}</td>
<td>{{ $item->customer_detail_licensePlate }}</td>
<td>{{ $item->vehicle_brand_name }}</td>
<td>{{ $item->vehicle_model_name }}</td>
</tr>
</tbody>
#endforeach
but the output is like image below
https://i.stack.imgur.com/rSktI.png
Thanks!!
I would do it as so:
In the controller, select the customers eager loading the vehicles.
$customers = Customer::with('vehicles')->get();
return view('customers')->with('customers', $customers);
In the view:
<table>
#foreach($customers as $index => $customer)
<tr>
<td rowspan="$customer->vehicles->count()">
{{ $index+1 }}
</td>
<td rowspan="$customer->vehicles->count()">
{{ $customer->fullName }}
</td>
<td rowspan="$customer->vehicles->count()">
{{ $customer->phone }}
</td>
<td> {{$customer->vehicles[0]->licensePlate }} </td>
<td> {{$customer->vehicles[0]->brandName }} </td>
<td> {{$customer->vehicles[0]->modelName }} </td>
</tr>
#for($i=1;$i<$customer->vehicles->count())
<tr>
<td> {{$customer->vehicles[$i]->licensePlate }} </td>
<td> {{$customer->vehicles[$i]->brandName }} </td>
<td> {{$customer->vehicles[$i]->modelName }} </td>
</tr>
#endfor
#endforeach
</table>
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Customer</th>
<th scope="col">Licence Number</th>
<th scope="col">Brand</th>
<th scope="col">Brand Type</th>
</tr>
</thead>
<tbody>
#foreach($report_data as $index => $item)
<tbody class="table-content">
<tr>
<td>{{ $index+1 }}</td>
<td style="text-align:left;">{{ $item->customer_fullName }}</td>
<td>{{ $item->customer_phone }}</td>
<td>{{ $item->customer_detail_licensePlate }}</td>
<td>{{ $item->vehicle_brand_name }}</td>
<td>{{ $item->vehicle_model_name }}</td>
</tr>
</tbody>
#endforeach
</tbody>
use this CDN for Bootstrap style
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
You need not to iterate tbody everytime, just iterate <tr></tr> as below
<table>
<thead>
<tr>
<th>No</th>
<th>Customer</th>
<th>Phone</th>
<th>License Number</th>
<th>Brand</th>
<th>Brand Type</th>
</tr>
</thead>
<tbody class="table-content">
#foreach($report_data as $index => $item)
<tr>
<td>{{ $index+1 }}</td>
<td rowspan="2" style="text-align:left;">{{ $item->customer_fullName }}</td> //rowspan should be in some condition
<td>{{ $item->customer_phone }}</td>
<td>{{ $item->customer_detail_licensePlate }}</td>
<td>{{ $item->vehicle_brand_name }}</td>
<td>{{ $item->vehicle_model_name }}</td>
</tr>
#endforeach
</tbody>
</table>

Laravel pagination problem one by one pages

I'm sorry, I have a problem with my laravel pagination Gives me one by one page for example, in my table 16 row I make paginate (10), the pagination gives me in the first page from 1 to 10 and in the second page from 1 to 6 I want the normal pagination from 1 to 10 and from 11 to 16 any help, please
public function allappointmnts(){
$allapo=DB::table('bookappoitments')->orderBy('times.id')
->join('users','bookappoitments.users_id','users.id')
->join('times','bookappoitments.times_id','times.id')
->join('dates','bookappoitments.Dates_id','dates.id')
->paginate(10);
return view('admin.Managers.allappoinments',compact('allapo'));
}
in the blade page:
<div class="text-center">
{!! $allapo->links(); !!}
</div>
The table :
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>#</th>
<th>Date</th>
<th>Time</th>
<th>Employee</th>
<th>Name</th>
<th>Email</th>
<th>Company</th>
<th>Phone</th>
<th>Location</th>
<th>Remarks</th>
<th style="text-align: center;">Action</th>
</tr>
</thead>
<tbody>
<tr>
#foreach($allapo as $Appointments)
<td>{{ $loop->index+1 }}</td>
<td>{{ $Appointments->Dates }}</td>
<td>{{ $Appointments->from_to }}</td>
<td>{{ $Appointments->name }}</td>
<td>{{ $Appointments->gustname }}</td>
<td>{{ $Appointments->gustemail }}</td>
<td>{{ $Appointments->gustcompany }}</td>
<td>{{ $Appointments->gustphone }}</td>
<td>{{ $Appointments->Location }}</td>
<td>{{ $Appointments->Remarks }}</td>
<td>
<a class="btn btn-success btn-mini deleteRecord " href="{{url('showbymanagment',$Appointments->id)}}">Show</a>
</td>
</tr>
#endforeach
Your problem is that you're using the current index of each loop - which will always be within the range of 1-10 of each page (since you have 10 elements per page).
You need to get the current page you're on using
$allapo->currentPage()
Then get the number of elements per page you got, using
$allapo->perPage()
Multiply these two, and it will be the base number for your pagination, meaning that its this number you should increment from.
The indexed table should then be
{{ $allapo->currentPage() * $allapo->perPage() + $loop->index }}
instead of
{{ $loop->index+1 }}
Then, to fix the heading-numbers (10 and 6 at the top), get the total number of results instead of the count of the page using
$allapo->total()
See the Laravel documentation for more details.
Not entirely sure, but you will need to write some sort of function to fix this.
Essentially you need to do this:
Get the page number (ex: 1)
Multiply it by the rows per page (ex: 10)
Add the iteration number
Subtract the per page number
function correct_pagination_numbers($cp, $pp, $counter)
{
$c = (($pp * $cp) + $counter) - $pp;
return $c;
}
Then you can call the function in your view like so:
<td>{{ correct_pagination_numbers($allapo->currentPage(), $allapo->perPage(), $loop->index) }}</td>
It would probably be the best solution to do this in a helper file. If you don't know how to create a helper file and autoload it, you can look here.
Try this code:
<?php $i = $lists->perPage() * ($lists->currentPage() - 1); ?>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>#</th>
<th>Date</th>
<th>Time</th>
<th>Employee</th>
<th>Name</th>
<th>Email</th>
<th>Company</th>
<th>Phone</th>
<th>Location</th>
<th>Remarks</th>
<th style="text-align: center;">Action</th>
</tr>
</thead>
<tbody>
<tr>
#foreach($allapo as $Appointments)
<td><?php $i++; ?>{{ $i }} </td>
<td>{{ $Appointments->Dates }}</td>
<td>{{ $Appointments->from_to }}</td>
<td>{{ $Appointments->name }}</td>
<td>{{ $Appointments->gustname }}</td>
<td>{{ $Appointments->gustemail }}</td>
<td>{{ $Appointments->gustcompany }}</td>
<td>{{ $Appointments->gustphone }}</td>
<td>{{ $Appointments->Location }}</td>
<td>{{ $Appointments->Remarks }}</td>
<td><a class="btn btn-success btn-mini deleteRecord " href="
{{url('showbymanagment',$Appointments->id)}}">Show</a></td>
</tr>
#endforeach
Try ths
#foreach($allapo as $key => $Appointments)
{{ $key + $allapo->firstItem() }}
#endforeach
to see the correct number change the line
<td>{{ $loop->index+1 }}</td>
to:
<td>{{ ($allapo->currentPage() - 1) * $allapo->perPage() + $loop->iteration }} <td>

Link of anchor tag is also printing in Laravel Blade

I have a table of employee's salary list. I wrap the employee id with an anchor tag to show salary history of each individual employees. The problem is when I print the table the link for individual salary history is also printed under the employee id.
Here is a screen shot print preview:
Blade
<div class="d-section col-md-12">
<table class="table table-striped table-condensed table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Position</th>
<th>Start Date</th>
<th>End Date</th>
<th>Worked</th>
<th>Basic</th>
<th>OT Rate</th>
<th>OT Hour</th>
<th>OT Amount</th>
<th>Basic Amount</th>
<th>Total</th>
<th>Signature</th>
<th>Finger</th>
</tr>
</thead>
<tbody>
#foreach($employees as $employee)
<tr>
<td>{{ $employee->eid }}</td>
<td>{{ $employee->name }}</td>
<td>{{ $employee->designation }}</td>
<td>{{ $employee->start }}</td>
<td>{{ $employee->end }}</td>
<td>{{ $employee->worked }}</td>
<td>{{ $employee->basic }}</td>
<td>{{ $employee->ot_rate }}</td>
<td>{{ $employee->ot_hour }}</td>
<td>{{ $employee->ot_amount }}</td>
<td>{{ $employee->basic_amount }}</td>
<td>{{ $employee->ot_amount + $employee->basic_amount }}</td>
<td></td>
<td>
{!! Form::open(['action'=>['SalaryController#destroy',$employee->id],'class'=>'no-print','method'=>'delete','onsubmit'=>'return deleteConfirm()']) !!}
{!! Form::submit('X',['class'=>'element btnn btn-danger']) !!}
<br/>
<span class="glyphicon glyphicon-edit"></span>
<br/>
<span class="glyphicon glyphicon-usd"></span>
{!! Form::close() !!}
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
Why not just create a CSS class for your anchors and hide them using that class?
foo
foo
And in your CSS:
.only-print{
display:none;
}
#media print{
a.hiddenTab {
display:none;
}
.only-print{
display:block;
}
}
All the anchors you'd want to hide would just use class="hiddenTab".
If you want to hide all a tags which have href set, you can do this:
a[href] { display: none; }

Custom query using DB::select() on laravel 4.2 controller

I had used a custom query in my controller in laravel here is my code in my controller:
public function c_viewSystemUser()
{
$title = "View System Users";
$jSu = DB::select(DB::raw("SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID"));
return View::make('ssims.view_systemUser',compact('title','jSu'));
}
I wanted to display its result in my blade file view_systemUser
here is my code inside the view:
#if ($jSu)
<table class="table table-striped table-hover" id="detailTable">
<thead>
<tr>
<th>System User ID</th>
<th>SystemUser Type ID</th>
<th>System UserName</th>
<th>System User Description</th>
</tr>
</thead>
<tbody>
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->dbo_systemusers.SystemUserID }}</td>
<td>{{ $vu->dbo_systemtypes.SystemType }}</td>
<td>{{ $vu->dbo_systemusers.SystemUserName}}</td>
<td>{{ $vu->dbo_systemusers.SystemUserDescription}}</td>
</tr>
#endforeach
</tbody>
</table>
And I am having an error:
Undefined property: stdClass::$dbo_systemusers (View:
C:\xampp\htdocs\laravel3\app\views\ssims\view_systemUser.blade.php)
Any suggestions?
Try this:
#foreach ($jSu as $key => $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach
You forgot to join dbo_systemusers with dbo_systemtypes.
Try this code
SELECT dbo_systemusers.SystemUserID,dbo_systemtypes.SystemType, dbo_systemusers.SystemUserName from dbo_systemusers INNER JOIN dbo_systemtypes ON dbo_systemusers.SystemUserTypeID = dbo_systemtypes.SystemUserTypeID
And Try this on View page
#foreach ($jSu as $vu)
<tr>
<td>{{ $vu->SystemUserID }}</td>
<td>{{ $vu->SystemType }}</td>
<td>{{ $vu->SystemUserName}}</td>
<td>{{ $vu->SystemUserDescription}}</td>
</tr>
#endforeach

Categories