- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发 web application
使用Asp.Net Mvc
其中我使用 Knockout Js 通过对数据执行一些操作来检索数据并将其发送到 Html View。
例如,我的数组中有以下数据,名为 datainput
Grade Type Volume Price
A Buy 1000 10
A Sell 1200 10
B Sell 100 100
我正在计算一个数组,当在 html 页面上显示时,它只需要一个不同的 Grade 值并将数据分组到该等级下,例如
A
Buy 1000 10
Sell 1200 10
B
Sell 100 100
使用上面的函数如下
self.uniqueSelect = ko.dependentObservable(function () {
var Transactiontypes = ko.utils.arrayMap(self.datainput(), function (item) { return item.Grade })
return ko.utils.arrayGetDistinctValues(Transactiontypes);
});
我不认为上面的脚本有问题,虽然不确定,但我继续使用从上面的脚本获得的数据,并尝试将其填充到 html,如下所示
<!--ko foreach: uniqueSelect-->
<tr>
<td class="clientproductHeader" data-bind="text: $data"></td>
<td class="clientproductHeader" colspan="10"></td>
</tr>
<tbody data-bind="foreach: ko.observableArray($root.datainput()).extendsdistinct('Grade').index.Grade()[$data]">
<tr>
<script type="text/html" id="read-template">
<td data-bind="text: Type"></td>
<td data-bind="text: Volume"></td>
<td data-bind="text: (typeof Price() === 'number') ? Price().toFixed(2) : '' "></td>
</script>
</tr>
</tbody>
<!--/ko-->
</table>
由于某些原因,上述脚本会显示重复的数据,例如,如果一个成绩重复两次,如示例数据所示,它会显示该成绩的相同数据两次,如果重复三次,则显示三次,依此类推。
我认为错误出在<!--ko foreach: uniqueSelect-->
因为它只是根据相似成绩的数量循环数据。
html 页面上的结果类似于
A
Buy 1000 10
Sell 1200 10
B
Sell 100 100
A
Buy 1000 10
Sell 1200 10
在上面的数据中,A 级重复了两次,因此 A 级的所有数据都重复了两次,而 B 级只有一个条目,因此只发生了一次我收到的数据屏幕不知道如何处理这个问题
最佳答案
我仔细阅读并编写了一个版本,该版本可以完成我认为您想要完成的任务。它使用 Pēteris 描述的方法。 See it in action .
您希望 View 代码尽可能简单,并且在返回 $root
时也要小心。如果您确定要使用过滤器来执行此操作,请创建一个执行如下类似分组的过滤器,然后迭代其每个条目/子项。另外,尽量避免在 View 中声明可观察量。
最后,您可以将 groupByGrade()
和 getGrades()
合并到一个函数中,该函数返回一个对象,该对象具有每个结果的属性。这将节省一个迭代周期。
View :
<tr>
<td class="clientproductHeader" data-bind="text: 'Grade ' + $data + ' (' + $root.log()[$data].length + ' trades)'"></td>
<td class="clientproductHeader" colspan="10"></td>
</tr>
<tbody data-bind="foreach: $root.log()[$data]">
<tr>
<td data-bind="text: type"></td>
<td data-bind="text: volume"></td>
<td data-bind="text: (typeof price === 'number') ? price.toFixed(2) : '' "></td>
<td class="actioncells">
<a class="btn btn-success" title="Edit" data-bind="click: $root.edit">Edit</a>
</td>
<td class="actioncells">
<a class="btn btn-danger" title="Delete " data-bind="click: $root.remove">Delete</a>
</td>
</tr>
</tbody>
<!--/ko-->
和 JavaScript:
function testViewModel() {
// Underscore and Lo-dash both have a function to group
// an object by key. That's what you want. Here is a
// really simple version.
function groupByGrade(data) {
return data.reduce(function(last, next) {
if (last[next.grade]) {
last[next.grade].push(next);
} else {
last[next.grade] = [next];
}
return last;
}, {});
}
// Get a unique list of the grades in the data to iterate over
function getGrades(data) {
return data.reduce(function(last, next) {
return !!~last.indexOf(next.grade) ? last : last + next.grade;
}, '').split('');
}
var rawData = [
{
grade: 'A',
type: 'Buy',
volume: 1000,
price: 10
}, {
grade: 'A',
type: 'Sell',
volume: 1200,
price: 10
}, {
grade: 'B',
type: 'Sell',
volume: 100,
price: 100
}
];
console.log(getGrades(rawData));
this.log = ko.observable(groupByGrade(rawData));
this.grades = ko.observableArray(getGrades(rawData));
}
ko.applyBindings(new testViewModel());
关于javascript - knockout 可观察数组中的错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27533592/
我想认真学习更多关于 ES6 的知识。我一直在网上做一些例子,虽然我得到了大部分例子,但有时我什至不知道从哪里开始。请注意,我对这个 ES6 和 Knockout 的东西 super 菜鸟,想通过从他
我正在使用 knockout.mapping 将我的 javascript 对象转换为可观察到的 knockout 。在这个 javascript 对象内部是一个嵌套对象,并且这个嵌套对象正在被转换,
我正在用 Kendo、Knockout 和 kendo-knockoutjs 库做一些实验。我想将 knockout View 模型与剑道数据源一起使用,并将其绑定(bind)到剑道网格小部件。 在剑
有谁知道现在当 jQuery.tmpl is not being actively developed or maintained anymore 时 Knockout 的默认模板引擎会发生什么? K
我面临一个问题,即添加新项目时计算的可观察数组未更新。 self.FilteredCityList = ko.computed(function() { var filteredCollectio
我想对数组中的多个属性使用 this fiddle 中的 ko.utils.arrayGetDistinctValues,因此我将数组映射到我想要的两个属性的数组 viewModel.justCate
我在使用 Knockout 的映射插件时遇到了一个奇怪的问题。 如果我通过映射填充一个 observable 数组,我将无法迭代该数组或获取其长度,即使 UI 已正确更新,该数组似乎也是空的。 你可以
所以我试图让 knockout 与 summernote 打得很好。它并没有真正起作用。我意识到这是因为 Summernote 使用了 而不仅仅是一个输入字段。 我的绑定(bind)是这样的: ko.
另一个我似乎无法找到帮助的 knockout 问题。 我基本上是在尝试实现级联下拉列表。前几天我请求帮助了解如何解析我的复杂 JSON(它来自 cakePHP Controller 。前几天我收到的帮
我在尝试使用映射插件将项目更新为 observableArray 的一部分时遇到问题。 我有以下代码 accounts.list = ko.mapping.fromJS(@(Html.Raw(Mode
有几件事我不明白。 根据 KO 文档(以及关于 SO 的许多帖子),在查询和编写 observable 时应该使用括号。但是在绑定(bind)子属性时,在我看来,是否使用括号并不重要。 = 都返回
我从 WCF 接收数据并将其绑定(bind)到表。我在这个论坛上得到了帮助,添加了一些计算字段,一切都很完美。我想在此表的页脚添加总计。我的页面的简单版本可以在 http://jsfiddle.net
我有一个使用 Knockout 显示过滤列表的页面。 View 模型有一个对象数组,以及一个使用 ko.utils.arrayFilter 来过滤数组的 ko.computed。 有时过滤会变得非常复
我正在使用 jquery quicksearch 来搜索由 knockout foreach 循环填充的表。 需要启动快速搜索元素 foreach 结束后 . 我尝试了几种方法,但到目前为止都没有成功
我正在尝试使用类似于 silverlight 中的数据表单的数据网格来创建数据表单功能。 我有一个 div 来显示从网格中选择的项目: $('#readDate').attr('data-bind',
我有一个带有分页功能的 View 模型,它维护 pageIndex 属性。 Viewmodel 有一个包含页面的页面数组。每个页面都有自己的 pageNumber 属性。 我的 View 模型的分页代
名称+复选框的单向绑定(bind)工作正常,但它最初不适用于单选按钮employeeTypeA,尽管它在viewmodel中的值为true,html显示单选按钮未设置,为什么会这样?
我正在尝试解决ko映射的一个小问题。场景是我的ViewModel基本上是对象的集合。每个对象都是通过json调用创建的,方法是: var ViewModel = ko.observableArray(
使用 computed 的主要区别是什么?函数和 subscribe在knockout.js 中的功能? 最佳答案 计算出的 observable 通常用于返回计算出的值。作为计算评估的一部分访问的任
我有带有项目验证的ko.observableArrays。用户可以将项目标记为已删除。将其标记为已删除时,我需要在该项目上禁用验证。 您如何动态禁用验证? 示例:http://jsfiddle.net
我是一名优秀的程序员,十分优秀!