0

Below is my JQGrid code which is working fine for loading of grid, paging and sorting.

Now, I need to add - Edit and Delete functionality.

I found on google, to enable "edit" and "delete" option in navbar but, I want it as like ---- User click on "a href" link and it should call javascritp function along with rowID . Can you guide me , how can i add hyper link and on click of it allow to call 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 });
});

NOTE:

As suggestion from one of comment below, Added below column:

                 {
                 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
                     }
                 }
             }

Now, PROBLEM:

Where to specify for calling - action of controller ? On click of Edit, nothing happen, I want to call my own controlller 'action and popup my own dialog instead of jqgrid dialog.

PLease guide me.

dsi
  • 3,199
  • 12
  • 59
  • 102
  • I have not added 'a href' yet. But there will be additional column in `col model` which should contain "delete" image icon . on click of image icon, it should call javascript function along with primary key so, i can make ajax call to server and delete the row from database and then after refresh the grid. Please suggest best way to achieve this. Same way, ON click of edit link, i need to open dialog box which should save the data. – dsi Sep 15 '14 at 07:25
  • Do you know `formatter: "actions"` which exists in jqGrid starting with 3.8.2 version? See [the answer](http://stackoverflow.com/a/5204793/315935). It allows to create Edit/Delete/Save/Cancel buttons which uses either inline editing of form editing internally. One can use `formatoptions` to forward the corresponding options to `editRow`/`editGridRow` and `delGridRow`. See [the documentation](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:predefined_formatter). The data posted to the server by the editing methods are described in the documatation. – Oleg Sep 15 '14 at 07:38
  • ok, I have added actions column how you suggested on that link. Now, how to provide - URL (controller and action) which will actually delete the record and then reload the grid. ? – dsi Sep 15 '14 at 07:45
  • You don't posted the new code. Which options of `formatter: "actions"` you use? Do you use `formatoptions: {editformbutton: true}` to use form editing or you use default inline editing? The data posted by editing functions are described in the documentation. For example [here](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:form_editing#what_is_posted_to_the_server2) you see the parameter sent by Delete. The most important is `id` parameter with is rowid of the row to delete. If you correctly fill the grid then id value is enough to delete the row. The grid will be reloaded after editing. – Oleg Sep 15 '14 at 08:04
  • If you modify the text of the question please write short comment to inform about it. I opened your question absolutely accidentally. To call your controller action you have to specify `editurl` parameter with URL of the action. – Oleg Sep 15 '14 at 13:36
  • @Oleg, you can make the answer for delete. I got solved as you suggested. for Edit, I have created new question. – dsi Sep 15 '14 at 15:07

1 Answers1

0

I see you are using MVC, this is how I am doing it, this may be different than how you are doing it, but I like to do it this way. Basically you specify how you edit, add and delete in your controller and then use "url: '/YourController/YourActionMethod/' to tell the grid where to look. there are many different options you can set such as caption, width etc See Here.

}).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
);
Mindless
  • 2,352
  • 3
  • 27
  • 51
  • thanks for this this. But as per provided design, i need to add inline image button and need to popup edit dialog instead of inline editing. and do delete operation on click of inline image button. – dsi Sep 15 '14 at 07:55
  • @Dhaval I see, I haven't tried inline editing yet, so couldn't help you on that, sorry. – Mindless Sep 15 '14 at 08:19
  • For now, I am not able to select the row even... selected row not remain present when I click at edit icon on navbar. – dsi Sep 15 '14 at 15:08