Bootstrap Nested Table
Table Template
The first thing is to create table like template which has similar table structure. And each line has two containers, "main" container for certain columns and "sub" for nested table.
<div class="table"> <div class="header"> <div class="line"> headers... </div> </div> <div class="body"> <div class="line"> <div class="main"> columns... </div> <div class="sub"> <div class="table"> ... </div> </div> </div> </div> </div>
CSS
The major problem of the <div> table approach is, the column elements cannot auto align and adjust their widths. So a width value must be manually set to each element, which can be a fixed value or precentage value. For fixed value, "overflow:auto" must be set in case of the total width exceed its parent element. For precentage value, it's better to have to total value to be 100%. And also, an indent value needs to be set. It's better but not necessary to be the width of the first column.And then, to make to be consistent with Bootstrap table style and configurable for further table styles. The following styles are needed. I've also add some styles to make it to be fitting to the basic Bootstrap table style.
.header>.line>div:nth-child(1), .body>.line>.main>div:nth-child(1){ width:5%; } .header>.line>div:nth-child(2), .body>.line>.main>div:nth-child(2){ width:15%; } .header>.line>div:nth-child(3), .body>.line>.main>div:nth-child(3){ width:15%; } .header>.line>div:nth-child(4), .body>.line>.main>div:nth-child(4){ width:45%; } .header>.line>div:nth-child(5), .body>.line>.main>div:nth-child(5){ width:10%; } .header>.line>div:nth-child(6), .body>.line>.main>div:nth-child(6){ width:10%; } .sub{ padding-left:5%; } .body>.line a{ color:#222; font-size:12px; } .header{ font-weight:bold; } .header>.line{ border-bottom:solid 2px #AAA; } .header>.line>div, .body>.line>.main>div{ float:left; padding:8px; } .header>.line:before, .body>.line>.main:before, .header>.line:after, .body>.line>.main:after{ content:' '; display:table; } .header>.line:after, .body>.line>.main:after{ clear:both; } .table>.body>.line:not(:first-child)>.main{ border-top:solid 1px #AAA; } .table>.body>.line.open>.main{ border-bottom:solid 1px #AAA; }
Javascript
Here it needs a little javascript to expand and unexpand nested tables. In real practice, it will also need to define whether the button shows or hides.
function togglecollapse(e){ e.preventDefault(); $(e.currentTarget.parentNode.parentNode.parentNode).toggleClass('open'); }
Next time I will write an article to demonstrate how I implement this template with some MVC approach.
没有评论:
发表评论