引用
js
var mygrid; // 定义mygrid
$(function () {
mygrid = new dhtmlXGridObject('gridbox1');
mygrid.enableAutoWidth(true); // 自动宽度
mygrid.setHeader("标题一,标题二,标题三"); // 设置grid的头
mygrid.setInitWidths("150,150,150")
mygrid.setColTypes("ro,ro,ro");
mygrid.setColAlign("center,center,center");
mygrid.setColSorting("str,str,str");
mygrid.setEditable(true);
mygrid.setSkin("dhx_skyblue"); //dhtmlXGrid默认加载为XML。
mygrid.init(); // 初始化
mygridjiazai(); // 调用方法,加载数据(数据库查询)
});
function mygridjiazai(){
$.ajax({
type: "POST",
url: "/Main/GetDemoData",
cache: false,
success: function (result) {
$.each(result, function (i, item) {
var str = item.ID + "," + item.Name + "," + item.Content; // 将hosnum,nodecode,localip保存在rowid里
mygrid.addRow(str, [
item.ID,
item.Name,
item.Content
]);
});
}
});
}
Controller
public ActionResult GetDemoData()
{
ArrayList jsonList = new ArrayList();
foreach (DataRow dr in Demo.DemoData().Rows)
{
Dictionary item = new Dictionary();
item.Add("ID", dr["ID"]);
item.Add("Name", dr["Name"]);
item.Add("Content", dr["Content"]);
jsonList.Add(item);
}
return Json(jsonList, JsonRequestBehavior.AllowGet);
}