你不需要插件来做到这一点,这通常使用几行简单的代码来完成。
演示:http: //jsfiddle.net/FHWaw/2/
在这个演示中,我在行和单元格上指定了一个标识“部分”(可以打开或关闭的东西)的属性:
<table>
<tr><td section-opener=mysection1 section-state=open> SOME HEADER </td></tr>
<tr section-content=mysection1><td>some text</td><td>some other text</td></tr>
<tr section-content=mysection1><td>some AAA</td><td>some zaaother text</td></tr>
<tr section-content=mysection1><td>some text</td><td>some other text</td></tr>
<tr><td section-opener=anothersection section-state=close> Hu ? </td></tr>
<tr section-content=anothersection><td>some text</td><td>some other text</td></tr>
<tr section-content=anothersection><td>some text</td><td>some other text</td></tr>
</table>
(在此示例中,第一部分首先打开,而第二部分打开)
然后我有这个,在点击时,它决定更改行的类,以便让它们可见或不可见:
$('body').on('click', 'td[section-opener]', function() {
changeState($(this).attr('section-opener'));
});
changeState = function(sectionId, newstate) {
var $opener = $('td[section-opener="'+sectionId+'"]');
var oldstate = $opener.attr('section-state');
if (oldstate==newstate) return newstate;
newstate = oldstate=='open' ? 'close' : 'open'; // sanitization
$opener.attr('section-state', newstate);
$('tr[section-content="'+sectionId+'"]').attr('section-state', newstate);
return newstate;
};
我有一个 CSS,主要是让封闭部分被隐藏(并且开启者可以清楚地识别):
td[section-opener] {
font-weight: bold;
cursor: pointer;
}
td[section-opener][section-state="close"]:before {
color: #ccc;
content: " \25B6 ";
}
td[section-opener][section-state="close"]:hover:before{
color: #aaa;
content: " \25B6 ";
}
td[section-opener][section-state="open"]:before {
content: "\25BC ";
}
tr[section-content][section-state="close"] {
display: none;
}
tr[section-content][section-state="open"] {
display: table-row;
}
由于不会删除任何内容,因此关闭再打开时您编辑的输入不会丢失。