admin管理员组文章数量:1331083
I'm suggested to use JQuery data table. Now I need to populate grid with bunch of json objects sent from my controller. How can I send this data on the grid from js
$.ajax({
...
url: '/Home/ReturnJsonData',
success: function (result) {
$.each(result, function (i, item) {
// this is where I should sent item object to my grid
});
},
error: function () { alert("error"); }
});
Update I've found these link, but I dont know how to implement it.
I'm suggested to use JQuery data table. Now I need to populate grid with bunch of json objects sent from my controller. How can I send this data on the grid from js
$.ajax({
...
url: '/Home/ReturnJsonData',
success: function (result) {
$.each(result, function (i, item) {
// this is where I should sent item object to my grid
});
},
error: function () { alert("error"); }
});
Update I've found these link, but I dont know how to implement it.
Share Improve this question edited Sep 12, 2012 at 10:03 Nope 22.3k8 gold badges49 silver badges73 bronze badges asked Sep 12, 2012 at 8:28 panjopanjo 3,51513 gold badges49 silver badges83 bronze badges 2- So why don't you parse the result, and create a table element then push the result's elements as rows, finally activate jQuery DataTable ? – Issa Qandil Commented Sep 12, 2012 at 8:40
- @IssaQandil can you show me on concrete example ? – panjo Commented Sep 12, 2012 at 8:51
4 Answers
Reset to default 1You should use JQuery DataTable sAjaxSource property to specify ajaxsource in your case it would be /HomeReturnJsonData
An example follow
$(document).ready(function () {
$('#myDataTable').dataTable({
"bServerSide": true,
"sAjaxSource": "Home/ReturnJsonData",
"bProcessing": true,
"aoColumns": [
{ "sName": "ID",
"bSearchable": false,
"bSortable": false,
"fnRender": function (oObj) {
return '<a href=\"Details/' +
oObj.aData[0] + '\">View</a>';
}
},
{ "sName": "COMPANY_NAME" },
{ "sName": "ADDRESS" },
{ "sName": "TOWN" }
]
});
});
You can use Jquery Grid Plugin in that case.
Read this article to use MVC Data Grid: using jqGrid and JSON
http://blog.davidrenz./?p=663
Update:
In that case if you only want to use J-query Datatable go to this link.
http://www.codeproject./Articles/155422/jQuery-DataTables-and-ASP-NET-MVC-Integration-Part
I put this in a function because that was the easiest way to do it for me, feel free to copy the function and use it. All your going to need to change is the url and the column names and number there of. To call it just copy the html with the paths done so that they match whatever yours are.
function SetUpGrid(tableID, pagerID, data) {
$("#" + tableID).jqGrid({
url: '/pagename/stuff?data=' + data,
datatype: 'json',
mtype: 'GET',
colNames: ['col name1', 'col name2', ... 'col nameN'],
colModel: [
{ name: 'colName1', index: 'colName1', align: "center", sortable: true, editable: false, resizable: false },
{ name: 'colName2', index: 'colName2', align: "center", sortable: true, editable: false, resizable: false },
...
{ name: 'colNameN', index: 'colNameN', align: "center", sortable: true, editable: false, resizable: false }
],
pager: '#' + pagerID,
autowidth: true,
viewrecords: true,
rowNum: 15,
pgbuttons: true,
pginput: false,
pgtext: "Page {0} of {1}",
recordtext: "Data {0} - {1} of {2}",
emptyrecords: "No data to display",
loadui: true,
rowList: [15, 30, 60],
scrollrows: true,
hidegrid: true,
sortorder: "desc",
beforeRequest: function () { // This just inserts a loading image, you don't need it but I was loading a lot of data and wanted the user to know something was happening.
$("#" + tableID).empty().append('<tr><td><img src="/Content/imgs/loading.gif" /></td></tr>');
},
loadComplete: function (data) {
/*
Called when the json load is done, this is a way to insert the data the way I want to.
Feel free to use whatever you want like links or <p>s or <div>s or whatever.
*/
if (data.length > 1) {
for (var key in data) {
if (data.hasOwnProperty(key)) {
$("#" + tableID).empty().append("<tr><td>" + data[key].colName1 + "</td><td>" + data[key].colName12+ "</td> ... <td>" + data[key].colNameN + "</td></tr>");
}
}
} else {
$("#" + tableID).empty().append("<tr><td>" + this.colName1 + "</td><td>" + this.colName2 + "</td> ... <td>" + this.colNameN + "</td></tr>");
}
},
loadError: function (xhr, status, error) {
// Called when an error occurs, handle as you wish, if you even do.
alert(xhr);
alert(status);
alert(error);
}
});
$("#" + tableID).jqGrid("navGrid", "#" + pagerID, { add: false, edit: false, refresh: false, del: false, search: false }).trigger("reloadGrid", [{ page: 1 }]);
}
<script src="/Scripts/jquery-1.7.2.js"></script>
<script src="/Scripts/jquery-ui-1.8.23.min.js"></script>
<script src="/Scripts/jquery.jqGrid.min.js"></script>
<script src="/Scripts/grid.locale-en.js"></script>
<script src="/Scripts/ADTFunding.js"></script>
<link href="/Content/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css" />
<link href="/Content/jquery.jqGrid/ui.jqgrid.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(function () {
SetUpFundingGrid('dataTable', 'pager', '9895998');
});
</script>
<table id="dataTable"></table>
<div id="pager"></div>
You can also use the fnAddData
property to push json to table. Check this article https://jqueryajaxphp./jquery-datatable-using-json/
本文标签: javascriptPopulate JQuery DataTable with my json dataStack Overflow
版权声明:本文标题:javascript - Populate JQuery DataTable with my json data - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742272449a2444563.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论