0

我在我的代码中使用淘汰赛 observables。

我的代码看起来像这样

self.allAggs = ko.observableArray();
self.aggregatedDataSource = ko.observable( new oj.ArrayTableDataSource(self.allAggs, {idAttribute: 'itemName'}) );
self.aggregatedDataSource.subscribe(function(value) {
   console.log('Value changed for aggregatedDataSource');
   console.log(ko.toJS(value));
});

要插入数据,我使用下面的代码

self.allAggs(newdata);

我这里有两个问题:

  1. 作为 newdata 的一部分传递给 self.allAggs 的数据与 UI 上显示的数据不同。

HTML 代码如下所示:

 <div id="aggregationContainer" data-bind="visible: isVisibleContainer($element.id)" class="blk" style="display:none;">
      <table id="aggTable" class="amc-full-width-table amc-max-height-table"
                       data-bind="ojComponent: {component: 'ojTable',
                        data: aggregatedDataSource,
                        display: 'grid',
                        columnsDefault: {sortable: 'enabled'}, columns: [
                        {headerText: $data.l10n_default('desktop-management.toolbar.option.',$data.selectedReportType()), field: 'itemName'},
                        {headerText: oj.Translations.getTranslatedString('desktop-management.report.column.hostCount'), renderer: hostCountRenderer, sortProperty: 'hostCount'}],
                        rootAttributes: {class:'amc-full-width-table'},
                        sort: $data.onVersionTableSort}">
       </table>
</div>
  1. 该控件永远不会进入订阅功能。

请帮助我了解我在哪里做错或遗漏了什么。

4

1 回答 1

0

无需将 ArrayTableDataSource 包装在淘汰赛 obserbavle 中。它对你没有额外的目的。

下面的代码应该可以帮助你。

self.allAggs = ko.observableArray([]); 
self.aggregatedDataSource = new oj.ArrayTableDataSource(self.allAggs, {idAttribute: 'itemName'});

您可以订阅淘汰赛可观察数组,如下所示

self.allAggs.subscribe(function(changes) {
   console.log('allAggs Value changed');
   console.log(ko.toJS(changes));
});

仅当数组中存在结构更改(例如添加/删除元素)时,才会调用上述订阅。如果任何数组元素状态发生变化,它将不会被调用。

于 2018-10-24T12:17:01.523 回答