1

我有一个带有 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 = "[{"FirstName":"b","LastName":"bb","LoginName":"b"},{"FirstName":"","LastName": "","LoginName":"test1"},{"FirstName":"","LastName":"","LoginName":"test2"}]"

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

2 回答 2

0

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

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

于 2014-09-23T12:39:05.883 回答
0

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

于 2013-01-10T18:22:34.423 回答