2

我想要一个带有计算列的简单 jqxGrid。看起来一切正常,但它不起作用。简单的例子:

<script type="text/javascript">
$(document).ready(function () {
    var vm = {
        date: ko.observable(new Date()),
        items: ko.observableArray(),
        load: function () {
            for (var i = 0; i < 10; i++) {
                var item = {
                    x: ko.observable(i),
                    y: ko.observable(i + 1)
                };
                item.sum = ko.computed(function() { return this.x() + this.y(); }, item);
                this.items.push(item);
            }
        }
    };
    ko.applyBindings(vm);
});
</script>

<input data-bind="click: load, jqxButton: {theme: 'metro'}" type="button" value="Load" />
<div data-bind="jqxGrid: {source: items, disabled: false, autoheight: true,
                editable: true,
                selectionmode: 'singlecell',
                theme: 'metro',
                columns: [
                { text: 'X', dataField: 'x' },
                { text: 'Y', dataField: 'y' },
                { text: 'Sum', dataField: 'sum'}
                ]}" id="jqxgrid">
</div>
<table style="margin-top: 20px;">
<tbody data-bind="foreach: items">
    <tr>
        <td data-bind="text: x"></td>
        <td data-bind="text: y"></td>
        <td data-bind="text: sum"></td>
    </tr>
</tbody>
</table>

这种情况正在发生:我能够更新 x 或 y,并且在下表中看到了新值,但 Sum 字段在第一次加载后永远不会更新。

4

1 回答 1

0

Your .sum property is a computed value, but jqxgrid has no idea about computes or other observable values. Therefore, it has no idea that it needs to update.

You could trigger an update yourself by subscribing to the computed and calling .updatebounddata on the grid:

vm.sum.subscribe(function() {
    $('#idOfYourGridHere').trigger( 'updatebounddata' );
}

I see you're using a custom knockout binding called jqxgrid. If you like, you could probably modify that binding so that it updates properly.

于 2012-12-03T19:58:47.263 回答