剔除koGrid中的计算值 [英] Computed values in knockout koGrid

查看:69
本文介绍了剔除koGrid中的计算值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用一个网格,其中一列具有基于网格中其他列的值的计算值...

I need to use a grid in which one column has a computed value based on values from other columns in the grid ...

作为示例需求,我有一列显示年龄,这是一个可编辑的列...如果更改了年龄,则下一列应计算值100-更新的年龄...

As a sample requirement I have one column which shows the age this is a editable column ... if the age is changed the next column should compute the value of 100 - the updated age ...

我在这里创建了一个jsfiddle来演示我的需求 JSFiddle

I have created a jsfiddle to demo my requirement here JSFiddle

{field: 'computedAge', displayName: '100 -Age',cellTemplate:self.calculatedCellTemplate}

是我希望在更新年龄列时填充的计算列

is the computed column that i wish to populate when the age column is updated

有人可以建议我如何实现这一目标吗?

Can anyone suggest how i could achieve this ?

推荐答案

您只需两个步骤即可实现:

You can achieve this by only two steps:

(1)为其中包含computedAge计算属性的数据列表中的每个项目创建实例化函数.

(1) Create instantiation function for every item in your datalist with computedAge computed property within it.

function Item(data) {
    this.name = ko.observable(data.name);
    this.age = ko.observable(data.age);
    this.computedAge = ko.computed(function(){
        return 100 - this.age();
    }, this);
}

(2)映射源数组以创建实例,而不是简单的observableArray创建.

(2) Map source array to create instances instead of simple observableArray creation.

self.browsers = ko.observableArray(
    ko.utils.arrayMap(
        datastore.initialData,
        function(data){ return new Item(data); }
    )
);

工作示例: http://jsfiddle.net/xp6xa/

更新: 要获得自我更新的单元格,请不要忘记像这样定义您的self.calculatedCellTemplate:

Update: To get self-updating cell do not forget to define your self.calculatedCellTemplate like this:

    self.calculatedCellTemplate = '<span data-bind="text: $parent.entity.computedAge"></span>';

http://jsfiddle.net/xp6xa/3/

这篇关于剔除koGrid中的计算值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆