QuintOutJS-如何使用可观察的阵列隐藏在foreach内部的某些元素?
我有一个网站所有者列表。我正在尝试构建一个UI,当我单击它们时,它将显示有关所有者的更多信息。
this.toExpand = ko.observableArray(); //initialize an observable array
this.invertExpand = ko.observable("");
this.invertExpand = function (index) {
if (self.invertExpand[index] == false) {
self.invertExpand[index] = true;
alert(self.invertExpand[index]); //testing whether the value changed
}
else {
self.invertExpand[index] = false;
alert(self.invertExpand[index]); //testing whether the value changed
}
};
这是HTML代码:
<div data-bind="foreach: WebsiteOwners">
<div>
<button data-bind="click: $root.invertExpand.bind(this,$index())" class="label label-default">>Click to Expand</button>
</div>
<div data-bind="visible: $root.toExpand()[$index]">
Primary Owner: <span data-bind="text:primaryOwner"></span>
Website Name : <span data-bind="text:websiteName"></span>
//...additional information
</div>
</div>
I have a list of WebsiteOwners. I'm trying to build a UI which will display more information about the owners when I click on them.
this.toExpand = ko.observableArray(); //initialize an observable array
this.invertExpand = ko.observable("");
this.invertExpand = function (index) {
if (self.invertExpand[index] == false) {
self.invertExpand[index] = true;
alert(self.invertExpand[index]); //testing whether the value changed
}
else {
self.invertExpand[index] = false;
alert(self.invertExpand[index]); //testing whether the value changed
}
};
Here's the HTML code :
<div data-bind="foreach: WebsiteOwners">
<div>
<button data-bind="click: $root.invertExpand.bind(this,$index())" class="label label-default">>Click to Expand</button>
</div>
<div data-bind="visible: $root.toExpand()[$index]">
Primary Owner: <span data-bind="text:primaryOwner"></span>
Website Name : <span data-bind="text:websiteName"></span>
//...additional information
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
网站世界
项目直接存储在可观察到的内容中。无需使用索引。不要忘记您通过没有参数来调用它可以观察到可观察的(例如
self.invertexpand ()
),然后通过使用值调用(例如)来写信给它。 self.invertexpand (true)
)我在此答案中包含了3个示例:
You can store one of your
WebsiteOwner
items directly in your observable. No need to use an index.Don't forget you read an observable by calling it without arguments (e.g.
self.invertExpand()
) and you write to it by calling with a value (e.g.self.invertExpand(true)
)I've included 3 examples in this answer:
1. Accordion
Here's an example for a list that supports a single expanded element:
2. Independent
If you want each of them to be able to expand/collapse independently, I suggest adding that state to an owner viewmodel:
3. Using
<details>
This one leverages the power of the
<details>
element. It's probably more accessible and by far easier to implement!