gpt4 book ai didi

javascript - KnockoutJS - 如何使用 Observable 数组在 foreach 中隐藏某些元素?

转载 作者:行者123 更新时间:2023-12-05 03:25:18 25 4
gpt4 key购买 nike

我有一个网站所有者列表。我正在尝试构建一个 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>

最佳答案

您可以存储您的 WebsiteOwner 之一直接在您的可观察对象中的项目。无需使用索引。

不要忘记通过不带参数调用它来读取一个可观察对象(例如 self.invertExpand<b>()</b> )并通过调用一个值(例如 self.invertExpand<b>(true)</b> )写入它

我在这个答案中包含了 3 个示例:

  • 只允许使用 knockout 打开单个详细信息
  • 一个允许使用 knockout 独立打开和关闭所有细节的工具
  • 一个不使用 knockout 而是使用纯 HTML 的人 🙂

1。 Accordion

下面是支持单个扩展元素的列表示例:

const websiteOwners = [
{ name: "Jane", role: "Admin" },
{ name: "Sarah", role: "Employee" },
{ name: "Hank", role: "Employee" }
];

const selectedOwner = ko.observable(null);

const isSelected = owner => selectedOwner() === owner;
const toggleSelect = owner => {
selectedOwner(
isSelected(owner) ? null : owner
);
}

ko.applyBindings({ websiteOwners, isSelected, toggleSelect });
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

<ul data-bind="foreach: { data: websiteOwners, as: 'owner' }">
<li>
<span data-bind="text: name"></span>
<button data-bind="
click: toggleSelect,
text: isSelected(owner) ? 'collapse' : 'expand'"></button>

<div data-bind="
visible: isSelected(owner),
text: role"></div>
</li>
</ul>

2。独立

如果您希望它们中的每一个都能够独立展开/折叠,我建议将该状态添加到所有者 View 模型中:

const websiteOwners = [
{ name: "Jane", role: "Admin" },
{ name: "Sarah", role: "Employee" },
{ name: "Hank", role: "Employee" }
];

const OwnerVM = owner => ({
...owner,
isSelected: ko.observable(null),
toggleSelect: self => self.isSelected(!self.isSelected())
});

ko.applyBindings({ websiteOwners: websiteOwners.map(OwnerVM) });
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

<ul data-bind="foreach: websiteOwners">
<li>
<span data-bind="text: name"></span>
<button data-bind="
click: toggleSelect,
text: isSelected() ? 'collapse' : 'expand'"></button>

<div data-bind="
visible: isSelected,
text: role"></div>
</li>
</ul>

3。使用 <details>

这个利用了 <details> 的力量元素。它可能更易于访问并且更容易实现!

const websiteOwners = [
{ name: "Jane", role: "Admin" },
{ name: "Sarah", role: "Employee" },
{ name: "Hank", role: "Employee" }
];

ko.applyBindings({ websiteOwners });
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

<ul data-bind="foreach: websiteOwners">
<li>
<details>
<summary data-bind="text: name"></summary>
<div data-bind="text: role"></div>
</details>
</li>
</ul>

关于javascript - KnockoutJS - 如何使用 Observable 数组在 foreach 中隐藏某些元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72056025/

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