如何在 Javascript 中使用 Knockout 计算分组行的小计?
我正在尝试计算所选行的分组行的小计,我可以通过循环遍历所有元素来计算每个更改总计。任何人都可以建议如何从分组行中添加或减去行值。如果我们有大量行并且选择全部会花费更多时间。这是我的示例代码 http://jsfiddle.net/Srinivasa/PAtSd/2/
I am trying to calculate sub totals for grouped row for selected rows, I am able to calculate but on every change total calculating by looping through all the elements. Can anybody suggest how to add or subtract row value from grouped row. If we have huge number of rows and select all is taking more time. here is sample code of mine http://jsfiddle.net/Srinivasa/PAtSd/2/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的方法对我来说看起来不错。如果您遇到性能问题,那么您必须有大量的行,在这种情况下您确实需要重新考虑 UI。也就是说,您实际上可以尝试通过订阅数量和选定可观察值来优化计算,并根据一本书的新旧值更改总计。下面的代码说明了这个想法,但并不是完整的解决方案。另外,标准的knockoutjs observable没有lastValue属性,所以你必须自己实现它(这里是一个如何做到这一点的示例)。
对于学生级别的小计,您需要将学生视图模型作为参数传递给书籍构造函数:
您可能需要将学生构造函数更改为如下所示:
Your approach looks good to me. If you have performance problems with it you must have really big number of rows, in which case you really want to rethink the UI. That said, you could actually try optimizing calculations by subscribing to qty and selected observables and changing your totals based on old and new values for one book. The code below illustrates the idea but is not a complete solution. Also, standard knockoutjs observable doesn't have lastValue attribute, so you would have to implement it by yourself (here is an example of how you would do it).
For student level subtotals you will need to pass student viewmodel as a parameter to book constructor:
You will probably need to change student constructor to something like this: