1

I've been having some success with JQGrid but I'm now in a situation where I need to load data once then perform all the crud operations on the clientside and as easy as that sounds I'm completely stuck. I've been all through stack overflow and google and can't seem to find any good examples of simple clientside jqgrid operations (I could be wrong but I wasn't able to find any clientside stuff in the official jqgrid docs either). I attempted to utilize the example here:

http://www.ok-soft-gmbh.com/jqGrid/LocalFormEditing.htm

but I'm still a newbie with ajax/javaxcript/jquery and it was somewhat overwhelming and I just couldn't get it functioning with my grid. I would really appreciate any tips or links to a tutorial. My code and what I've gathered about the process so far is as follows:

I have my grid:

 editSettings = {
                jqModal: false,
                reloadAfterSubmit: false,
                closeOnEscape: true,
                savekey: [true, 13],
                closeAfterEdit: true,
                onclickSubmit: editSubmit,
                height: 200,
                width: 400
            },
             addSettings = {
                jqModal: false,
                reloadAfterSubmit: false,
                savekey: [true, 13],
                closeOnEscape: true,
                closeAfterAdd: true,
                onclickSubmit: addSubmit,
                height: 200,
                width: 400
            },
         $('#engineerGrid').jqGrid({
         datatype: 'json',
         colNames: ['Engineer Name', 'Engineer Type', '% Utilization'],
         colModel: [
         {name: 'name', index: 'name', width: 150, align: "left", editable: true, edittype: "select",
         editoptions: { value: "@(Model.engineerOptions)" }, sorttype: 'string', formatter: 'select'
     },
         { name: 'type', index: 'type', width: 150, align: "left", editable: true, edittype: "select",
             editoptions: { value: "@(Model.typeOptions)" }, formatter: 'select'
         },
         { name: 'ut', index: 'ut', width: 125, align: "left", editable: true, sorttype: 'number', formatter: 'number' }
            ],
         rowNum: 10,
         rowList: [5, 10, 20],
         pager: '#pager',
         gridview: true,
         rownumbers: true,
         autoencode: true,
         ignoreCase: true,
         sortname: 'invdate',
         viewrecords: true,
         sortorder: 'desc',
         loadonce: true,
         caption: 'Project Engineers',
         editurl: 'clientArray',
         url: '@Url.Action("EditProjectEngineerJQGridData","Project",new{projID = Model.projectID})'
     }).jqGrid('navGrid', '#pager', {}, editSettings, addSettings, delSettings);
 });

I'm getting json info from the server and I think that because I have loadonce:true set it should switch the datatype from json to local. This is where I start to get confused. Do I need to write all the crud functions manually and link to them with onclickSubmit? If that's the case would anyone be willing to explain how to get/set the data correctly?

addSubmit = function(){   
               //I think something needs to go here?   
                };

Edit:

Thought I'd reiterate for anyone who sees this. Since I don't have much experience this might be low level but maybe other newbies can benefit from a layman's explanation. When working with local data you cannot use form editing (except for delete which will work). In order to add/edit rows you can however use inlineNav. What I got working is below, there are some finicky parts but as far as add/edit/delete on local data it works.

     $(document).ready(function () {
     var myData = [],
            editOptions = {},
            addOptions = {},
            lastSel = -1,
            parameters = {
                edit: false,
                editicon: "ui-icon-pencil",
                add: true,
                addicon: "ui-icon-plus",
                save: true,
                saveicon: "ui-icon-disk",
                cancel: true,
                cancelicon: "ui-icon-cancel",
                addParams: { useFormatter: false },
                editParams: {}
            },
            myDelOptions = {
                onclickSubmit: function (options, rowid) {
                    var grid = $('#engineerGrid');
                    // delete the row
                    grid.delRowData(rowid);                     
                    grid.trigger("reloadGrid", [{ page: 0}]);
                    return true;
                },
                processing: true
            };

     $('#engineerGrid').jqGrid({
         datatype: 'json',
         url: '@Url.Action("CreateProjectEngineerJQGridData", "Project")',
         colNames: ['Engineer Name', 'Engineer Type', '% Utilization'],
         colModel: [
         { name: 'name', index: 'name', width: 150, align: "left", editable: true, edittype: "select",
             editoptions: { value: "@(Model.engineerOptions)" }, sorttype: 'string', formatter: 'select'
         },
         { name: 'type', index: 'type', width: 150, align: "left", editable: true, edittype: "select",
             editoptions: { value: "@(Model.typeOptions)" }, formatter: 'select'
         },
         { name: 'ut', index: 'ut', width: 125, align: "left", editable: true, formatter: 'number',
             formatoptions: { decimalPlaces: '0', defaultValue: '20'}
         }
            ],
         rowNum: 20,
         rowList: [5, 10, 20],
         pager: '#pager',
         gridview: true,
         rownumbers: true,
         autoencode: true,
         ignoreCase: true,
         sortname: 'invdate',
         viewrecords: true,
         sortorder: 'desc',
         loadonce: true,
         width: 750,
         caption: 'Project Engineers',
         editurl: 'clientArray',
         onSelectRow: function (id) {
         //if selected row changed restore values of previously selected row
             if (id && id !== lastSel) {
                 jQuery('#engineerGrid').restoreRow(lastSel);
                 lastSel = id;
             }
             jQuery('#engineerGrid').editRow(id, true);
         }
     }).jqGrid('navGrid', '#pager', { edit: false, add: false, del: true }, editOptions, addOptions, myDelOptions);
     jQuery("#engineerGrid").jqGrid('inlineNav', '#pager', parameters);
 });

Anyway, thanks for the help Oleg!

tereško
  • 58,060
  • 25
  • 98
  • 150
John Pires
  • 13
  • 1
  • 4

1 Answers1

2

The demo from my old answer contains really long and hard core code. It's based on playing with internal parameter processing: true. I agree that it's difficult for the newbie, but till now jqGrid still don't support local form editing.

The part of the code with delSettings is not so complex. So you can use it. For the row editing and inserting of the new row you can use inline editing. I personally find the editing mode the most comfortable because the user still see all the data on it's original place. So you can use it instead of form editing. If you need table used not for editing, but for view of the data then I would use in the case ondblClickRow callback to start editing. If you need the grid for editing only you can start editing on row selection: onSelectRow.

If you like navigator toolbar, then you can use navGrid only for "Delete" operation and inlineNav for "Add" and "Edit" operations. The method inlineNav is still relatively new and probably still not perfect but it can good be used.

As alternative to navigator toolbar you can use formatter: 'actions' in an additional column with "edit" and "del" buttons. You can also combine all the above methods corresponds to your taste.

On the official demo page you will find examples of all the ways.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thank you very much for the response Oleg! I'd like to make sure I'm understanding you correctly if that's ok. Form editing is not supported for local data. It can be accomplished by writing custom code but this is difficult. If I want to allow for adding/deleting/editing on local data I could accomplish this by using inline editing for editing rows and inserting new rows which IS supported for local data. For deleting local data I could still use navGrid. – John Pires Apr 19 '12 at 21:32
  • @JohnPires: You are welcome! It seems to me that you full understand me. The word "supported" which we both used is probably not full correct because we speak about open source product which one get for free. I mean mostly that the usage of "internals" like `processing: true` can follow that the code will not work in the next version, but the code which I posted more as year before still run. So you should decide yourself which way are better for your requirements and your current project. – Oleg Apr 19 '12 at 21:44