2016-08-03 159 views
1

我想在我的Kendo ListView模板中格式化DateTime對象,但建議的kendo.toString方法似乎不適用於我。在Kendo模板中格式化日期

我已經剪出了很多與我的問題無關的代碼,使它更容易理解。

我有一個Kendo DataSource看起來像以下:

contactDataSource: new kendo.data.DataSource({ 
    transport: { 
     read: { 
      url: "/baa/contact/getcontacts", 
      dataType: "json", 
      type: "GET" 
     } 
    }, 
    schema: { 
     model: { 
      id: "Id", 
      fields: { 
       Id: { type: "number", editable: false, nullable: true }, 
       CompanyName: { type: "string" }, 
       ContactName: { type: "string" }, 
       ContactPhone: { type: "string" }, 
       ContactEmail: { type: "string" }, 
       ImageUrl: { type: "string" }, 
       Website: { type: "string" }, 
       RecentBaas: [ 
        { 
         Name: { type: "string" }, 
         StatusDisplay: { type: "string" }, 
         Status: { type: "number" }, 
         LastModDate: { type: "date" } 
        } 
       ] 
      } 
     } 
    } 
}) 

然後,我有我的觀點一個模板,如下所示:

<script type="text/x-kendo-templ" id="contactTemplate"> 
    <div class="row"> 
     <div class="col-md-12"> 
      # for (var i = 0; i < RecentBaas.length; i++) { # 
      # if (RecentBaas[i].Status == 1) { # 
       <div class="alert alert-success" role="alert"> 
        <p>#=kendo.toString(RecentBaas[i].LastModDate, "F")#</p> 
       </div> 
      # } # 
      # } # 
     </div> 
    </div> 
</script> 

我沒有得到任何錯誤在我的控制檯中,當我加載這個頁面,但日期根本沒有格式化。例如,它仍然只顯示爲/Date(1461203814927)/

我讀過關於如何使用toString函數格式DateTime對象的Kendo文檔,並且據我所知我做的都是正確的。但也許我仍然失去了一些東西?

回答

2

請試試下面的代碼片段。

<script type="text/x-kendo-templ" id="contactTemplate"> 
    <div class="row"> 
     <div class="col-md-12"> 
      # for (var i = 0; i < RecentBaas.length; i++) { # 
      # if (RecentBaas[i].Status == 1) { # 
       <div class="alert alert-success" role="alert"> <p>#=kendo.toString(kendo.parseDate(RecentBaas[i].LastModDate), 'yyyy-MM-dd')#</p> 
       </div> 
      # } # 
      # } # 
     </div> 
    </div> 
</script> 

讓我知道,如果它不工作

+0

沒錯奏效!我不認爲我必須先解析它,因爲我可以發誓我之前沒有解析過。雖然謝謝! – Quiver