weixin_33739541 2016-12-21 13:52 采纳率: 0%
浏览 23

无法在网格中打印数据

I am trying to print my data coming from controller in a table but unable to do so. I am using Kendo UI template in order to print. I am attaching the code below along with the error that I am getting.

<div id="example"></div>

<script>

        $.ajax(
            {
                type: 'POST',
                url: '/default1/KendoDataAjaxHandle/',
                dataType: 'json',
                success: function (result) {


                    //Get the external template definition using a jQuery selector
                    var template = kendo.template($("#javascriptTemplate").html());

                    console.log(result);
                    var data = template(result); //Execute the template
                    $("#example").html(data); //Append the result

                }
            })

</script>

<script id="javascriptTemplate" type="text/x-kendo-template">

    <table>
        <thead>
            <tr>
                <th>Customer ID</th>
                <th>Customer name</th>
                <th>Gender</th>
            </tr>
        </thead>
        <tbody>
            # for (var i=0; i < result.length; i++){ #
            <tr>
                # for (var j=0; j < result[i].length; j++){ #
                <td>
                    #= result[j] #
                </td>
                # } #
            </tr>
            # } #
        </tbody>
    </table>

</script> 

What i am doing in the above code is making an ajax call to the action method and getting the results in JSON. Here's my controller method:

public ActionResult KendoDataAjaxHandle([DataSourceRequest]DataSourceRequest request)
        {
            IQueryable<Customer> products = db.Customers;
            DataSourceResult result = products.ToDataSourceResult(request);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

Error that I am getting on executing the code is: result is undefined. However on consoling the results returned after ajax call, I get an object which has all the values.

How do I print those values coming back in the table? Also please correct me if I have done something wrong here. Thank in advance.

  • 写回答

1条回答 默认 最新

  • weixin_33701564 2016-12-21 14:33
    关注

    Try changing the the variable name referenced inside the template from "result" to "data" as that is what Kendo uses in its template execution code.

    Example: http://dojo.telerik.com/@Stephen/ENUze

    Updated Example Showing how to iterate the object's fields(based on comment): http://dojo.telerik.com/@Stephen/oXOJU

    NOTE: this assumes that the fields are listed in the order you have specified in your comment. If the fields don't come back in this order, you will have to add code to map the field name to the column index instead of just blindly looping.

    Update 2:

    Updated example using exact format of returned data:

    http://dojo.telerik.com/@Stephen/ipeHec

    Note: you have to deal with the CustomerAltID that is in the returned data but not in the table somehow. My example chooses to remove it from list of keys to process. How you deal with it is up to you.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度