gpt4 book ai didi

javascript - 多层 ul li backbone.js

转载 作者:行者123 更新时间:2023-11-29 19:54:21 24 4
gpt4 key购买 nike

我正在尝试使用 backbone.js 进行嵌套导航,下面是我的代码。

Json 数据:

[
{"id":1,"name":"one","parent_id":0},
{"id":2,"name":"two","parent_id":0},
{"id":3,"name":"three","parent_id":0},
{"id":4,"name":"four","parent_id":0},
{"id":5,"name":"five","parent_id":0},
{"id":6,"name":"one","parent_id":2},
{"id":7,"name":"two","parent_id":2},
{"id":8,"name":"three","parent_id":2},
{"id":9,"name":"four","parent_id":2},
{"id":10,"name":"five","parent_id":2},
]

HTML :

<ul>
<li><a href="#">one</a></li>
<li><a href="#">two</a>
<ul>
<li><a href="#">one</a></li>
<li><a href="#">two</a></li>
<li><a href="#">three</a></li>
<li><a href="#">four</a></li>
<li><a href="#">five</a></li>
</ul>
</li>
<li><a href="#">three</a></li>
<li><a href="#">four</a></li>
<li><a href="#">five</a></li>
</ul>

和 Backbonejs 代码在这里:

var MenuList = Backbone.View.extend({
tagName: 'ul',
id: 'menu-trial-task',
initialize: function() {
this.collection = new ItemCollection();
this.collection.on('sync', this.render, this);
this.collection.fetch();
},
render: function() {
_.each( this.collection.models, function( item ) {
this.$el.append( new MenuItem({model:item}).render().el );
}, this);
$('nav').html(this.$el);
return this;
}
});

var MenuItem = Backbone.View.extend({
tagName: 'li',
initialize: function() {
this.template = _.template($('#munu_itemt_view').html());
},
render: function() {
this.$el.html( this.template( this.model.toJSON()));
return this;
}
});
var menuList = new MenuList();

结果是:

<ul>
<li><a href="#">one</a></li>
<li><a href="#">two</a></li>
<li><a href="#">three</a></li>
<li><a href="#">four</a></li>
<li><a href="#">five</a></li>
<li><a href="#">one</a></li>
<li><a href="#">two</a></li>
<li><a href="#">three</a></li>
<li><a href="#">four</a></li>
<li><a href="#">five</a></li>
</ul>

如您所见,这不是必需的结果。

我也尝试过使用 underscore.js 但失败了。有什么好的简单方法可以做到这一点吗?

最佳答案

尝试嵌套 View ,我建议使用模板

var data = [
{"id":1,"name":"one","parent_id":0},
{"id":2,"name":"two","parent_id":0},
{"id":3,"name":"three","parent_id":0},
{"id":4,"name":"four","parent_id":0},
{"id":5,"name":"five","parent_id":0},
{"id":6,"name":"one","parent_id":2},
{"id":7,"name":"two","parent_id":2},
{"id":8,"name":"three","parent_id":2},
{"id":9,"name":"four","parent_id":2},
{"id":10,"name":"five","parent_id":2},
]

var NestedView = Backbone.View.extend({
render: function(){
this.$el.html( "<ul id='ul-0'></ul>" )
this.collection.each( function( model ){ this.renderElement( model ) }, this )
},

renderElement: function( model ){
var ul = this.getParentUl( model );
this.appendElement( ul, model );
},

getParentUl: function( model ) {
var ul = this.$el.find( "#ul-" + model.get( "parent_id" ) );
if( ul.length == 0 ) {
this.appendListInElement( model );
ul = this.$el.find( "#ul-" + model.get( "parent_id" ) );
}

return ul;
},

appendListInElement: function( model ){
var li = this.$el.find( "#li-" + model.get( "parent_id" ) );
li.after( "<ul id='ul-" + model.get( "parent_id" ) + "'></ul>" );
},

appendElement: function( ul, model ){
ul.append( "<li id='li-" + model.get( "id" ) + "'>" + model.get( "name" ) + "</li>" );
}
});

var elements = new Backbone.Collection( data );
var nestedView = new NestedView({ el: "#wrapper", collection: elements });
nestedView.render();


<div id="wrapper"></div>

完整代码如下,带下划线模板

<!DOCTYPE html>
<html>
<head>
<title>Backbone</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://underscorejs.org/underscore.js"></script>
<script type="text/javascript" src="http://backbonejs.org/backbone.js"></script>

<script type="text/template" id="parent-template">
<ul id='ul-<%= parent_id %>'></ul>
</script>
<script type="text/template" id="child-template">
<li id="li-<%= id %>"><%= name %></li>
</script>

<script type="text/javascript">

var compiledParent = _.template( $('#parent-template').html() );
var compiledChild = _.template( $('#child-template').html() );

var data = [
{"id":1,"name":"one","parent_id":0},
{"id":2,"name":"two","parent_id":0},
{"id":3,"name":"three","parent_id":0},
{"id":4,"name":"four","parent_id":0},
{"id":5,"name":"five","parent_id":0},
{"id":6,"name":"one","parent_id":2},
{"id":7,"name":"two","parent_id":2},
{"id":8,"name":"three","parent_id":2},
{"id":9,"name":"four","parent_id":2},
{"id":10,"name":"five","parent_id":2},
];

var NestedView = Backbone.View.extend({
render: function(){
this.$el.html( compiledParent({parent_id : '0'}) );
this.collection.each( function( model ){ this.renderElement( model ) }, this )
},

renderElement: function( model ){
var ul = this.getParentUl( model );
this.appendElement( ul, model );
},

getParentUl: function( model ) {
var ul = this.$el.find( "#ul-" + model.get( "parent_id" ) );
if( ul.length == 0 ) {
this.appendListInElement( model );
ul = this.$el.find( "#ul-" + model.get( "parent_id" ) );
}

return ul;
},

appendListInElement: function( model ){
var li = this.$el.find( "#li-" + model.get( "parent_id" ) );
li.after( compiledParent({parent_id : model.get( "parent_id" )}) );
},

appendElement: function( ul, model ){
ul.append( compiledChild({id:model.get( "id" ) , name:model.get( "name" )}) );
}
});

$(document).ready(function()
{
var elements = new Backbone.Collection( data );
var nestedView = new NestedView({ el: "#wrapper", collection: elements });
nestedView.render();
});
</script>
</head>
<body>
<div id="wrapper">
</div>
</body>
</html>

关于javascript - 多层 ul li backbone.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16417768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com