Search code examples
phpkendo-uikendo-datasource

Kendo html string got formatted as html htmlentities


So in back end I use PHP and here is how string is formated :

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

and in response it is formatted good :

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

and when I try display it to user using :

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" }
            }
        }          
    }
});      

and I got text displayed as result. When I try to inspect that text I got this

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

And I am not sure it witch point that happened and why.

I am using latest version of Kendo UI.

EDIT

$("#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" }
            ]        
        });

Solution

  • Not sure if it is a defect or a feature but you can easily solve it defining a template for photos field as 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" }
    ]        
    

    See it here : http://jsfiddle.net/OnaBai/H6dD5/

    From my point of view it is a feature since this is the only way of being able to print special characters as <, >, &... without requiring a template. It is understood that you would probably not sending HTML but some variable data that will generate the HTML.

    Alternatively, you might consider sending the URLs of the photos as a Comma Separated Value photos: 'url/test1.jpg, url/test2.png' and then define a template as:

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

    Not sure if this is easier than having to generate the HTML in the Back-End but at least it is (from my point of view) a cleaner separation of Model-View-Controller.

    See this approach here: http://jsfiddle.net/OnaBai/H6dD5/1/