我有以下JavaScript,它根据按列"报告类别"分组的SharePoint列表动态创建链接。然而,代码正在运行,如果有一个空组但有数据,那么它应该创建所有链接和一个名为" Others"的链接。我想知道为什么要创建2个链接,其他"其他"以及它应该创建的所有其他链接。 html已上传https://jsfiddle.net/ea8dyvyq/5/
<div class="nav-report-category">
</div>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//$("table.ms-listviewtable td[class=’ms-gb’]").each(function() {
//$("table.ms-listviewtable td[class^='ms-gb']").each(function(){
$(".ms-listviewtable td[class^='ms-gb']").each(function(){
$(".ms-listviewtable td[class^='ms-gb']").length;
//$("table.ms-listviewtable td.ms-gb").each(function(){
//$("ms-gb.td").each(function(){
//Hide all Counts for Groups
//$(this).find("span:last").remove();
//alert('loop is working');
var mydiv = document.getElementsByClassName("nav-report-category")[0];
//var liTag = document.createElement('li');
//var aTag = document.createElement('a');
//aTag.setAttribute("href","yourlink.htm");
var str = $(this).text();
str = str.substring(str.indexOf(":") +1); //remove everything before :
str = str.substring(0, str.indexOf("(")); //remove counts
//str = str.substring(str.indexOf(":") + 1);
//$(this).find("span:last").remove();
//var RptCat = $(".ms-listviewtable td.ms-gb:first a:eq(1)").text();
//var grNameRaw = $(this).text().replace(RptCat+' :','');
//var grName = grNameRaw.substring(0,grNameRaw.indexOf('(')-1).replace(/s|xA0/g,'');
//aTag.innerHTML = $(this).text();
//aTag.innerHTML = str.trim();
//aTag.setAttribute('target', '_blank');
if (!str.trim() || str.trim() == null)
{
var liTag = document.createElement('li');
var aTag = document.createElement('a');
aTag.innerHTML = "Other";
}
else
{
var liTag = document.createElement('li');
var aTag = document.createElement('a');
aTag.innerHTML = str.trim();
}
aTag.setAttribute('target', '_blank');
//var href = $(this).attr('href');
var link = window.location.href + "&RC=" + str.trim();
aTag.setAttribute("href",link);
liTag.appendChild(aTag);
mydiv.appendChild(liTag);
//mydiv.appendChild(aTag);
//mydiv.appendChild(document.createElement ("span"));
//mydiv.appendChild(document.createTextNode (" "));
});
//alert('test');
});
</script>