【问题标题】:How to achieve edit and delete on jqgrid?如何在jqgrid上实现编辑和删除?
【发布时间】:2014-11-08 14:25:57
【问题描述】:

下面是我的 JQGrid 代码,它可以很好地加载网格、分页和排序。

现在,我需要添加 - 编辑和删除功能。

我在谷歌上找到,在导航栏中启用“编辑”和“删除”选项,但是, 我希望它像----用户单击“a href”链接,它应该调用 javascritp 函数以及 rowID 。你能指导我,我怎样才能添加超链接并点击它允许调用functino?

        $('#CategoriesGrdList').jqGrid({
        ajaxGridOptions: {
            error: function () {
                $('#CategoriesGrdList')[0].grid.hDiv.loading = false;
                alert('An error has occurred.');
            }
        },
        url: '@Url.Action("GetAllCategoriesList", "Categories")/' + 0,
        gridview: true,
        autoencode: true,
        //public JsonResult GetEnrolls(int adClassSchedID,DateTime attendanceDate,int adProgramID,int syCampusID)
        postData: { categoryId: 1 },
        //postData: { categoryId: rowID, attendanceDate: $('#AttendanceDate').val(), adProgramID: $('#adProgramID').val(), syCampusID: $('#syCampusID').val() },
        datatype: 'json',
        jsonReader: { root: 'List', page: 'Page', total: 'TotalPages', records: 'TotalCount', repeatitems: false, id: 'Id' },
        mtype: 'GET',
        colNames: ['Id', 'Code', 'Description', 'IsActive'],
        colModel: [
              { name: 'Id', index: 'Id', hidden: true },
            { name: 'Code', index: 'Code', width: 170 },
            { name: 'Description', index: 'Description', width: 170 },
        { name: 'IsActive', index: 'IsActive', width: 170 }
        ],
        pager: $('#CategoriesGrdPager'),
        sortname: 'Code',
        rowNum: 40,
        rowList: [3, 3, 3],
        width: '525',
        height: '100%',
        viewrecords: true,

        beforeSelectRow: function (rowid, e) {
            return false;

        },
        sortorder: 'desc'
    }).navGrid('#CategoriesGrdPager', { edit: false, add: false, del: false, search: false, refresh: false });
});

注意:

根据以下评论之一的建议, 新增以下栏目:

                 {
                 name: 'act', index: 'act', width: 55, align: 'center', sortable: false,    formatter: 'actions',
                 formatoptions: {
                     keys: true, // we want use [Enter] key to save the row and [Esc] to cancel   editing.
                     onEdit: function (rowid) {
                         alert("in onEdit: rowid=" + rowid + "\nWe don't need return anything");
                     },
                     onSuccess: function (jqXHR) {
                         // the function will be used as "succesfunc" parameter of editRow function
                         // (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:inline_editing#editrow)
                         alert("in onSuccess used only for remote editing:" +
                               "\nresponseText=" + jqXHR.responseText +
                               "\n\nWe can verify the server response and return false in case of" +
                               " error response. return true confirm that the response is successful");
                         // we can verify the server response and interpret it do as an error
                         // in the case we should return false. In the case onError will be called
                         return true;
                     },
                     onError: function (rowid, jqXHR, textStatus) {
                         // the function will be used as "errorfunc" parameter of editRow function
                         // (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:inline_editing#editrow)
                         // and saveRow function
                         // (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:inline_editing#saverow)
                         alert("in onError used only for remote editing:" +
                               "\nresponseText=" + jqXHR.responseText +
                               "\nstatus=" + jqXHR.status +
                               "\nstatusText" + jqXHR.statusText +
                               "\n\nWe don't need return anything");
                     },
                     afterSave: function (rowid) {
                         alert("in afterSave (Submit): rowid=" + rowid + "\nWe don't need return anything");
                     },
                     afterRestore: function (rowid) {
                         alert("in afterRestore (Cancel): rowid=" + rowid + "\nWe don't need return anything");
                     },
                     delOptions: {
                         // because I use "local" data I don't want to send the changes to the server
                          // so I use "processing:true" setting and delete the row manually in onclickSubmit
                         onclickSubmit: function (rp_ge, rowid) {
                             // we can use onclickSubmit function as "onclick" on "Delete" button
                             alert("The row with rowid=" + rowid + " will be deleted");

                             // reset processing which could be modified
                             rp_ge.processing = true;

                             // delete row
                             $(this).delRowData(rowid);
                             $("#delmod" + $(this)[0].id).hide();

                             if ($(this)[0].p.lastpage > 1) {
                                 // reload grid to make the row from the next page visable.
                                 // TODO: deleting the last row from the last page which number is higher as 1
                                 $(this).trigger("reloadGrid", [{ page: $(this)[0].p.page }]);
                             }

                             return true;
                         },
                         processing: true // !!! the most important step for the "local" editing
                         //     skip ajax request to the server
                     }
                 }
             }

现在,问题:

控制器的调用动作在哪里指定? 单击编辑时,什么也没有发生,我想调用我自己的控制器操作并弹出我自己的对话框而不是 jqgrid 对话框。

请指导我。

【问题讨论】:

  • 您指的是哪个“a href”链接?您发布的代码不包含任何 <a> 元素。网格的哪些列应该是“可编辑的”?您选择了哪个editing mode(内联编辑、表单编辑或单元格编辑)?您写过“与 rowID 一起调用 JavaScript 函数”,但如果您将服务器上的数据保存在数据库中,则应将数据发送到服务器。不是吗?
  • 我还没有添加'a href'。但col model 中会有额外的列,其中应包含“删除”图像图标。单击图像图标时,它应该与主键一起调用 javascript 函数,因此,我可以对服务器进行 ajax 调用并从数据库中删除该行,然后在刷新网格后。请建议实现这一目标的最佳方法。同样,点击编辑链接,我需要打开保存数据的对话框。
  • 你知道formatter: "actions" 存在于jqGrid 3.8.2 版本开始吗?见the answer。它允许创建编辑/删除/保存/取消按钮,这些按钮在内部使用表单编辑的内联编辑。可以使用formatoptions将对应的选项转发给editRow/editGridRowdelGridRow。见the documentation。文档中描述了通过编辑方法发布到服务器的数据。
  • 好的,我已经添加了您在该链接上建议的操作列。现在,如何提供 - 将实际删除记录然后重新加载网格的 URL(控制器和操作)。 ?
  • 您没有发布新代码。您使用formatter: "actions" 的哪些选项?您是使用formatoptions: {editformbutton: true} 来使用表单编辑还是使用默认的内联编辑?文档中描述了编辑功能发布的数据。例如here 你会看到Delete 发送的参数。最重要的是id 参数,它是要删除的行的rowid。如果您正确填充网格,则 id 值足以删除该行。网格将在编辑后重新加载。

标签: jqgrid jqgrid-formatter mvcjqgrid


【解决方案1】:

我看到你正在使用 MVC,这就是我的做法,这可能与你的做法不同,但我喜欢这样做。基本上,您指定如何在控制器中编辑、添加和删除,然后使用 "url: '/YourController/YourActionMethod/' 告诉网格在哪里查看。您可以设置许多不同的选项,例如标题、宽度等 @987654321 @。

}).navGrid('#CategoriesGrdPager', { cloneToTop: true, search: false },
{
    url: '/YourController/EditCategories/',
    editCaption: 'Edit Post',
    closeAfterEdit: true,
    closeOnEscape: true,
    dataheight: 1150,
    top: 25,
    left: 50,
    width: 900,
},//edit options
{
    url: '/YourController/AddCategories/',
    addCaption: 'New Post',
    closeAfterAdd: true,
    closeOnEscape: true,
    dataheight: 1150,
    top: 25,
    left: 50,
    width: 900,
},//add options
{
    url: '/YourController/DeleteCategories/',
    caption: 'Delete Post',
    closeOnEscape: true,
}//delete options
);

【讨论】:

  • 感谢这个。但是根据提供的设计,我需要添加内联图像按钮并且需要弹出编辑对话框而不是内联编辑。并在单击内联图像按钮时执行删除操作。
  • @Dhaval 我明白了,我还没有尝试过内联编辑,所以无法帮助你,抱歉。
  • 现在,我什至无法选择行...当我单击导航栏上的编辑图标时,选定的行不存在。
猜你喜欢
  • 2011-10-31
  • 2012-08-17
  • 2011-03-10
  • 1970-01-01
  • 2012-06-18
  • 1970-01-01
  • 2012-05-02
  • 2014-06-02
  • 1970-01-01
相关资源
最近更新 更多