动态嵌套列? [英] Dynamic nested columns?
问题描述
我正在尝试构建一个动态表,其中可以在单击时拆分列。例如,如果单击第1层中第1列上的拆分,则第1列将具有两个子列=第二层。当您在第1层中再次单击拆分时,将在第2层中添加一列,依此类推。但是,现在我不知道如何确定正确的colspan,具体取决于子列数最多的列设置的最大值。
代码在这里,我只是从数据库加载数据。数据从模型传递到创建表格的视图。
我尝试过:
到目前为止,这就是我所拥有的:
I am trying to build a dynamic table wherein the columns can be split upon a click. For example, if you click on Split on column 1 in layer 1, column 1 will have two child columns = 2nd layer. When you click on split again in layer 1, one column will be added in layer 2, so on and so forth. However, now I am stuck with how to determine the right colspan depending on the maximum value set by the column with the highest number of child columns.
In the block of code here, I am simply loading data from the database. Data is passed from a model to the view where the table is created.
What I have tried:
So far this is what I have:
<table border=1>
<tr>
<?php
$parentcols = array();
$maxcolspan = 0;
//loop through parent columns in layer 1
foreach($parentcoldata as $parentkey)
{
$children = array();
foreach($childcoldata as $childcol)
{
//check if the current child col is a child of the current parent col
if ($childcol->parentcol_id == $parentkey->id)
{
$curchild = array("childname" => $childcol->name, "layer_id" => $childcol->layer_id);
$children[$childcol->id] = $curchild;
}
}
//know number of children of current parent column
$childnum = count($children);
if ($childnum == 0) {unset($children); echo "Unset";}
//set colspan based on column with the highest number of children
if ($childnum > $maxcolspan)
{
$maxcolspan = $childnum;
}
//save the details about the parent column
$parent = array("name" => $parentkey->name,
"child" => $childnum,
"children" => $children,
"id" => $parentkey->id
);
}
//print row for parent columns - 1st layer
for ($i=1; $i<=count($parentcols); $i++) {
echo '<th id="parentcol'.$parentcols[$i]['id'].'" colspan='.$parentcols[$i]['child'].' style="padding: 5px">'.$parentcols[$i]['name'].'<br />'.
'<a href="">Edit</a> <br />'.
'<a href="">Delete</a> <br />'.
'<a href="">Split</a>'.
'</th>';
}
?>
</tr>
<?php
//this is where i attempt to print the child columns
for ($index = 1; $index <= $maxcolspan-1; $index++)
{
echo "<tr>";
foreach($parentcols as $parentcolid => $value) //$parentcolid = key/id
{
//loop through children columns
foreach($value['children'] as $children => $child)
{
if ($child['layer_id'] == $index){
echo '<td id="'.$children.'" style="padding:5px">'.$child['childname'].
'<a href="">Edit</a> <br />'.
'<a href="">Delete</a> <br />'.
'<a href="" parent-id="someparentid">Split</a>'
.'</td>';
}
}
}
echo "</tr>";
}
?>
</table>
但是使用此代码,第二层中子列的子列(第3层)不再包含在内。
我卡住了,不知道我是什么我不见了。
表结构:
列 [ ^ ]
推荐答案
parentcols = array();
parentcols = array();
maxcolspan = 0 ;
// 遍历第1层中的父列
foreach (
maxcolspan = 0; //loop through parent columns in layer 1 foreach(
parentcoldata as
parentcoldata as
这篇关于动态嵌套列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!