我有一些旧的代码,我想看看我是否可以使用knockout.js。
var secs = 960 + Math.floor(data.length / 6) * 1060 + 10;
$("#metro-scrollbar").css("width", secs.toString() + 'px');
var group_count = 0;
section = ""
for (i = 0; i < data.length; i++) {
if (i % 6 == 0)
section += "<section class=\"" + (data.length - i <= 6 ? "last" : "") + "\"><h2 class=\"section-title\">Group " + ++group_count + "</h2>";
section += "<a href=\"/TheoryTests/Test/" + data[i].Id + "/" + data[i].Title.replace(/ /g,'-') + "\">";
section += "<div class=\"metro-tile double" + (i % 3 == 2 ? " last" : "") + "\"><div class=\"a1x2\"></div><div class=\"live-tile metrogreen\">";
section += "<span class=\"tile-title\">" + data[i].Title + "</span>";
section += "<div class=\"dark\"><div class=\"TheoryTestTile\"><p>Helo</p></div></div>";
section += "</div></div></a>";
if (i % 6 == 5)
section += "</section>";
}javascript生成了一组带有一些嵌套div的部分。每6个div就会创建一个新的部分。我了解如何使用knockout.js绑定数据
data-bind="foreach: test,visible: tests().length > 0“但是,如果(I%6 == 0),我该如何做出决策呢?
更新
<div id="metro-grid">
<div id="metro-scrollbar" data-bind="foreach: tests, visible: tests().length > 0">
<!-- ko if: $index() % 6 == 0 -->
<section data-bind ="css: { 'last' : $parent.isLastSection($index)}," >
<!-- /ko -->
<div class="metro-tile double " data-bind ="css: { 'last' : $parent.isLastTile($index)}">
<div class="a1x2"></div><div class="live-tile metrogreen">
<div ></div>
</div>
</div>
<!-- ko if: $index() % 6 == 0 -->
</section>
<!-- /ko -->
</div>
</div>上面的问题是,我仍然希望在节中生成div。测试是一个包含12个元素的列表。我想在每个第6个元素中创建一个新的部分。
更新2
function TaskListViewModel() {
// Data
var self = this;
self.tests = ko.observableArray([]);
this.sections = [];
self.isLastTile = function (i) {
return i() % 3 == 2;
};
self.isLastSection = function (i) {
return i() >= Math.floor(self.tests().length / 6);
};
this.createSections = ko.computed(function () {
var tests = self.tests();
current = [];
sections.push(current);
for (var i = 0; i < tests.length; i++) {
current.push(tests[i]);
if (((i+1) % 6) == 0) {
current = [];
sections.push(current);
}
}
});
$.getJSON(url, function (allData) {
var mappedTasks = $.map(allData, function (item) { return new Task(item) });
self.tests(mappedTasks);
//var secs1 = self.tests().length / 6 * 960 + 960 + 10;
var secs = 960 + Math.floor(self.tests().length / 6) * 1060 + 10;
$("#metro-scrollbar").css("width", secs.toString() + 'px');
});
}
ko.applyBindings(new TaskListViewModel());发布于 2012-10-18 00:51:43
我建议你在你的viewModel中添加2个ko.observables。第一个是'sections‘数组,第二个是ko.computed,每次测试更新时都会重新填充'sections’,然后使用绑定循环'sections‘。
这是一个小提琴的例子:
http://jsfiddle.net/Zholen/q57RX/
var vm = function() {
var self = this;
this.tests = ko.observableArray([1,2,3,4,5,6,7,8,9,
10,11,12,13,14,15,16]);
this.sections = [];
this.createSections = ko.computed(function(){
var tests = self.tests();
console.log(tests.length);
for(var i = 0; i < tests.length; i++)
{
if(i%6 == 0)
self.sections.push([]);
self.sections[self.sections.length - 1].push(tests[i]);
}
});
};
<div>
<div data-bind="foreach: sections">
<section data-bind="foreach: $data">
<div data-bind="text:$data">
</div>
</section>
</div>
</div>发布于 2012-10-17 23:15:11
您可以在foreach context中使用$index对象:
<div data-bind="foreach: items">
<span data-bind="visible: $index() % 6 == 0"></span>
</div>阅读有关foreach绑定的更多信息:http://knockoutjs.com/documentation/foreach-binding.html
编辑:要避免渲染,请执行以下操作:
<div data-bind="foreach: items">
<!-- ko if: $index() % 6 == 0-->
<span data-bind="text: $data"></span>
<!-- /ko -->
</div>发布于 2012-10-17 23:22:08
Knockout.js有一个"if“绑定:http://knockoutjs.com/documentation/if-binding.html
https://stackoverflow.com/questions/12937219
复制相似问题