【问题标题】:collapsible list with dynamically loaded content具有动态加载内容的可折叠列表
【发布时间】:2015-10-23 23:34:24
【问题描述】:

我尝试了在互联网上找到的各种列表,但似乎没有一个能正常工作。我认为原因可能是列表的内容是动态加载的(使用 JQuery ajax)。

$(document).ready 上,我加载列表并将html 数据附加到div

之后我尝试使列表可折叠(列表是嵌套的,它是数据库的导航)。

到目前为止我最好的尝试:

function loadNavigation() {
  var _url = "http://api.writeplace.nl/idofnavigation
	var credentials = btoa("username:password!");
  
  $.ajax({
    headers: {
    	"Authorization": "Basic " + credentials
  	},
    url: _url,
    success: function(data, text) {
    	//console.log(data);
    	$("#container").empty();
    	$("#container").append(data);
		$("#nav > li > ul ").addClass("verberg");
		$("#nav > li > ul > li > ul").addClass("verberg");
    }
  });
}


function loadPage(documentId) {
	
  if (!documentId) {
  	documentId = "document-id";
  }

  var _url = "http://api.writeplace.nl/=" +  documentId;

	var credentials = btoa("username:password!");
	
  
  $.ajax({
    headers: {
    	"Authorization": "Basic " + credentials
  	},
    url: _url,
    success: function(data, text) {
    	$("#tekst").empty();
    	$("#tekst").append(data);
    }
  });
}




	

function uitvouwen(){

function uitvouwen1(){
	$("#container").on("click", "#nav > li", function(){

		$(this).children().toggleClass("verberg");
		//console.log("khbvdkvbedkvbewdkvfjbwekbfwkdjbvkejdbv");
	});
}


function uitvouwen2(){
	$("#container").on("click", "#nav > li > ul > li", function(){
		$(this).children().toggleClass("verberg");
		//console.log("gffhgfhh");
		
	});
}


uitvouwen2();
uitvouwen1();



}

导航已加载,最上面的ul 的id 为nav。在document.ready 上,我将hide 类添加到#nav>li>ul#nav>li>ul>li>ul,因此li 项目默认设置为隐藏。然而,此列表的问题在于,如果您单击树中的链接或更深层的 li 项目,它也会将其注册为对上述级别的单击。

有没有办法解决这个问题?还是一种更好(更简洁)的方式来制作具有动态内容的可折叠嵌套列表?

【问题讨论】:

  • 您需要某种路径控制。因此,当有人点击链接时,该函数实际上可以确定点击了哪个链接。您可以通过使用类或data-attribute 来做到这一点。
  • 为了进一步帮助您,我们需要更多代码。列表的 HTML 和函数 loadnavigation。您能否更新您的 sn-p 使其适用于您拥有的基本版本?
  • 感谢您的回答!我已经用 j-query 的其余部分更新了 sn-p。有什么方法可以用我的 jquery 文件添加路径控制吗? (我没有访问原始服务器文件的权限,其他人正在管理这些文件)
  • 是的,您可以,当添加元素时,您可以控制它。你能发布渲染的输出吗?
  • jsfiddle.net/pr95mq6f/1 将所有内容上传到 jsfiddle 似乎有问题,链接显示的 jquery 站点无法正常工作,我会尝试修复它

标签: javascript jquery ajax nested-lists dynamic-content


【解决方案1】:

这是我将如何执行此操作的示例。我使用了data-id。属性。这在为菜单分配功能时会派上用场。您始终可以使用 ID 向下或向上移动。它通过查找包含data-attribute 的祖先或自身来隐藏/显示UL

$(document).ready(function() {
  $("#nav").html(loadnavigation(menu, null)); //to load the li items//
  $("#container").click(function(e){
    //e refers to the click event. 
    //bind it
    var eventTarget = $(e.target);

    //we need to use closest here. If you append other HTML-elements to the li, eventTarget
    //will refer to these elements. This way, it always travels up to the 
    //closest ancestor with an data-id attribute.
    eventTarget.closest("li[data-id]").children("ul").toggleClass("hide"); //find the closest ancestor (or self) that has data-id and hide its ul.
    
  });
});

//below is my own code just to show an example of my comment:

//I'm using a array, combined with objects to build the basic structure of the menu.
var menu = [{
  name: "list 1",
  menu: [{
    name: "sublist 1"
  }, {
    name: "sublist 2"
  }]
}, {
  name: "list 2",
  menu: [{
    name: "sublist 1",
    menu: [{
      name: "sublist 1"
    }, {
      name: "sublist 2"
    }]
  }, {
    name: "sublist 2"
  }]
}, {
  name: "list 3"
}];

function loadnavigation(obj, dataID) {
  var html = ""; //using var here is important. We want this variable to be private and within the scope of only this function.
  //this will be a recursive function.
  for (var i = 0; i < obj.length; i++) {
    var id = i;
    if (dataID != null || dataID != undefined) //we start the function at first with value null.
    {
      id = dataID + "-" + id; //separate id's from eachother with a dash   
    }
    var menuItem = '<li data-id="' + id + '">';
    menuItem += obj[i].name; //append the menu name with +=
    if (obj[i].menu) //obj has submenu
    {
      menuItem += "<ul class='hide'>" + loadnavigation(obj[i].menu, id) + "</ul>";
    }
    menuItem += "</li>"; //close
    html += menuItem;
  }
  return html;
}
ul.hide {
 display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
  <ul id="nav">

  </ul>
</div>

代码应该自己解释。

为什么我觉得data-attribute很方便:

$("li[data-id^='1-']")

上面的选择器将选择从 id 1 开始的树分支中的所有子节点。

【讨论】:

  • 像魅力一样工作。谢谢!
猜你喜欢
  • 2014-03-26
  • 1970-01-01
  • 2011-06-19
  • 2012-03-05
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 2021-04-30
相关资源
最近更新 更多