SlickGrid Ajax调用空行

时间:2013-01-10 14:52:46

标签: json jquery slickgrid

我有一个带有AJAX调用的SlickGrid。我在Chrome下跟踪它并发现数据是正确的但不知何故网格显示所有空行。知道为什么吗?

网络服务

    [System.Web.Services.WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static string LoadUsers()
    {
        List<Person> users = new List<Person>();
        using (SqlConnection conn = UtWeb.ConnectionGet())
        {
            conn.Open();
            SqlCommand command = new SqlCommand("UserGet", conn);

            using (SqlDataReader r = command.ExecuteReader())
            {
                while (r.Read())
                {
                    Person person = new Person();
                    person.LoginName = r["LoginName"].ToString();
                    person.FirstName = r["FirstName"].ToString();
                    person.LastName = r["LastName"].ToString();
                    users.Add(person);
                }
            }
        }

        MemoryStream stream = new MemoryStream();
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Person[]));
        serializer.WriteObject(stream, users.ToArray());
        stream.Position = 0;
        StreamReader streamReader = new StreamReader(stream);
        return streamReader.ReadToEnd();            
    }

代码

<script src="../Scripts/jquery-1.7.2.min.js"></script>
<script src="../Scripts/jquery.event.drag.min.js"></script>

<script src="../Scripts/SlickGrid/slick.core.js"></script>
<script src="../Scripts/SlickGrid/slick.grid.js"></script>
<script>

    var grid;
    var loader;
    var columns = [
    { id: "login-name", name: "Login Name", field: "LoginName" },
    { id: "firstname", name: "First Name", field: "FirstName" },
    { id: "lastname", name: "Last Name", field: "LastName" }
  ];

    var options = {
        editable: false,
        enableAddRow: false,
        enableCellNavigation: true,
        enableColumnReorder: false
    };

    $(document).ready(function () {
        $.ajax({
            url: 'TestSlickGrid.aspx/LoadUsers',
            async: false,
            data: "{}",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            success: OnSuccess,
            failure: function (response) {
                alert("ajax fail!");
            }
        });
        function OnSuccess(response) {
            if (parseInt(response) == 0)
                alert(response.d);
            else {
                loader = response; 
                grid = new Slick.Grid("#myGrid", loader.d, columns, options);
            }

        }
    });    
</script>

在Chrome中,它显示了这一点 loader.d = “[{” 姓 “:” B”, “名字”: “BB”, “LoginName将”: “B”},{ “姓”: “”, “名字”: “”, “LoginName将”: “测试1” },{ “姓”: “”, “名字”: “”, “LoginName将”: “TEST2”}]“

        loader = '{"employees":' + response.d + '}'; 
        var obj = eval("(" + loader + ")");
        grid = new Slick.Grid("#myGrid", obj.employees, columns, options);

2 个答案:

答案 0 :(得分:0)

loader.d解析为JSON,alert(loader.d[0].LoginName)显示什么?

答案 1 :(得分:-1)

尝试为嵌入网格的div提供一些高度。在您的情况下,我相信您的div名称是myGrid

尝试为这个div定义一些高度,它会起作用。我遇到了这个问题,并且能够通过仅为我的div定义高度来解决这个问题。