订阅淘汰赛Observable无法正常工作

时间:2018-10-24 09:44:42

标签: knockout.js observable oracle-jet knockout-subscribe

我在代码中使用了可剔除的观测值。

我的代码如下

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. 控件永远不会进入订阅功能。

请帮助我了解我在哪里做错了或缺少什么东西。

1 个答案:

答案 0 :(得分: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));
});

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

这应该达到您的目的。希望这会有所帮助。

相关问题