在 Asp.Net MVC 中向 jqGrid 添加命令列

发布于 2024-09-24 00:07:52 字数 418 浏览 2 评论 0原文

我希望你早上好。

我想在我的 Asp.Net 应用程序中使用 jqGrid。到目前为止,我一直在生成一个 html 表并将其转换为网格,但我想尝试一种更好的方法:通过 AJAX 调用加载数据,就像网格所设想的那样。我找到了如何获取数据,但除了数据列之外,我还想拥有“编辑”和“删除”列。我该如何实现它?

我不想为控制器中的按钮制作 html。理想的解决方案是仅发送数据,并让 jqGrid 根据某些客户端模板和 id 值添加所需的列。我无法手动执行此操作,因为我无法将列添加到现有网格中。那么,我该怎么办?

更新。我不需要仅仅实现编辑/删除功能。我需要的是根据某些模板和“id”值添加一个包含 HTML 的列,例如 [更多详细信息]

I hope you have a good morning.

I'd like to use jqGrid in my Asp.Net app. So far, I've been producing an html table and converting it to a grid, but I'd like to try a better approach: loading the data via an AJAX call as it's supposed with the grid. I found out how to fetch data, but I'd like to have the "Edit" and "Delete" columns in addition to the data columns. How do I achieve it?

I'd rather not craft the html for the buttons in my controller. The ideal solution would be to send just the data, and have jqGrid add the required columns based on some client-side templates and the id values. I can't do it manually since I can't add columns to the existing grid. So, what do I do?

Update. I don't need just to achieve edit/delete functionality. What I need is to add a column with HTML based on some template and the "id" value, like <a href="MoreDetails/{id}">[More details]</a>

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

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

发布评论

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

评论(2

丘比特射中我 2024-10-01 00:07:52

如果您使用 jqGrid,从服务器发送回纯数据是正确的方法。

要实现您想要的行编辑,请查看 jqGrid 演示 并选择左侧部分“行编辑”,然后“自定义编辑”。我个人更喜欢使用所谓的“内联编辑”(在“行编辑”下的同一演示“输入类型”中选择)。您可以通过双击而不是选择行来实现编辑模式的切换(请参阅 jqGrid - 仅编辑可编辑列的某些行 作为示例)。要删除行,您可以另外使用“表单中的“删除”按钮编辑”。要使用它,您应该添加 Navigator 与 navGrid 方法有关并使用相应的参数选择工具栏中您需要的按钮。要在演示中查看此内容,请选择“实时数据操作”,然后选择“导航器”。

更新:在jqGrid 演示中,请参阅“行编辑”,然后“自定义编辑”,您可以看到如何在 gridCompleteloadComplete 句柄内使用 setRowData 来设置 ANY HTML 代码片段。为什么你不喜欢这个方法?您还可以使用预定义的显示链接格式化程序来显示链接或使用自定义格式化程序自定义取消格式化程序,用于显示任何 HTML 包含 jqGrid 单元格。

Sending pure data back from the server is the correct way if you use jqGrid.

To implement Row editing like you want look at the jqGrid Demo and choose on the left part "Row Editing" and then "Custom Edit". I personally prefer to use so named "inline editing" (choose on the same demo "Input types" under "Row Editing"). You implement switching in editing mode by double-click instead of selecting the row (see jqGrid - edit only certain rows for an editable column as an example). To delete the rows you can use additionally "Delete" button from the "form editing". To use it you should add Navigator with respect of navGrid method and choose with the corresponding parameters the buttons in the toolbar which you need. To see this on the demo choose "Live Data Manipulation" and then "Navigator".

UPDATED: In the jqGrid Demo, see "Row Editing" and then "Custom Edit" you can see how you can use setRowData inside of gridComplete or loadComplete handle to set ANY HTML code fragment. Why do you don't like the method? You can also use predefined showlink formatter to display a link or use custom formatter and custom unformatter to display any HTML contain in a jqGrid cell.

夜无邪 2024-10-01 00:07:52

您现在可能已经明白了,但无论这里有价值的是我使用自定义格式化程序的答案。 “操作”列使用自定义格式化程序呈现,该格式化程序显示一个调用简单 JavaScript 函数的按钮。

$(document).ready(function () {
        $("#all-errors-list").jqGrid({
            url: '/Error/AllErrors/',
            datatype: 'json',
            mtype: 'GET',
            colNames: ['Id', 'Error','Actions'],
            colModel: [
            { name: 'Id', index: 'Id', width: 100, align: 'left', editable: true},
            { name: 'ErrorDetails', index: 'ErrorDetails', width: 350, align: 'left' },
            { name: 'ActionId', width:400, formatter: actionFormatter}
                      ],
            pager: '#all-errors-pager',
            rowNum: 10,
            rowList: [10, 20, 50],
            sortname: 'Id',
            sortorder: 'asc',
            viewrecords: true,
            imgpath: '<%=Html.ImagePath()%>',
            caption: 'Open Errors',
            height: "100%",
            width: "100%",
            gridComplete: function () { $("button").button(); }
        })

    function actionFormatter(cellvalue, options, rowObject) {
        return "<button onclick=\"alert('" + cellvalue + "')\">Details</button>" ;
    }

我希望这有帮助。

You probably figured it out by now, but for whatever is worth here is my answer using a custom formatter. The Action column is rendered using a custom formatter that shows a button that calls a simple javascript function.

$(document).ready(function () {
        $("#all-errors-list").jqGrid({
            url: '/Error/AllErrors/',
            datatype: 'json',
            mtype: 'GET',
            colNames: ['Id', 'Error','Actions'],
            colModel: [
            { name: 'Id', index: 'Id', width: 100, align: 'left', editable: true},
            { name: 'ErrorDetails', index: 'ErrorDetails', width: 350, align: 'left' },
            { name: 'ActionId', width:400, formatter: actionFormatter}
                      ],
            pager: '#all-errors-pager',
            rowNum: 10,
            rowList: [10, 20, 50],
            sortname: 'Id',
            sortorder: 'asc',
            viewrecords: true,
            imgpath: '<%=Html.ImagePath()%>',
            caption: 'Open Errors',
            height: "100%",
            width: "100%",
            gridComplete: function () { $("button").button(); }
        })

    function actionFormatter(cellvalue, options, rowObject) {
        return "<button onclick=\"alert('" + cellvalue + "')\">Details</button>" ;
    }

I hope that helps.

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