Collapse rows in html table

how can i collapse all table rows

i have table rows inside a table, and also another nested table rows inside each row, my problem is i can collapse and expand in the nested table rows, but when i try to expand in the main table, only the first row is expanded the rest ones are expanded by default when i launch the program, how can i fix it. this is my code

tbody.collapse.in < display: table-row-group; >.tigray < background-color: darkcyan; >.zoba
     
head1 head2
nested title1 13 13
nested title2 18 13
nested title 3 32 13
nested title4 13 13
nested title5 18 13
nested title 6 32 13

Источник

Goal

Make a table where each row is clickable to show a hidden sub-content (left side) and / or to navigate with clickable buttons (right).

alt text

bootstrap collapse component stylish buttons We must therefore integrate bootstrap collapse component and stylish buttons.

Bad Idea with table

Step 1 : table

it is the one bootstrap offers us a simple table Nothing to say is the most logical solution, because it is the one bootstrap offers us a simple table.

  1 Mark Otto @mdo   2 Jacob Thornton @fat  

alt text

Step 2 : table + collapse

bootstrap the doc with only data-target and collapse You must use the bootstrap the doc with only data-target and collapse.

Читайте также:  Создание страниц на php через

Problem: The collapse does not work with a TR tag for target.

Solution: Create a DIV in a sub-TR (optional: add CSS to hide the permanent residual margin/padding of the hidden/collapsed line).

 class="accordion-toggle" data-toggle="collapse" data-target="#collapseOne"> 1 Mark Otto @mdo   colspan="3">  id="collapseOne" class="collapse in"> Details 1 
Details 2
Details 3

alt text

Step 3 : table + collapse + buttons

Problem: a click on a button causes a side effect of expand/collapse the container row. It’s bad when you click for a new tab/window on the details button to displays, because it will activate the accordion effect on the current row (example: click on edit).

Solution: you must prevent the expand/collapse from triggering when a button is clicked. It is therefore necessary to assign the click to expand each cell rather than on the TD TR tags.

It works BUT JavaScript lags on expanding/collapsing when there are dozens of roww and sub-rows on the page and there are duplicate code.

 class="accordion-toggle" >  data-toggle="collapse" data-target="#collapseTwo">1  data-toggle="collapse" data-target="#collapseTwo">Mark  data-toggle="collapse" data-target="#collapseTwo">Otto  data-toggle="collapse" data-target="#collapseTwo">@mdo  class="btn btn-primary" href="https://www.google.com/"> class="icon-search icon-white">   colspan="4">  id="collapseTwo" class="collapse in"> - Details 1 
- Details 2
- Details 3

alt text

Better Idea with div

Step 1 : div

Bootstrap grid system You must use the Bootstrap grid system.

Problem: table style is lost.

Solution: (optional) You must duplicate the CSS if you will get the same rendering as before (top row border, cell width, header bold).

 class="row-fluid" >  class="span2">1  class="span2">Mark  class="span2">Otto  class="span2">@mdo  

alt text

Step 2 : div + collapse

(same solution table and collapse)

Problem: The cursor’s shape does not change when being over the clickable row (“hand” icon expected here).

Solution: (optional) use a personal style or re-use the class accordion-toggle on the collapsable DIV.

 class="row-fluid" data-toggle="collapse" data-target="#collapseTwo">  class="span1">1  class="span3">Mark  class="span3">Otto  class="span3">@mdo   id="collapseTwo" class="row-fluid collapse in">  class="span1">
class="span9"> Details 1
Details 2
Details 3

alt text

Step 3 : div + collapse + buttons

(same solution collapse + table + buttons)

Problem: Adding buttons also add conflicts with the collapse/expand effect.

Solution: remove the button form the DIV toggler.

Solution

 class="row-fluid" >  class="accordion-toggle" data-toggle="collapse" data-target="#collapseThree">  class="span1">1  class="span3">Mark  class="span3">Otto  class="span3">@mdo   class="span1">  type="button" class="btn btn-primary"> class="icon-search icon-white">    id="collapseThree" class="row-fluid collapse in">  class="span1">
class="span9"> Details 1
Details 2
Details 3

Conclusion

I currently use the DIV version. With TABLE, fhe final page was not reactive enough, the source code was duplicated, and there were a lot of bugs/maintenance and technical solution TABLE was not always understandable, even with the doc bootstrap support.

Источник

How to implement expand/collapse in HTML table rows using jQuery

This jQuery tutorial, we will discuss how to implement expand/collapse in an HTML table rows using jQuery. Users can click on the expand button, which will display rows inside that. Similarly, when the user clicks on the collapse button, the HTML table rows will be collapsed.

There are two ways, we can implement expand/collapse in HTML table rows using jQuery.

Implement expand/collapse in HTML table rows using jQuery

A common UI will have an HTML table of data rows. When we click on “Expand”, it shows a detailed breakdown of “child” rows below the “parent” row. In a parent row, click on the “+” sign; it expands the child row with detailed information. At the same time, the parent sign toggles to “- “.

Once we click on “- “sign, then it will collapse child rows with parent sign “+”.

  • Put a class of “parent” on each parent row (tr).
  • Give each parent row (tr) an attribute “data-toggle=”toggle””.
  • Give each child row cover under a >If you want to use the below code in a classic SharePoint page, then you can write the code inside a content editor or script editor web part.

Below is the sample of the HTML table structure.

  

+ Technology

4

Below is the script code to expand/collapse in HTML table rows:

   

The output will appear like below:

expand collapse in HTML table rows using jQuery

table collapse row

Implement expand/collapse in HTML table rows using jQuery

A common UI which will have an HTML table of record rows, in which when we click on “Expand”, it shows a detailed breakdown of “child” rows below the “parent” row.

  • Add a class of “parent” on each parent row (tr).
  • Give each parent row (tr) an id.
  • Give each child row a class of “child-ID” where ID is the id of the parent tr that it belongs to.
  
ID Name Total
123 Bill Gates 100
2018-01-02 A short description of Microsoft revenue 15
2018-02-03 Another New Project description 45
2010-03-04 More New Stuff 40
456 Bill Brasky 50
2009-07-02 A short Two Describe a Third description 10
2008-02-03 Another New story description 20
2009-03-04 More story Stuff 20
789 Phil Upspace 75
2008-01-02 A short New Games description 33
20011-04-03 Another Games description 22
20011-04-04 More Games Stuff 20

Below is the script code to implement expand/collapse in HTML table rows using jQuery:

   

You can check the output will appear like below:

expand collapse in HTML table rows using jQuery

You may like the following jQuery tutorials:

In this tutorial, we learned how to implement expand/collapse in HTML table rows using jQuery.

I am Developer working on Microsoft Technologies for the past 6+years. I am very much passionate about programming and my core skills are SharePoint, ASP.NET & C#,Jquery,Javascript,REST. I am running this blog to share my experience & learning with the community I am an MCP, MCTS .NET & Sharepoint 2010, MCPD Sharepoint 2010, and MCSD HTML 5,Sharepoint 2013 Core
Solutions. I am currently working on Sharepoint 2010, MOSS 2007, Sharepoint 2013,Sharepoint 2013 App Dev, C#, ASP.NET, and SQL Server 2008.

Источник

expand/collapse table rows with JQuery

I want to expand and collapse table rows when header columns is clicked. I only want to expand/collapse rows which are under the specific header (clicked). Here is my table structure:

  
Header
data data
data data
Header
date data
data data
data data

Any thoughts on how can I accomplish this task. Using div this task seems quite simple, but I have tabular data which I want to manipulate. One idea I can think of is to use css class in every row which distinguish rows in under each header and use JQuery to expand/collapse those rows only when header is clicked. But if my table has 10-15 headers then it seems difficult to keep track of css classes. Please suggest a suitable way to achieve this.

While you can have multiple tbody elements I’m pretty certain that you can only have one thead element, so I’m not sure that would help. I have, though, posted an answer which I think meets your needs. Though it does involve adding a class-name to the tr elements that contain a header.

7 Answers 7

Give a class say header to the header rows, use nextUntil to get all rows beneath the clicked header until the next header.

JS

Html

  Header  data data  data data  

Demo

$('.header').click(function()< $(this).find('span').text(function(_, value)); $(this).nextUntil('tr.header').slideToggle(100); // or just use "toggle()" >); 

Demo

You can also use promise to toggle the span icon/text after the toggle is complete in-case of animated toggle.

Or just with a css pseudo element to represent the sign of expansion/collapse, and just toggle a class on the header.

.header .sign:after < content:"+"; display:inline-block; >.header.expand .sign:after
$(this).toggleClass('expand').nextUntil('tr.header').slideToggle(100); 

Demo

Just to complement, .toggle() works better than .slideToggle(100); when there are many lines to toggle.

The easiest way to achieve this, without changing the HTML table -based structure, is to use a class-name on the tr elements containing a header, such as .header , to give:

 
Header
data data
data data
Header
date data
data data
data data
// bind a click-handler to the 'tr' elements with the 'header' class-name: $('tr.header').click(function()< /* get all the subsequent 'tr' elements until the next 'tr.header', set the 'display' property to 'none' (if they're visible), to 'table-row' if they're not: */ $(this).nextUntil('tr.header').css('display', function(i,v)< return this.style.display === 'table-row' ? 'none' : 'table-row'; >); >); 

In the linked demo I’ve used CSS to hide the tr elements that don’t have the header class-name; in practice though (despite the relative rarity of users with JavaScript disabled) I’d suggest using JavaScript to add the relevant class-names, hiding and showing as appropriate:

// hide all 'tr' elements, then filter them to find. $('tr').hide().filter(function () < // only those 'tr' elements that have 'td' elements with a 'colspan' attribute: return $(this).find('td[colspan]').length; // add the 'header' class to those found 'tr' elements >).addClass('header') // set the display of those elements to 'table-row': .css('display', 'table-row') // bind the click-handler (as above) .click(function () < $(this).nextUntil('tr.header').css('display', function (i, v) < return this.style.display === 'table-row' ? 'none' : 'table-row'; >); >); 

I expanded one of the answers, however my functionality is a bit different. In my version, different rows form different groups. And «header» row triggers collapsing/expanding of that particular group. Also, each individual subgroup memorizes state that its in. It might sound a bit confusing, you can test drive my version using jsfiddle. Hope this code snippets will be helpful to someone out there!

 
Header 1 Header 2
Group 1 data 2
data 3 data 4
Group 2 data 2
data 3 data 4
Sub Group 1 data 6
data 7 data 8
data 9 data 10
Sub Sub Group 1 data 11
data 12 data 13
data 14 data 15
Sub Group 2 data 11
data 12 data 13
data 14 data 15
table, tr, td, th < border: 1px solid black; border-collapse:collapse; >img.button_open < content:url('http://code.stephenmorley.org/javascript/collapsible-lists/button-open.png'); cursor:pointer; >img.button_closed < content: url('http://code.stephenmorley.org/javascript/collapsible-lists/button-closed.png'); cursor:pointer; >
function CreateGroup(group_name) < // Create Button(Image) $('td.' + group_name).prepend(""); // Add Padding to Data $('tr.' + group_name).each(function () < var first_td = $(this).children('td').first(); var padding_left = parseInt($(first_td).css('padding-left')); $(first_td).css('padding-left', String(padding_left + 25) + 'px'); >); RestoreGroup(group_name); // Tie toggle function to the button $('img.' + group_name).click(function()< ToggleGroup(group_name); >); > function ToggleGroup(group_name) < ToggleButton($('img.' + group_name)); RestoreGroup(group_name); >function RestoreGroup(group_name) < if ($('img.' + group_name).hasClass('button_open')) < // Open everything $('tr.' + group_name).show(); // Close subgroups that been closed $('tr.' + group_name).find('img.button_closed').each(function () < sub_group_name = $(this).attr('class').split(/\s+/)[0]; //console.log(sub_group_name); RestoreGroup(sub_group_name); >); > if ($('img.' + group_name).hasClass('button_closed')) < // Close everything $('tr.' + group_name).hide(); >> function ToggleButton(button) < $(button).toggleClass('button_open'); $(button).toggleClass('button_closed'); >CreateGroup('group1'); CreateGroup('group2'); CreateGroup('sub_group1'); CreateGroup('sub_group2'); CreateGroup('sub_sub_group1'); 

Источник

How to implement collapsible table rows in Javascript?

when I press on the + sign. Are there any JavaScript libraries that could help me do such a thing, or is there some magic native command that could do it for my easily?

4 Answers 4

Put each section in a different tbody element, each with an id attribute. Make the first row of each tbody a single header cell (th with the appropriate colspan) with your section header text and an anchor to #the_tbody_id. Then:

  1. Add CSS: tbody[id]:not(:target) > tr:first-child + tr
  2. Or, for non-partial-CSS3 browsers, add Javascript to hide the non-first rows in each of those tbodys (eg, loop through tablelement.getElementsByTagName(«tbody»)), and hook the anchor to toggle their display property on click (eg, loop through tbodyelement.getElementsByTagName(«tr»)).

Setting display on table rows is tricky. What do you set the display property to when you want to display the tr? block? Heh. A TR is not a block level element.

No, you would set it to ‘table-row’ if you were setting it. But you obviously don’t have to. Toggle the .style.display property between the values ‘none’ and ».

Dojo and Jquery both can do this for you, but in case you don’t want to use a javascript library this isn’t hard with just plain JavaScript. Use an onclick event handler for the +/- toggle, write a function that is executed which sets a block element (one of your sets of rows) to display:none (a css rule). If you’re going to use a table to display this, don’t try to dynamically hide or show table rows, it doesn’t work well.

Источник

Оцените статью