[英]Show tabs with bootstrap and knockout.js
I am trying to show tabs with Bootstrap. 我正在尝试使用Bootstrap显示选项卡。
With this code tabs don't appear. 使用此代码选项卡不会出现。
View: 视图:
<div id="tabs" class="container">
<ul class="nav nav-tabs">
<ul class="folders" data-bind="foreach: folders">
<li data-bind="text: $data, css:{ selected: $data == $root.chosenFolderId() }, click: $root.goToFolder" >
<a href="#home" data-toggle="tab"></a>
<a href="#home" data-toggle="tab">
</li>
</ul>
< /ul>
</div>
View Model: 查看模型:
function WebmailViewModel() {
// Data
var self = this;
self.folders = ['Inbox', 'Archive', 'Sent', 'Spam'];
self.chosenFolderId = ko.observable();
// Behaviours
self.goToFolder = function(folder) { self.chosenFolderId(folder); };
};
ko.applyBindings(new WebmailViewModel());
What is the problem with my code? 我的代码有什么问题?
You have a lot of problems in the code you've posted: 您发布的代码中存在很多问题:
a
tag that is not closed. a
标签未关闭。 ul
that's a direct descendant of the first ul
, you probably want just one. ul
这是第一个直接后裔ul
,你可能只想要一个。 a
tags inside one tab li
? a
一个片内标签li
? You only want one. li
and a text
binding (which will overwrite the content). li
内容和text
绑定(这将覆盖内容)。 Bootstrap requires the a
so you should move the binding to that element I'd think. a
因此您应该将绑定移动到我认为的那个元素上。 click
binding should probably be on the a
tag, not the li
. click
绑定也许应该是在a
标签,而不是li
。 selected
class for active li
items, but if you look at the Bootstrap documentation you'll see that it should be active
. li
项渲染了一个selected
类,但是如果查看Bootstrap文档,您会发现它应该是active
。 data-*
attributes from Bootstrap (ie data-toggle
) with a Knockout way of selecting tabs ( click
and css
bindings). data-*
属性(即data-toggle
)与选择选项卡( click
和css
绑定)的淘汰方式混合使用。 You probably only want the latter After fixing all of those (and maybe one or two minor things I forgot to mention above), you end up with something along these lines: 修复所有这些问题之后(也许我忘了上面提到的一两个小问题),您将得到以下结果:
function WebmailViewModel() { // Data var self = this; self.folders = ['Inbox', 'Archive', 'Sent', 'Spam']; self.chosenFolderId = ko.observable(); // Behaviours self.goToFolder = function(folder) { self.chosenFolderId(folder); }; }; ko.applyBindings(new WebmailViewModel());
body { padding: 10px; }
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/> <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <div id="tabs" class="container"> <ul class="nav nav-tabs folders" data-bind="foreach: folders"> <li data-bind="css:{ active: $data == $root.chosenFolderId() }"> <a href="#" data-bind="text: $data, click: $root.goToFolder"></a> </li> </ul> </div>
After correcting all the mistakes Jeroen pointed out, there is one little improvement left to make: 纠正了Jeroen指出的所有错误之后,还有一点需要改进:
function WebmailViewModel() { var self = this; self.folders = [ {id: '#inbox', name: 'Inbox'}, {id: '#archive', name: 'Archive'}, {id: '#sent', name: 'Sent'}, {id: '#spam', name: 'Spam'} ]; self.chosenFolder = ko.observable(self.folders[0]); } $(function () { ko.applyBindings(new WebmailViewModel()); });
<link href="http://getbootstrap.com/dist/css/bootstrap.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.0/bootstrap.min.js"></script> <div id="tabs" class="container"> <ul class="nav nav-tabs" role="tablist" data-bind="foreach: folders"> <li data-bind=" click: $root.chosenFolder, css: { active: $data === $root.chosenFolder() } "> <a href="" role="tab" data-toggle="tab" data-bind=" text: name, attr: {href: id} "></a> </li> </ul> <div>Tab content here</div> </div>
Note that observables are themselves functions. 请注意,可观察对象本身就是函数。 If you want to store a clicked object in an observable you can assign that observable as the click handler directly.
如果要将单击的对象存储在可观察对象中,则可以将该可观察对象直接分配为单击处理程序。
When a click event occurs, knockout will call the event handler and pass the clicked object as the first argument, which conveniently exactly is the way you set an observable's value in knockout. 当发生click事件时,敲除将调用事件处理程序,并将被单击的对象作为第一个参数传递,这恰好是您在敲除中设置可观察值的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.