Create HTML table with headers in a vertical column dynamically with laravel? - php

I've been hunting around, but all the answers and documentation on this is for tables that aren't dynamically generating everything.
This is my blade file:
#extends('layout')
#section('content')
<h1>User Profile Data</h1>
<table class="table table-striped">
<tr>
#foreach ($columns as $column => $name)
<th><strong> {{ $name }} </strong></th>
#endforeach
#foreach ($profile as $person)
#foreach ($person as $name)
<td> {{ $name }} </td>
#endforeach
#endforeach
</tr>
</table>
#stop
It's all working, except for one problem, the heads go horizontally, rather than vertically. I'd like to be able to automatically populate the data from the database and have it displayed side by side.
Like so:
Name: John
Age: 25
Height: 176
etc.
What am I missing?

v.1 Directly generate the vertical headers table.
This can be easily solved after you realize how your table will actually look like in HTML, in order to have vertical headers.
<h1>User Profile Data</h1>
<table class="table table-striped">
<tr>
<th><strong> Name: </strong></th>
<td> John </td>
<td> Joane </td>
</tr>
<tr>
<th><strong> Surname: </strong></th>
<td> Doe </td>
<td> Donald </td>
</tr>
<tr>
<th><strong> Email: </strong></th>
<td> john.doe#email.com </td>
<td> jane#email.com </td>
</tr>
</table>
After you know your above goal you just have to nest the foreach loops so that you can populate the data.
For each column you'll have to return the matching data from the profile.
Instead of returning $person->name use $person[$col]
To nest the foreach use the same technique for your other question:
How to show data from (multidimensional?) arrays with Laravel 5.3 blade
v.2 Use normal tables combined with CSS tricks to reflow table
Import twitter bootstrap and use .table-reflow on a well formed table.
The generated table will look like this and can be generated with your initial laravel code:
<table class="table table-striped table-hover table-reflow">
<thead>
<tr>
<th ><strong> Name: </strong></th>
<th ><strong> Surname: </strong></th>
<th ><strong> Email: </strong></th>
</tr>
</thead>
<tbody>
<tr>
<td> John </td>
<td> Doe </td>
<td> john.doe#email.com </td>
</tr>
<tr>
<td> Joane </td>
<td> Donald </td>
<td> jane#email.com </td>
</tr>
</tbody>
</table>
Content order and complex tables:
Beware that the table-reflow style changes the visual order of content. Make sure that you only apply this style to well-formed and simple data tables (and in particular, don’t use this for layout tables) with appropriate <th> table header cells for each row and column.
In addition, this class will not work correctly for tables with cells that span multiple rows or columns (using rowspan or colspan attributes).
You should check the docs here:
twitter-bootstrap .table-reflow
Edit:
In addition to Kronmark's excellent points, I'm leaving this here for new people. This is what the automated table might look like:
<table class="table table-striped table-hover table-reflow">
#foreach($array as $key=>$value)
<tr>
<th ><strong> {{ $key }}: </strong></th>
<td> {{ $value }} </td>
</tr>
#endforeach
</table>
The key is the title from the table, the value is the data point associated with it. I.e. Full_Name = Key, John Magsson = value.
This is produced from an array, the process should be explained here:
Blade view not printing data from array
Good luck. :)

Related

Iterating through a column in blade.php file

I'm trying to figure how to iterate through this table which shows monthly records provided by different facilities within any one month.
As it is, every record from each facility has a month associated with it, but I want to make it so that there only one month record, followed by the next month displaying in the same way and so on.
I've been told to use an if statement to iterate through the month column to determine the number of rows one month record should cover, but I'm confused regarding the syntax, any suggestions?
This is the table code I'm working with atm:
<table id="table2" class="table table-bordered table-striped table-condensed table-hover display" >
{{-- Beginning of headers --}}
<thead>
<tr class="text-center">
<th>Month</th>
<th>Grade</th>
<th colspan="3">Volume (m<sup>3</sup>)</th>
</tr>
<tr class="text-center">
<th></th>
<th></th>
<th>Received</th>
<th>Milled</th>
<th>Balance</th>
</tr>
</thead>
{{-- End of headers --}}
{{-- Beginning of data --}}
#foreach ($grades1 as $g)
<tbody class="text-center">
<tr class="text-center">
<td rowspan="100">{{$g->yr_mm}}</td>
<tr>
<td>{{$g->grade}}</td>
<td>{{$g->vol_received}}</td>
<td>{{$g->vol_milled}}</td>
<td>{{$g->vol_balance}}</td>
</tr>
</tr>
</tbody>
#endforeach
{{-- End of data --}}
</table>

Laravel: get checkbox value from html table

I have a blade page with a html table where any row has a checkbox, all these rows have a key value. I need all these checkbox values and update a table. At the moment I have this solution, but it seems not the best choice.
<table class="table table-sm table-striped table-bordered" style="width: 100%;">
<thead class="thead-light">
<tr>
<th >ARRIVED</th>
<th >Cron.</th>
<th >NAME</th>
</tr>
</thead>
<tbody>
{{$counter=1}}
#foreach($data as $row)
<tr>
<td><input type="checkbox" name="arrived_yes_no_{{$counter}}"></td>
<td>{{$row->Crono}}</td>
<td>{{$row->Name}}</td>
</tr>
{{$counter++}}
#endforeach
</tbody>
</table>
I will rename any checkbox with the _$counter suffix so when I send via post all the page content I can (I hope) retrive all the checkbox values.
Is there a better option?
#Virginia has the right idea. Changing the checkbox names to array syntax keyed by the ID of the row will make it much easier to work with server side.
// checkbox names
name="arrived_yes_no[{{ $row->id }}]"

Request Datas from 2 sites in bootstrap Datatable

I have a bootstrap datatable-basic with a few pages.
In the first cell of the table is a checkbox.
The table looks like that just with a few more rows.
<table class="table datatable-basic table-hover" >
<thead>
<tr>
<th>Status</th>
<th>Keyword</th>
<th>Suchvolumen</th>
</tr>
</thead>
<tbody>
<tr>
<td> <input type="checkbox" id="checkboxManuellerStatus1" name="checkboxManuellerStatus[1]" value="abc"> abc</td>
<td class="keywordDataStyle">abc</td>
<td>abc</td>
</tr>
<tr>
<td> <input type="checkbox" id="checkboxManuellerStatus2" name="checkboxManuellerStatus[2]" value="abc2"> abc</td>
<td class="keywordDataStyle">abc</td>
<td>abc</td>
</tr>
</tbody>
All checkboxes have incrementing names and ids. checkboxManuellerStatus1,checkboxManuellerStatus2,checkboxManuellerStatus3,... and so on.
If I request the checkboxes in php, I get all the values of checked checkboxes.
$array = $_REQUEST['checkboxManuellerStatus'];
Thats good, except that I only get the values of the current page in the table, but I need all values of all pages. How do I do that?
Thanks

Styling table in Laravel

I have a problem with #foreach in my blade.php. When I insert movie with multiple categories my other moves to the right. Here is how it looks in browser
and here is code in blade
#if($movies)
<div class="row">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Categories</th>
<th>Actors</th>
</tr>
</thead>
<tbody>
#foreach($movies as $movie)
<tr>
<td>{{$movie->name}}</td>
#foreach($movie->categories as $category)
<td>{{$category->category_name}}</td>
#endforeach
#foreach($movie->actors as $actor)
<td>{{$actor->actor_name}}</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
#endif
Your issue lies here:
#foreach($movie->categories as $category)
<td>{{$category->category_name}}</td>
#endforeach
You are saying that for each category, it should insert a new <td> into you table. This will of course skew the content since your table expects a set number of columns.
Instead, you should move your foreach statement inside the <td></td>
<td>
#foreach($movie->categories as $category)
{{$category->category_name}}
#endforeach
</td>
This will print the links for each category in the same column.

Can I iterate object rendered by mustache?

I have table generated by mustache loop that looks like this:
names.mustache
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th>
#
</th>
<th>
Name
</th>
</tr>
</thead>
<tbody>
{{#allnames}}
{{#active}}
<tr>
<td>
{{count}}
</td>
<td>
{{name}}
</td>
</tr>
{{/active}}
{{/allnames}}
</tbody>
</table>
And I want to iterate count so that my table can have row numbers. basically, i initialized $count=1. How can I implement that with clean efficient code? or is there a better simpler way?
UPDATED
For Mustache, you need to create a function
var data = {
items: allnames,
, index: function() {
return ++window['index']||(window['index']=0);
}
}
and then use {{index}} inside the loop
originally
Sorry, I was thinking of handlebars which works as below (see correct answer above)
Use {{#index}}
{{#allnames}}
{{name}} is {{#index}}
{{/allnames}}
Note: index starts from zero

Categories