Codeigniter2 - Generate a table that contain multiple sub-table - codeigniter

For the Table class in user guide, it mention how to generate a table with multiple columns/rows. But I want to ask how can we generate a table that contain multiple sub-table, example as below:
<table>
<tr>
<td>
<table>
<tr>
<td>sub-table-01</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>sub-table-02</td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td>sub-table-n</td>
</tr>
</table>
</td>
</tr>
</table>
Could anyone suggest the snippet for this function?
Thanks

You can nest the calls to generate(). Example:
$data1 = array(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9),
);
$data2 = array(
array('a', 'b'),
array('c', 'd'),
array('e', 'f'),
);
$data3 = array(
array('Heading1', 'Heading 2', 'Heading 3'),
array('Row1', $this->table->generate($data1), $this->table->generate($data2)),
array('Row2', $this->table->generate($data1), $this->table->generate($data2)),
array('Row3', $this->table->generate($data1), $this->table->generate($data2)),
);
echo $this->table->generate($data3);
You'll have to replace this with your own data in a way that makes sense of course, but hopefully this gives you the idea.

Related

not able to show array in Table #foreach

I am new to Laravel and coding. Trying to show Month name (Array) in table but not able to do the same. I tried few things like "json_encode" but got error each time.
My controller code is
$months = array();
for ($i = 0; $i < 12; $i++) {
$timestamp = mktime(0, 0, 0, date('n') - $i, 1);
$months[date('n', $timestamp)] = date('M-Y', $timestamp);
}
Output is
array:12 [▼
3 => "Mar-2022"
2 => "Feb-2022"
1 => "Jan-2022"
12 => "Dec-2021"
11 => "Nov-2021"
10 => "Oct-2021"
9 => "Sep-2021"
8 => "Aug-2021"
7 => "Jul-2021"
6 => "Jun-2021"
5 => "May-2021"
4 => "Apr-2021"
]
View file
<table id="incometable" class="table m-0 table table-bordered table-hover table" data-order='[[ 0, "desc" ]]'>
<thead>
<tr>
<th align="center">Month</th>
<th>Milk Sale Amount (Rs.)</th>
<th>Animal Sale Amount (Rs.)</th>
<th>Other Sale Amount (Rs.)</th>
</tr>
</thead>
<tbody>
#foreach ($months as $item )
<tr>
<td>{{ $item->$months }}</td>
</tr>
#endforeach
</tbody>
</table>
Thanks in Advance
During your #foreach() loop, $item is not an Object, it is a String 'Mar-2022' through 'Apr-2021'. You need to adjust your code:
#foreach($months as $key => $label)
<tr>
<td>{{ $label }}</td>
<tr>
#endforeach
If you need the 1 through 12 values, they are available in each iteration as $key.

Laravel : "Object of class stdClass could not be converted to string" while pass variable into closure

I get an error like this
ErrorException
This my link index
<td>{{$cmface}}</td>
This my Routes
Route::get('/detail_face_to_face/{id}', 'FaceToFaceController#detail')->name('detail_face_to_face');
This my Controller
public function detail($id)
{
$xclass = DB::table('face_to_face')->select('Class')->where('id', $id)->first();
$tface = DB::table('tbclass')
->select('tbclass.F_Name','tbclass.Class')
->where('tbclass.Class', $xclass)
->whereNotExists(function ($query) use ($id) {
$query->select('id_user')
->from('absent')
->where([['absent.id_face_to_face', $id],['absent.type_face_to_face', '1'],])
->whereRaw('absent.id_user = tbclass.ID_No');
})
->orderBy('tbclass.F_Name', 'ASC')
->paginate(10);
return view('face_to_face.detail',['tface' => $tface]);
}
This my face_to_face.detail page
<table class="table">
<thead style="white-space:nowrap;">
<tr>
<th>No</th>
<th>Name</th>
<th>Class</th>
</tr>
</thead>
<tbody style="white-space:nowrap;">
#if($tface->count()===0)
<tr>
<td class="table-success text-center" colspan="10"><< Data is Empty >></td>
</tr>
#else
#foreach($tface as $no => $tm)
<tr>
<td>{{ ++$no + ($tface->currentPage()-1) * $tface->perPage() }}</td>
<td>{{$tm->F_Name}}</td>
<td>{{$tm->Class}}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
{{ $tface->links() }}
if I click the link on the index page, i get that error
Can anyone help ???
Could you dd($xclass)?
Error is in, ->where('tbclass.Class', $xclass). You probably do ->where('tbclass.Class', $xclass->Class)
Regard

validation using required_with in laravel for input filed as array?

i have a table with checkbox as array and textbox as also array. what i want to achieve is when the user checked a checkbox it should validate that input textbox are not be empty.
public function roombooking(Request $request)
{
$messsages = array(
'check.required'=>'No room was selected.Please select room to proceed for booking !',
'txtnos.required|numeric'=>'Please enter no of persons',
);
$rules = array(
'check'=>'required',
'txtnos'=>'required_with:data', //txtnos is a array input filed and data is a array checkbox
);
$validator = Validator::make($request->all(), $rules,$messsages
);
if ($validator->fails()) {
return Redirect::back()
->withErrors($validator)
->withinput();
}
}
Html code
<table class="table table-hover" data-toggle="table" id="table"
data-click-to-select="true">
<thead>
<tr>
<th style="width:10%;" data-field="ActivityId">Select</th>
<th style="width:30%;" data-field="ActivityName">Activity Name</th>
<th style="width:30%;" data-field="Rate">Rate/Person</th>
<th style="width:30%;">Nos. of person</th>
</tr>
</thead>
<tbody>
#foreach($loadactivity as $key=>$activity)
<tr>
<td>
<input type="checkbox" name="data[]" value="0;{!! $activity->ActivityId !!};{!! $activity->Rate !!};0;0;{!! $activity->ActivityName !!}" />
</td>
<td>{!! $activity->ActivityName !!}</td>
<td>{!! $activity->Rate !!}</td>
<td >{!! Form::text('txtnos[]','',['class' => 'form-control small-textbox ','txtnoid'=>$activity->ActivityId]) !!}</td>
</tr>
#endforeach
</tbody>
</table>
please help me
Change your form:
...
<td><input type="checkbox" name="row[{{$key}}][data]" value="0;{!! $activity->ActivityId !!};{!! $activity->Rate !!};0;0;{!! $activity->ActivityName !!}" />
</td>
<td>{!! $activity->ActivityName !!}</td>
<td>{!! $activity->Rate !!}</td>
<td>{!! Form::text('row[{{$key}}][txtnos]','',['class' => 'form-control small-textbox ','txtnoid'=>$activity->ActivityId]) !!}</td>
...
So the only thing that's changed is the name of data and txtnos, it will give you the following:
$exampleResult = [
'row' => [
// old $key as new key
0 => [
'txtnos' => 'entered value',
'data' => '1', // But only if checked
],
1 => [
'txtnos' => 'entered value',
'data' => '1', // But only if checked
],
]
];
Validation rules
$rules = [
'row.*.txtnos' => 'required_with:row.*.data'
];
In the example, txtnos on each row is required if data on the same row isset.
Validation message
$messages = [
'row.*.txtnos.required_with' => 'Enter a value or uncheck the checkbox..'
];
Important:
The validation for .*. was added in Laravel 5.2, you didn't specify your exact version so I'm not sure if it will work for you. Anyway, there is another way to do this.
For versions < 5.2, loop the input rows and replace the * in my example with the current key.

Laravel5.1, Relation, EloquentORM, Many to Many, Sort

<table border="true">
<tr>
<th style="width:100px">Table</th>
<th colspan="3" style="width:300px">Columns</th>
</tr>
<tr>
<td style="width:100px">Articles</td>
<td style="width:100px">id</td>
<td colspan="2" style="width:200px; color: #aaaaaa">other dependent variables</td>
</tr>
<tr>
<td style="width:100px">Article_Tag</td>
<td style="width:100px">id</td>
<td style="width:100px">article_id</td>
<td style="width:100px">tag_id</td>
</tr>
<tr>
<td style="width:100px">Tags</td>
<td style="width:100px">id</td>
<td style="width:100px">name</td>
<td style="width:100px"></td>
</tr>
</table>
Then, I want to sort Articles table by [name] of Tags table.
I tried eager-loading with closure below, but it did'nt work.
Model Article and Tag are connected with each other just by belongsToMany,
and I succeed in output their data, but they weren't sorted.Offcourse, I did'nt give getTag any argument.(When I gave argument, they weren't narrowed by [name] either.)
use App\Article;
use App\Tag;
...
public function __construct(Article $article)
{
$this->article = $article;
}
...
public function getTag($tag = NULL)
{
$flag = isset($tag);
$articles = $this->article->orderBy('created_at', 'desc')->with([
'tags' => function($query) use ($flag, $tag){
($flag)
? $query->where('name', $tag)
: $query->orderBy('name');
}
])->paginate(15);
Try this variant:
$articles = $this->article->with([
'tags' => function($query) use ($flag, $tag){
return $flag
? $query->where("name", $tag);
: $query->orderBy("name")
}
])->paginate(15);

ASP.NET MVC Razor Headers and Views for Each Column Dynamically

I have the following Razor lines for now:
<table border=1 cellpadding=3 cellspacing=1 rules="rows" frame="box">
<tr>
<th>Türkçe Söz Dizisi</th>
<th>English Word Sequence</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Tr)
</td>
<td>
#Html.DisplayFor(modelItem => item.En)
</td>
<td>
#Html.ActionLink((String)#ViewBag.Edit, "Edit", new { id=item.ID }) |
#Html.ActionLink((String)#ViewBag.Delete, "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
However, I will add some columns automatically from the program to database table. I need a way to print this th's and td's accordingly. In short, I need a way to go through Model's dynamic columns. How can I achieve this?
EDIT: My Model type here is "Proposal". However, I want to reach dynamic attribute of Proposal.Type.Word(Tr, En, or any other added Language Enum). How can I?
#foreach (var item in Model) {
Type objectType = Type.GetType(typeof(RexamOneriSistemi.Models.Word).AssemblyQualifiedName);
System.Reflection.PropertyInfo[] fields = objectType.GetProperties();
<tr>
<td>#Html.DisplayFor(modelItem => item.ID)</td>
<td>#Html.DisplayFor(modelItem => item.Owner.Name)</td>
#foreach (System.Reflection.PropertyInfo f in fields) {
if (f.Name.ToString() == #HttpContext.Current.Session["Language"].ToString())
{
<td>
// What to do here exactly to get item.Type.Word.[dynamic attribute]?
</td>
}
</tr>
}
I can get Razor String
string s = "#Html.Displayfor(modelItem => item.Type.Word." + System.Web.HttpContext.Current.Session["Language"] + ")"
Resulting: #Html.Displayfor(modelItem => item.Type.Word.Tr)
Can I insert a string to be rendered as Razor Syntax? If yes, how?
I have tried this code and it works
<table border=1 cellpadding=3 cellspacing=1 rules="rows" frame="box">
<tr>
#{
Type objectType = Type.GetType(typeof(yourProjectNamespace.Models.Language).AssemblyQualifiedName);
System.Reflection.PropertyInfo[] fields = objectType.GetProperties();
foreach (System.Reflection.PropertyInfo f in fields)
{
<th> #f.Name.ToString() </th>
}
}
</tr>
#foreach (yourModelType item in Model) {
<tr>
foreach (System.Reflection.PropertyInfo f in fields)
{
<td>#Html.Display(f.Name.ToString())</td>
}
<td>
#Html.ActionLink((String)#ViewBag.Edit, "Edit", new { id=item.ID }) |
#Html.ActionLink((String)#ViewBag.Delete, "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
To get Assemply Qualified Name of your class , see this link here
Came across this question, wrote this maybe it will help someone else. This code will dynamically load your model into a table.
#model System.Collections.Generic.List<App.Client.Models.MyModel>
<table>
<thead>
<tr>
#foreach (var property in Model.GetType().GetGenericArguments()[0].GetProperties())
{
<th>#property.Name</th>
}
</tr>
</thead>
<tbody>
#foreach (var modelItem in Model)
{
<tr>
#foreach (var property in modelItem.GetType().GetProperties())
{
<td>#property.GetValue(modelItem)</td>
}
</tr>
}
</tbody>
</table>

Resources