我有一个带有 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);