ASP.NET MVC +没有 AJAX 的 jqGrid

发布于 2024-08-14 21:22:23 字数 377 浏览 3 评论 0原文

我有一个 ASP.NET MVC 应用程序,它正在对产品数据库执行搜索。我想使用 TreeGrid 模块在 jqGrid 中显示结果。我真的不需要网格是 AJAX-y 的,因为数据是静态的,而且它足够小,可以一次全部发送到客户端。

第一个问题:如何设置 jqGrid,以便不再从 URL 中提取 JSON 数据,而只是在 JS 变量或其他内容中查找?

其次,让 ASP.NET MVC 将 JSON 数据放入 JavaScript 变量的最合适方法是什么?我的控制器中已经有了 List,只是想在 JSON 处理后以某种方式将其放入 JS 变量中。

或者我是否过于反对当前的情况而只是接受 jqGrid 似乎想要的 AJAX-y 工作方式?

谢谢,

〜贾斯汀

I have an ASP.NET MVC application which is executing a search against a products database. I want to display the results in a jqGrid using the TreeGrid module. I don't really need the grid to be AJAX-y because the data is static and it is small enough that it can all be sent to the client at once.

First question: how do I set up jqGrid so that instead of pulling the JSON data from a URL it just looks in a JS variable or something?

Secondly, what is the most appropriate way to get ASP.NET MVC to put JSON data into a JavaScript variable? I already have the List in my controller and just want to somehow get it out into a JS variable after JSON-ing it.

Or am I fighting against the current too much and just accept the AJAX-y way that jqGrid seems to want to work?

Thanks,

~ Justin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乙白 2024-08-21 21:22:23

以下是如何使用 JavaScript 函数显示 jqGrid 树。

$(document).ready(function() {
    TreeDemo.setupGrid($("#tree"));
});

TreeDemo = {
    data: { A: ["A1", "A2"], B: ["B1", "B2"] },
    setupGrid: function(grid) {
        grid.jqGrid({
            colNames: ['Name'],
            colModel: [
                  { name: 'Name', index: 'Name', width: "250em" }
                ],
            datatype: TreeDemo.treeData,
            loadui: "none",
            sortname: 'Number',
            treeGrid: true,
            treeGridModel: "adjacency",
            sortorder: "asc"
        })
    },
    treeData: function(postdata) {
        var items = postdata.nodeid ? TreeDemo.data[postdata.nodeid] : TreeDemo.data;
        var i = 0;
        var rows = new Array();
        for (val in items) {
            var isLeaf = postdata.nodeid != undefined;
            rows[i] = {
                Name: val,
                Id: val,
                level: postdata.nodeid ? 1 : 0,
                parent: postdata.nodeid || null,
                isLeaf: isLeaf ? "true" : "false",
                expanded: "false"
            }
            i++;
        }
        $("#tree")[0].addJSONData({
            Total: 1,
            Page: 1,
            Records: 2,
            Rows: rows
        });
    }
};

请注意,执行此操作的方式有很多选项,我的示例只是其中之一。

我将 JSON 获取到 JS var 的方法是:

  1. 编写一个 HTML Helper,向页面发出一个简短的脚本。
  2. 如果由于某种原因无法内联数据,请编写一个返回 JavaScriptResult 的操作来获取文件中的数据。

您可以使用 .NET JavaScript 序列化器创建 JSON。查看 MVC 源代码中的 JsonResult.ExecuteResult 示例。

Here is how to display a jqGrid tree using a JavaScript function.

$(document).ready(function() {
    TreeDemo.setupGrid($("#tree"));
});

TreeDemo = {
    data: { A: ["A1", "A2"], B: ["B1", "B2"] },
    setupGrid: function(grid) {
        grid.jqGrid({
            colNames: ['Name'],
            colModel: [
                  { name: 'Name', index: 'Name', width: "250em" }
                ],
            datatype: TreeDemo.treeData,
            loadui: "none",
            sortname: 'Number',
            treeGrid: true,
            treeGridModel: "adjacency",
            sortorder: "asc"
        })
    },
    treeData: function(postdata) {
        var items = postdata.nodeid ? TreeDemo.data[postdata.nodeid] : TreeDemo.data;
        var i = 0;
        var rows = new Array();
        for (val in items) {
            var isLeaf = postdata.nodeid != undefined;
            rows[i] = {
                Name: val,
                Id: val,
                level: postdata.nodeid ? 1 : 0,
                parent: postdata.nodeid || null,
                isLeaf: isLeaf ? "true" : "false",
                expanded: "false"
            }
            i++;
        }
        $("#tree")[0].addJSONData({
            Total: 1,
            Page: 1,
            Records: 2,
            Rows: rows
        });
    }
};

Note that there are lots of options for how you do this and my example is only one.

The way I would get the JSON into a JS var is to either:

  1. Write a HTML Helper which emits a short script to the page.
  2. Write an action which returns a JavaScriptResult to get the data in a file, if, for some reason, you can't have the data inline.

You create the JSON using the .NET JavaScript serializer. Look at the JsonResult.ExecuteResult in the MVC source code for an example.

怪我鬧 2024-08-21 21:22:23

请参阅 jqGrid 文档 wiki 中的数据操作页面。在那里您会发现许多将数据提供给网格的方法。

See the Data Manipulation page in the jqGrid documentation wiki. There you'll find many ways to feed the data to the grid.

花心好男孩 2024-08-21 21:22:23

还有一个 Table_to_jqGrid 插件 可能是一个有用的选项。

There is also a Table_to_jqGrid plugin that may be an useful option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文