2015-09-25 41 views
0

我有一个可观察的项目创建组件的数组与参数。KnockoutJS组件与KendoUI图表只适用于最后一个图表

在每个组件中,我使用该参数来查询数据(通过AJAX)。我将这些数据返回到Observable数组中,并使用该数组作为KendoUI图表的数据源。

<csglist params="account:'08167526'"></csglist> 
<hr> 
<csglist params="account:'0873458'"></csglist> 
<hr> 
<csglist params="account:'0828337'"></csglist> 
<hr> 
<csglist params="account:'086778'"></csglist> 

调用获取数据

 getCategory = function() { 
      self.categoryChart([]); 
      xhr_get(cont.publish + 'api/dbmain/getCategory', { 'id': params.account }).done(function (allData) { 
       var mappedLogs = $.map(allData, function (item) { return new categoryData(item) }); 
       self.categoryChart(mappedLogs); 
       buildChart(); 
      }) 
     } 
function buildchart(){ 

    $(document).ready(createChart); 
    $(document).bind("kendo:skinChange", createChart); 
} 

的创建图表

function createChart() {  
     $("#" + self.act() + "chart").kendoChart({ 
     dataSource: { 
      data: ko.toJS(self.categoryChart) 
      //, sort: { field: "category", dir: "asc" } 
     }, 
     title: { 
      text: self.type() 
     }, 
     legend: { 
      visible: true, 
      position: "bottom" 
     }, 
     seriesDefaults: { 
      type: "bar", 
      stack: true 
     }, 
     series: [{ 
      field: "sales", 
      name: "Current Sales", 
      color: "#66110F" 
     }, { 
      field: "opp", 
      name: "Opportunity", 
      color: "#E65F5B" 
     }], 
     valueAxis: { 
      // max: 180, 
      line: { 
      visible: false 
      }, 
      minorGridLines: { 
      visible: true 
      }, 
      visible: false 
     }, 
     categoryAxis: { 
      field: "category", 
      majorGridLines: { 
      visible: false 
      } 
     }, 
     tooltip: { 
      visible: false, 
      template: "#= series.name #: #= value #" 
     } 
     }); 
    } 

但是当所有创建的组件:用一个参数创建的组件列表中

例,只有最后一个有数据。

谁能告诉我为什么会发生这种情况?

+0

你应该粘贴你正在填充数组的代码部分,因为它似乎存在问题。 – Buzinas

+0

你做了一个邪恶的jQuery和Knockout联盟。你需要一个绑定处理程序。 http://juliameyer.github.io/knockout-kendo/dataviz/Chart.html –

+0

Roy J你说,因为我在数组中使用这个绑定,我需要创建一个绑定句柄来应用它? – user1813251

回答

0

当我将buildChart = function更改为self.buildChart = function时,它可以工作!

相关问题