2014-12-30 97 views
1

只有滿足特定的條件(例如該行有特定值的字段),纔有可能在Kendo UI網格中使用行模板?如果沒有滿足這個條件,那麼它不應該渲染模板,而只是正常渲染行。有條件的模板kendo ui

我不想在模板本身內指定條件,因爲除非我弄錯了,否則如果條件不滿足,我還必須在模板定義中包含「默認」html。

這是我試圖實現的一個例子,它不起作用。爲了簡潔我省略了不相關的我的問題其他網格屬性:

$("#divGrid").kendoGrid({ 
    rowTemplate: function (data) { 
     if (condition) kendo.template($("#myRowTemplate").html(data)); 
     // else render row without the template, but how? 
    } 
}); 

回答

1

首先,kendo.template返回功能需要被調用(與作爲參數模板數據)爲了返回HTML代碼。所以,你的榜樣的工作,它需要像這樣修改:

$("#divGrid").kendoGrid({ 
    rowTemplate: function (data) { 
     if (condition) { 
      return kendo.template($("#myRowTemplate").html())(data); 
     } // else render row without the template, but how? 
    } 
}); 

現在,遺憾的是沒有辦法「使該行正常」因爲你已經指定rowTemplate功能。您只能指定模板(或字符串),它需要顯示在其他條件:

$("#divGrid").kendoGrid({ 
    rowTemplate: function (data) { 
     if (condition) { 
      return kendo.template($("#myRowTemplate").html())(data); 
     } else { 
      return '<tr>Normal row</tr>'; 
      // or return kendo.template($("#myRowTemplate2").html())(data) 
      // or return "<tr>" + data.name + ": " + data.age + "</tr>" 
     } 
    } 
}); 

希望這有助於。

+0

這是不幸的,但感謝您的回覆,並解決了缺少參數的問題。標記爲正確的答案。 – aw1975