gpt4 book ai didi

javascript - 如何在不添加更多 html 的情况下单独切换 foreach div 的 knockout ?

转载 作者:行者123 更新时间:2023-12-02 21:51:01 43 4
gpt4 key购买 nike

我使用 foreach 方法为可观察数组中的每个项目创建标记,以创建 TreeView 。输出示例

类别名称1 内容 内容类别名称2 内容 内容

当我单击类别名称时,我只想显示/隐藏其内容,目前,当我单击类别名称时,它会显示并隐藏所有类别。

 var reportFilters = [
{ Text: "Campaign", Value: primaryCategories.Campaign },
{ Text: "Team", Value: primaryCategories.Team },
{ Text: "Agent", Value: primaryCategories.Agent },
{ Text: "List", Value: primaryCategories.List },
{ Text: "Inbound", Value: primaryCategories.Inbound },
{ Text: "Daily", Value: primaryCategories.Daily },
{ Text: "Services", Value: primaryCategories.Services },
{ Text: "Occupancy", Value: primaryCategories.Occupancy },
{ Text: "Data", Value: primaryCategories.Data }
];


self.showCategory = ko.observable(false);
self.toggleVisibility = function (report) {
var categoryName = report.PrimaryReportCategory;
var categoryContent = report.ID;
if (categoryName == categoryContent ) {
self.showCategory(!self.showCategory());
};
}
<div class="report-category-treeview" data-bind="foreach: $root.categories, mCustomScrollBar:true">
<ul class="column-list" >
<li class="report-category-heading" data-bind="click: $root.toggleVisibility"><span class="margin-top10" ><i class="fas fa-chevron-down"></i> <span class="report-category-name" data-bind="text: categoryName"></span></span></li>
<li id="panel" class="report-category-container" data-bind="foreach: reports, visible: $root.showCategory">
<div class="column-list-item" data-bind="click: $root.report_click, css: { 'selected': typeof $root.selectedReport() != 'undefined' && $data == $root.selectedReport() }">
<span class="column-list-text" data-bind="text: ReportName"></span>
</div>
</li>
</ul>
</div>

最佳答案

currently, when I click on the category name, it shows and hides all the categories.

这是因为 showCategory 是负责显示/隐藏的单个可观察对象。您真正想要的是每个类别显示\隐藏一个可观察的内容。

我不确定您的整个数据模型是什么样子,但既然您特别询问了类别,那么您应该创建一个类别 View 模型,可能还有一些容器 View 模型,我在这里将其命名为 master:

var categoryVM = function (name) {
var self = this;
self.name = ko.observable(name);
self.isVisible = ko.observable(false);
self.toggleVisibility = function () {
self.isVisible(!self.isVisible());
}
// ... add here your other observables ...
}

// name 'masterVM' whatever you like
var masterVM = function () {
var self = this;
self.categories = ko.observables([]);
// ... probably add here other observables, e.g. 'reports' ...
self.init = function (rawCategories) {
rawCategories.forEach(function (item) {
categories.push(new categoryVM(item.name)); // replace 'name' with your property
}
}
}

var master = new masterVM();
master.init(getCategories()); // pass in your categories from wherever they come from
ko.applyBindings(master);

然后,在您的 html 中,这将是您的外部 foreach:

<div class="report-category-treeview" data-bind="foreach: categories ... />

和您的 li(为了简洁起见,我省略了 li 下的嵌套标签):

<li class="report-category-heading" 
data-bind="click: toggleVisibility">
<li id="panel" class="report-category-container"
data-bind="foreach: $root.reports, visible: isVisible">

关于javascript - 如何在不添加更多 html 的情况下单独切换 foreach div 的 knockout ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60132074/

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