- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 KnockoutJS 来创建工作申请网站,并使用 JS 中的 getJSON 方法。
不幸的是,我得到了这个结构:
办公室
纽约
华盛顿
洛杉矶
我使用JS中的filter
函数过滤掉了一些尚未开放的城市的办事处,效果很好。
但现在我需要过滤除物流之外的所有部门,因为我只想显示特定城市的物流工作。我希望它是动态的,因此即使有更多部门,它也只会显示后勤。
我找不到好的解决方案。有什么想法吗?
编辑:这是虚拟 JSON:
最佳答案
既然你对工作感兴趣,我建议做一个Job
模型将当前仅由结构定义的数据合并到一个方便的对象中。
为了展平数据,您执行一组 reduce
行动:
const jobData={offices:[{location:"ny",departments:[{name:"Logistics",jobs:[{title:"driver for x"},{title:"driver for y"}]},{name:"Finance",jobs:[{title:"CFO"}]}]},{location:"la",departments:[{name:"Logistics",jobs:[{title:"driver for z"}]},{name:"IT",jobs:[{title:"tech support manager"}]}]}]}
const Job = (title, department, officeLocation) => ({
title,
department,
officeLocation
});
const JobList = ({ offices }) => ({
jobs: offices.reduce(
(allJobs, { location, departments }) => departments.reduce(
(allJobs, { name, jobs }) => jobs.reduce(
(allJobs, { title }) => allJobs.concat(
Job(title, name, location)
),
allJobs
),
allJobs
),
[]
)
})
console.log(JobList(jobData))
现在我们已经整理好了数据格式,我们可以开始编写 knockout 代码了。
我创建了一个 table
呈现计算出的作业列表。在计算中,我们过滤 2 个属性:所需的办公室和所需的部门。
过滤器本身是“平坦的”,因为 Job
对象拥有我们需要的所有数据。例如,物流过滤器可以应用如下:
const logisticsJobs = ko.pureComputed(
jobList().filter(job => job.department === "logistics")
);
这是示例。使用<select>
表头中的元素以应用过滤器。
function JobFinder() {
const jobData = ko.observable({ offices: [] });
const jobList = ko.pureComputed(
() => JobList(jobData())
);
// Lists of properties we can filter on
this.offices = ko.pureComputed(
() => uniques(jobList().map(job => job.officeLocation))
);
this.departments = ko.pureComputed(
() => uniques(jobList().map(job => job.department))
);
// Filter values
this.requiredOffice = ko.observable(null);
this.requiredDepartment = ko.observable(null);
// Actual filter logic
const officeFilter = ko.pureComputed(
() => this.requiredOffice()
? job => job.officeLocation === this.requiredOffice()
: () => true
);
const departmentFilter = ko.pureComputed(
() => this.requiredDepartment()
? job => job.department === this.requiredDepartment()
: () => true
);
const allFilters = ko.pureComputed(
() => [ officeFilter(), departmentFilter() ]
)
const filterFn = ko.pureComputed(
() => job => allFilters().every(f => f(job))
)
// The resulting list
this.filteredJobs = ko.pureComputed(
() => jobList().filter(filterFn())
);
// To load the data (can be async in real app)
this.loadJobData = function() {
jobData(getJobData());
}
};
// Initialize app
const app = new JobFinder();
ko.applyBindings(app);
app.loadJobData();
// utils
function uniques(xs) { return Array.from(new Set(xs)); }
// Code writen in the previous snippet:
function getJobData() {
return {offices:[{location:"ny",departments:[{name:"Logistics",jobs:[{title:"driver for x"},{title:"driver for y"}]},{name:"Finance",jobs:[{title:"CFO"}]}]},{location:"la",departments:[{name:"Logistics",jobs:[{title:"driver for z"}]},{name:"IT",jobs:[{title:"tech support manager"}]}]}]};
};
function Job(title, department, officeLocation) {
return {
title,
department,
officeLocation
}
};
function JobList({ offices }) {
return offices.reduce(
(allJobs, { location, departments }) => departments.reduce(
(allJobs, { name, jobs }) => jobs.reduce(
(allJobs, { title }) => allJobs.concat(
Job(title, name, location)
),
allJobs
),
allJobs
),
[]
)
};
th {
text-align: left;
width: 30%
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<table>
<thead>
<tr>
<th>Job Title</th>
<th>Location</th>
<th>Department</th>
</tr>
<tr>
<th></th>
<th>
<select data-bind="
options: offices,
value: requiredOffice,
optionsCaption: 'Show all locations'">
</select>
</th>
<th>
<select data-bind="
options: departments,
value: requiredDepartment,
optionsCaption: 'Show all departments'">
</select>
</th>
</tr>
</thead>
<tbody data-bind="foreach: filteredJobs">
<tr>
<td data-bind="text: title"></td>
<td data-bind="text: officeLocation"></td>
<td data-bind="text: department"></td>
</tr>
</tbody>
</table>
关于javascript - KnockoutJS 按特定属性过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54024386/
我已经使用 knockout js 实现了一个简单的 spa,您可以在其中在多个页面之间导航,每个页面都呈现一个 View 模型。菜单是一个 2 级菜单,因此我有主页面和子页面。我用knockout的
我想使用组件作为表格行模板,但似乎找不到方法,这可能吗? 当我执行以下操作时,它不会将组件放在 tbody 中,而是将其放在表格上方。 组件 模板 最佳答案
我希望能够将单击和双击事件绑定(bind)到一段文本。我知道我可以使用 data-bind ="event: { dblclick: doSomething } 双击,但我还需要能够在单击时执行不同的
我是否可以告诉 knockout 映射插件订阅所有属性更改调用某个函数? 我意识到我可以通过这种方式手动订阅属性更改事件: var viewModel = { name: ko.observa
我想要一个可见或不可见,具体取决于我的 javascript 中的 bool 值。 我的 HTML 是: Remove
我想将此 json 映射到自定义对象。问题是项目不是 typeof Item 对象而是普通对象。我在这里缺少什么? 你可以在这里测试:http://jsfiddle.net/5jhpE/ var js
我有一个 self 描述的定义如下: var my_data = { types: { typeA: {fieldX: { type: "string"}}, typeB:
首先看一下: http://pastebin.com/823NMiWc 这是 Knckoutjs + Jquery Raty 插件 假设这些是数据库中的列: story_rev: ko.observa
我正在使用 knockout.js 并尝试设置我的选择绑定(bind)的默认值(不使用 optionCaption),并在有人更改它时捕获该值。
我目前有一个数字框,用户可以在其中输入数字 1、2 或 3,并在此基础上设置一些条件格式。 但是,我希望数字字段成为一个下拉列表,其中显示“短”、“长”和“非常长”之类的内容,但在幕后,传递的实际值是
我正在寻找一些有关如何创建代表我的评论系统中的帖子的模型的指南。本质上,它是 Facebook 的简化版本,其中有不同用户的帖子,每个帖子都有零个或多个评论。亲子关系始终只有一层。 创建 Post 对
我需要为数组的每个元素计算一个可观察值。此计算可观察量的解析需要依赖于每个数组元素上下文中存在的其他属性。 请检查以下示例案例: 这是 KnockoutJS foreach 绑定(bind)到嵌套数组
我在网格中有一个嵌套的组产品选项数组。我想要一个弹出编辑器,列出每个分组产品选项的所有产品(产品选项行),并允许用户检查它们之间的关系。我遇到过多对多关系的示例,但没有看到 self 引用分组多对多的
我正在使用KnockoutJs构建一个搜索列表,代码如下: HTML: 部分Js搜索功能: this.name = ko.observable(''); this.query = ko
我是 KnockoutJS 新手。我有一个按以下方式工作的应用程序: 加载页面时,复杂的数据结构会传递到前端 该数据结构被分成更小的 block ,这些数据 block 被传递给组件 用户与组件交互以
我有一个 ASP.Net MVC 网站,在 View 中使用 KnockoutJS 和 KOGrid。它动态地呈现某一特定列中的超链接,如下所示: cellTemplate: '' 已决定,单击时,浏
我正在使用javascript和knockoutjs来使用viewmodel实现搜索过滤器。我无法让搜索过滤器工作。下面是我的js文件 //Object Constructor Class for L
我使用 KnockoutJS 来创建工作申请网站,并使用 JS 中的 getJSON 方法。 不幸的是,我得到了这个结构: 办公室 纽约 部门 金融 职位 示例 ... IT 物流 营销 华盛顿 部门
我在这里创建了一个问题示例: http://jsfiddle.net/JustinN/qWeLT/1/ 我的实际代码已连接到 ASP.NET Web 方法,因此示例代码已调整为指向公共(public)
在过去的几天里,我对 Knockoutjs 越来越感兴趣。它看起来非常有前途,因为它对 MVVM 模式和 WPF 进行了类似绑定(bind)的建模,但每当它为非 RIA Web 应用程序带来一些有用的
我是一名优秀的程序员,十分优秀!