【问题标题】:Kendo html string got formatted as html htmlentitiesKendo html 字符串被格式化为 html htmlentities
【发布时间】:2014-04-22 13:09:54
【问题描述】:

所以在后端我使用 PHP,这是字符串的格式:

$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;  

我不确定发生了什么以及为什么。

我正在使用最新版本的剑道 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" }
            ]        
        });

【问题讨论】:

  • html_entity_decode 是这样发生的。
  • 我实际上是在用它来试图阻止它。 Documentation
  • 向我们展示您如何使用照片字段。
  • @AtanasKorchev 是您需要的吗(正在编辑中)?

标签: php kendo-ui kendo-datasource


【解决方案1】:

不确定是缺陷还是功能,但您可以轻松解决它,将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/

从我的角度来看,这是一个功能,因为这是能够在不需要模板的情况下将特殊字符打印为 &lt;&gt;&amp;... 的唯一方法。据了解,您可能不会发送 HTML,而是发送一些会生成 HTML 的可变数据。

或者,您可以考虑将照片的 URL 作为逗号分隔值 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/

【讨论】:

  • 非常有帮助。我想强调#= photos #(等号)而不是#: photos #(冒号)。这些行为类似于打开 asp.net 标签
猜你喜欢
  • 1970-01-01
  • 2017-06-21
  • 1970-01-01
  • 2014-01-17
  • 2019-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
相关资源
最近更新 更多