2014-04-22 35 views
1

所以在後端我使用PHP和這裏是如何字符串格式化:劍道HTML字符串得到HTML格式ヶ輛

$temp['photos'] = html_entity_decode($HTMLformatedImg); 

,並響應它被格式化好:

"photos":"<img src='url/test1.jpg'><img src='url/test2.png'>" 

當我嘗試顯示給用戶使用:

dataSourceDeals = new kendo.data.DataSource({ 
    //serverPaging: true, 
    serverSorting: true, 
    transport: { 
     read: { 
      url: crudServiceBaseUrlDeals + "read&businessId={/literal}{$details.id}{literal}", 
      dataType: "jsonp" 
     }, 
     update: { 
      url: crudServiceBaseUrlDeals + "update&businessId={/literal}{$details.id}{literal}", 
      dataType: "jsonp" 
     }, 
     destroy: { 
      url: crudServiceBaseUrlDeals + "destroy&businessId={/literal}{$details.id}{literal}", 
      dataType: "jsonp" 
     }, 
     create: { 
      url: crudServiceBaseUrlDeals + "create&businessId={/literal}{$details.id}{literal}", 
      dataType: "jsonp" 
     }, 
    }, 
    batch: false, 
    pageSize: 10, 
    schema: { 
     total: "total", 
     data: "data", 
     model: { 
      id: 'id', 
      fields: { 
       id:     { type: "number", editable: false }, 
       dealName:   { type: "string" }, 
       photos:    { type: "string" }, 
       description:   { type: "string" }, 
       active:   { type: "string" } 
      } 
     }   
    } 
});  

我得到的文本顯示爲結果。當我試圖檢查我得到這個文本

&lt;img src='url/test1.jpg'&gt;&lt;img src='url/test2.png'&gt; 

而我不知道它發生了什麼女巫點,爲什麼。

我正在使用最新版本的Kendo UI。

編輯

$("#deals").kendoGrid({ 
      dataSource: dataSourceDeals, 
      pageable: true, 
resizable: true, 
toolbar: [{ text:"Add Deal", className: "gridAddDeal"}, { text:"Edit Selected", className: "gridEditDeal"}, { text:"Delete Selected", className: "gridDeleteDeal"}], 
      height: 400, 
      sortable: 'true', 
selectable: true, 
      columns: [  
       { field: "id", title: "ID", width: "40px" }, 
       { field: "dealName", title: "Coupon Name", width: "100px" }, 
       { field: "photos", title: "Photos", width: "100px" }, 
       { field: "description", title: "Description", width: "100px" }, 
       { field: "active", title: "Active", width: "70px" } 
      ]   
     }); 
+0

'html_entity_decode'是怎麼回事。 – Ohgodwhy

+0

我實際上正在使用它來試圖阻止它。 [文檔](http://www.php.net/manual/en/function.html-entity-decode.php) –

+1

向我們展示如何使用照片字段。 –

回答

3

不知道這是一個缺陷或功能,但你可以很容易地解決它定義模板photos場爲template: "#= photos #"

columns: [  
    { field: "id", title: "ID", width: "40px" }, 
    { field: "dealName", title: "Coupon Name", width: "100px" }, 
    { field: "photos", title: "Photos", width: "100px", template: "#= photos #" }, 
    { field: "description", title: "Description", width: "100px" }, 
    { field: "active", title: "Active", width: "70px" } 
]   

看到它在這裏: http://jsfiddle.net/OnaBai/H6dD5/

從我的角度來看,這是一個功能,因爲這個我是能夠打印特殊字符爲<,>,& ...而不需要模板的唯一方法。據瞭解,您可能不會發送HTML,而是會生成HTML的一些可變數據。

或者,你可以考慮將照片的網址爲逗號分隔值photos: 'url/test1.jpg, url/test2.png',然後將模板定義爲:

<script id="photo-tpl" type="text/kendo-tpl"> 
    # var ps = data.photos.split(","); # 
    # for (var i = 0; i < ps.length; i++) { # 
     <img src="#= ps[i] #"/> 
    #}# 
</script> 

不知道這是不是不得不產生後面的HTML更容易 - 結束,但至少它(從我的角度來看)模型 - 視圖 - 控制器的清潔分離。

看到這個方法在這裏:http://jsfiddle.net/OnaBai/H6dD5/1/

+0

非常有幫助。我想強調'#=照片#'(等號),而不是'#:照片#'(冒號)。這些行爲類似於打開asp.net標記<%= vs. <%:因爲equals不會自動進行編碼。冒號。大多數kendo演示使用冒號。 – nickles80