我有一个列表Web部件,具有2个级别的分组依据。我正在尝试添加一个按钮以展开或折叠(切换)。期望-即使列表被部分展开,单击“展开”也应展开所有组,单击“折叠”应将所有组折叠。
我曾尝试从网络复制少量代码,但由于是非编码者,我想我没有得到理想的结果。
示例:使用此代码,但不会切换开关,对于部分打开的列表,它将关闭未打开的项目并关闭已关闭的项目。
<script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script language="Javascript" type="text/Javascript">
function collapseGroups() {
$("img[id^='img_']").click();
}
function expandGroups() {
$("img[id^='img_']").click();
}
</script>
<input id="btnExpand1" onclick="expandGroups()" type="button" value="Expand All" />
<input id="btnExpand2" onclick="collapseGroups()" type="button" value="Collapse All" />
[您需要获取所有锚点标签,这些锚标签的点击可扩展或折叠一个组(针对特定组),然后迭代调用“ click()”事件。例如
$('.ms-listviewtable').find('tbody').each(function() { //loop by id of your list view table
if($(this).attr('id').indexOf('titl0-') > -1) { //for each 'tbody' with 'id' containing 'titl0' string, you can inspect the html of your list view table to see what id's are being formed'
//console.log($(this).attr('id'));
var a = $(this).find('a');
a.click();
}
});
您可以设置一个标志来跟踪状态当前是'Expanded'
还是'Collapsed'
。
在SharePoint 2013中,您可以按以下方式修改代码。
<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
function collapseGroups() {
$("img.ms-commentcollapse-icon").click();
}
function expandGroups() {
$("img.ms-commentexpand-icon").click();
}
</script>
<input id="btnExpand1" onclick="expandGroups()" type="button" value="Expand All" />
<input id="btnExpand2" onclick="collapseGroups()" type="button" value="Collapse All" />