简体   繁体   English

在表格行中展开和折叠

[英]Expand and collapse in table row

I have a table which is being generated from server side and it looks like this . 我有一个从服务器端生成的表,它看起来像这样

Now the requirement is to hide all the column of Category B, remove duplicate rows for Category A and show entries of corresponding entries of Category B in expand-collapse way. 现在要求隐藏类别B的所有列,删除类别A的重复行,并以展开 - 折叠方式显示类别B的相应条目的条目。 Each A1 Name Column cell will have a expand button and when it is clicked the entries of B columns of that row will be shown below that. 每个A1 Name列单元格都有一个展开按钮,当单击它时,该行的B列条目将显示在下面。

I'm able to hide B category and remove duplicate rows by 我可以隐藏B类别并删除重复的行

var hide_duplicate_row = function () {
            var seen = {};
            $('td:nth-child(2)').each(function () {
                var txt = $(this).text();
                if (seen[txt])
                    $(this).closest('tr').hide();
                else
                    seen[txt] = true;
            });
        };


var show_only_head = function(){
$('td:nth-child(4),th:nth-child(4)').hide();
$('td:nth-child(3),th:nth-child(3)').hide();
}

hide_duplicate_row();
show_only_head();

Fiddle: http://jsfiddle.net/ME3kG/3/ 小提琴: http//jsfiddle.net/ME3kG/3/

but I'm stuck with the expand collapse part, how do I populate the B category's row data in this way? 但是我坚持使用展开折叠部分,如何以这种方式填充B类别的行数据? Any input on this will be appreciated, thanks. 任何有关此的意见将不胜感激,谢谢。

Full table: 全表:

在此输入图像描述

Desired table: 所需表格:

在此输入图像描述

Here is the plan for this: 这是这个计划:

Goes through the table rows one by one: 逐个遍历表格行:

$('table tr').each(function () {

On each row get the content you need: 在每一行获取您需要的内容:

var cell_3 = $("td:nth-child(3)", this).html(); // a3
var cell_4 = $("td:nth-child(4)", this).html(); // a4

While you are still processing the current row modify the A1 column like this: 当您仍在处理当前行时,修改A1列,如下所示:

$( "td:nth-child(1)", this ).append( '<div class="toggle">' + cell_3 + ' - ' + cell_4 + '</div>' );

Now you should have a newly generated DIV in each A1 column. 现在,您应该在每个A1列中都有一个新生成的DIV。 You'll need to assign a toggle functionality on 'click' event and you should be done. 您需要在“点击”事件中指定切换功能,您应该完成。 The END. 结束。

It seems you are developing using jQuery and this is the reason to explain you the idea, not giving you the exact code. 看来你正在使用jQuery进行开发,这就是向你解释这个想法的原因,而不是给你确切的代码。 :-) :-)

EDIT 1 : 编辑1

Here is the final code, according to the few specific requirements: http://jsfiddle.net/ME3kG/26/ and some formatting: http://jsfiddle.net/ME3kG/30/ 这是最终的代码,根据一些具体要求: http//jsfiddle.net/ME3kG/26/和一些格式: http//jsfiddle.net/ME3kG/30/

在你的td中添加一个隐藏onload的div并在click事件中显示它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM