简单的 jQuery SlickGrid JSON 示例或文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4984167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple jQuery SlickGrid JSON example or documentation
提问by doberkofler
I'm looking for a simple example on how to use SlickGrid when trying to retrieve the data as JSon via jQuery.Ajax. I was also unable to find any documentation of the SlickGrid plugin and was wondering if I was just looking in the wrong places. Any help to get me started with SlickGrid would be most appreciated.
我正在寻找一个关于如何在尝试通过 jQuery.Ajax 以 JSon 形式检索数据时使用 SlickGrid 的简单示例。我也找不到 SlickGrid 插件的任何文档,想知道我是否只是找错了地方。任何帮助我开始使用 SlickGrid 的帮助将不胜感激。
采纳答案by zalath
An example within an asp.net page. The webservice myData
returns a json string that needs to match the grid columns.
asp.net 页面中的示例。webservicemyData
返回一个需要匹配网格列的 json 字符串。
$(function () {
$.ajax({
url: "WS.asmx/myData",
global: false,
type: "POST",
data: "{}",
contentType: "application/json",
dataType: "json",
async: false,
success: function (json) {
data = eval('(' + json.d + ')');
if (!data) { alert('no data'); };
},
error: function (msg) {
var errorText = eval('(' + msg.responseText + ')');
alert('Error : \n--------\n' + errorText.Message);
}
}
);
if (data) {
dataView = new GridNic.Data.DataView();
grid = new GridNic.Grid($("#myGrid"), dataView.rows, columns, options);
var pager = new GridNic.Controls.Pager(dataView, grid, $("#pager"), columns);
var columnpicker = new GridNic.Controls.ColumnPicker(columns, grid, options);
... and so on
... 等等
In Asp.Net, the size of the json string is restricted by default. In case of trouble you have to declare a larger size in the web.config e.g. :
在 Asp.Net 中,默认情况下限制 json 字符串的大小。如果出现问题,您必须在 web.config 中声明更大的尺寸,例如:
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="5000000">
</jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
回答by Van Gale
The AJAX example in the SlickGrid repository is quite complex, because it's trying to get tricky with caching, etc. For example, it keeps track of all the rows already sent and will only request new rows from the server. It's also hard coded for the specific example of Digg stories. Documentation is sorely lacking and it seems buggy with the versions 1.5+ of jQuery (which changed how ajax was handled).
SlickGrid 存储库中的 AJAX 示例非常复杂,因为它试图在缓存等方面变得棘手。例如,它跟踪所有已发送的行,并且只会从服务器请求新行。它也是针对 Digg 故事的特定示例进行了硬编码。文档非常缺乏,而且 jQuery 1.5+ 版本似乎有问题(它改变了 ajax 的处理方式)。
I had much easier time getting started by using Andrew Childs fork of SlickGrid, which contains very simple and straightforward instructions on how to use AJAX at the bottom of the README:
通过使用 SlickGrid 的 Andrew Childs 分支,我可以更轻松地开始使用,其中包含有关如何在自述文件底部使用 AJAX 的非常简单明了的说明:
The repository is at https://github.com/andrewchilds/SlickGrid
回答by gor
回答by wynshaft
The solution is simple, but they do not explicitly state how to do this on their wiki page.
解决方案很简单,但他们没有在他们的 wiki 页面上明确说明如何做到这一点。
SlickGrid expects JSON to be in object form. So if for any reason it is in string form just use:
SlickGrid 期望 JSON 为对象形式。因此,如果出于任何原因它是字符串形式,请使用:
JSON.parse(jsonString);
If you're loading from ajax, just simply do this:
如果您从 ajax 加载,只需执行以下操作:
$.getJSON("file.json", function(data) {
grid = new Slick.Grid("#myGrid", data, columns, options);
}