2013-09-23 122 views
2

我有一個加載在document.ready()上的kendo數據源,並用來自JSON請求的結果填充組合框。然後,一旦用戶選擇組合框中的一個項目,我告訴有一個函數customerSelected()執行來爲該客戶加載值。我遇到的問題是我無法在頁面加載後設置另一個Kendo數據源來填充數據。如果我使用常規的Jquery ajax調用,那麼在執行Kendo模板之後數據似乎會加載並且不會填充我的數據。加載頁面後的Kendo數據源

function customerSelected(customerid) { 
    var customer = customerid; 

    var commTemplate = kendo.template(' # for (var i=0; i < data.length; i++) { # <div class="communication" contentEditable>#= data[i].fname # - #= data[i].lname # #= data[i].details #</div> # } # '); 
    var commData = new kendo.data.DataSource({ 
     transport: { 
      read: { 
       url: "index.php?mode=showCustomerData", 
       dataType: "json" 
      } 
     } 
    }); 

    $("#communications").html(commTemplate(commData)); 
} 

$('document').ready(function() { 

    var customers = new kendo.data.DataSource({ 
     transport: { 
      read: { 
       url: "index.php?mode=showCustomers", 
       dataType: "json" 
      } 
     } 
    }); 
    $("#customerBox").kendoComboBox({ 
     dataSource: customers, 
     dataTextField: "name", 
     dataValueField: "customer_id", 
     filter: "contains", 
     change: function(e) { 
      console.log(this.value()); 
      customerSelected(this.value()); 
      $("#customerSelected").val(this.value()); 

     } 
    }); 


}); 

此處的每個數據源均會返回正確的JSON數據,這些數據與其他客戶端進行了驗證。問題在於customerSelected()函數中的kendo.data.DataSource()實際上沒有做任何事情。我嘗試了各種方法來獲得這個功能,我習慣於在純粹的JQuery世界中進行Ajax調用和附加/更新DOM。在DOM被加載後,我在這裏丟失了什麼以允許另一個DataSource?

回答

2

你的問題是:

  1. 你沒有創建數據源後讀取數據。

  2. 數據異步加載,所以你不能馬上使用它。

試試這個:

function customerSelected(customerid) { 
    var customer = customerid; 

    var commTemplate = kendo.template(' # for (var i=0; i < data.length; i++) { # <div class="communication" contentEditable>#= data[i].fname # - #= data[i].lname # #= data[i].details #</div> # } # '); 
    var commData = new kendo.data.DataSource({ 
     transport: { 
      read: { 
       url: "index.php?mode=showCustomerData", 
       dataType: "json" 
      } 
     } 
    }); 

    // run this callback the next time data changes 
    // which will be when the data is done being read from the server 
    commData.one("change", function() { 
     $("#communications").html(commTemplate(commData.data())); 
    }); 

    // read the data from the server 
    commData.fetch(); 
} 
1

嘗試:

var commTemplate = kendo.template(' # for (var i=0; i < data.length; i++) { # <div class="communication" contentEditable>#= data[i].fname # - #= data[i].lname # #= data[i].details #</div> # } # '); 
var commData = new kendo.data.DataSource({ 
transport: { 
    read: { 
     url: "index.php?mode=showCustomerData", 
     dataType: "json" 
    } 
}, 
change: function() { 
    $("#communications").html(kendo.render(commTemplate, this.view()); 
} 
}); 

commData.read(); 

來自:http://demos.kendoui.com/web/datasource/remote-data.html

+0

這是有益的,但它不填充數據。我看到DataSource實際上向服務器發出了請求,Chrome網絡開發者選項卡顯示結果:[{「id」:「1」,「fname」:「Keiser」,「lname」:「Von」,「details」 :「這些都是一些細節」}]但數據不會在頁面上迭代。我能夠讓模板引擎來做迭代的數據,當我嘗試使用jquery,但數據字段沒有正確分配 – Keizer

+0

也許我需要定義模式? console.log()報告{「options」:{「data」:[],「schema」:{},「serverSorting」:false,「serverPaging」:false,「serverFiltering」:false,「serverGrouping」 「serverAggregates」:假的, 「一批」:假的, 「運輸」:{ 「讀」:{ 「URL」: 「的index.php模式= showCustomerData」, 「數據類型」: 「JSON」}}}, 「_地圖」 :{}, 「_預取」:{}, 「_數據」:[], 「_ pristineData」:[], 「_範圍」:[], 「_視圖」:[], 「_原始」:[], 「_毀」: [], 「_基團」:[], 「_事件」:{ 「同步」:[空]}, 「運輸」:{ 「選項」:{ 「讀」:{ 「URL」?「的index.php模式= showCustomerData「,」dataType「:」json「}},」cache「:{}},」reader「:{},」_ requestInProgress「:true} – Keizer

+0

進一步修補程序我能夠獲取數據, :function(){$(「#communications」)。html(commTemplate(this._pristine)); } – Keizer

0

$( 「#通信」)HTML(commTemplate(this._pristine));

+2

添加關於爲什麼解決這個問題的解釋將有助於OP學習。 –

+0

這比堅持劍道的約定更像是一種破壞。只有它的原因是因爲我在內部拋棄了DataSource的輸出,發現this._pristine有我期待的結果數據。它應該比這更直接。 DataSource.data對象沒有被填充,但DataSource._pristine是,當在Google上搜索時我找不到與_pristine相關的任何東西 – Keizer

+0

看看你的原始代碼,在我看來,問題是你試圖填充剛剛定義數據源之後的數據,但不能保證數據已被接收。嘗試在你的服務(index.php)中添加一個延遲,看看它是否仍然運行...我會說,除非你在等待接收數據的回調中運行這段代碼,否則它將失敗。除非你想依賴KendoUI內部構件,並且在實現過程中將來會發生變化:除了'_pristine',而不是'data()'方法。 – OnaBai