0

I have create a gird uing jqgrid and in that i am using form dialog to add , edit and delete record.

but in Edit Record i want to pass the id of a record along with the url even though form data will contain that , as i am using RESTful url so to update a record i have to pass the respective ID. as follow

www.example.com/eventInfo/10 , Method is PUT 

so far i have tried

jQuery("#eventGrid").jqGrid('navGrid','#pager', {}, //options 

        {
                height:280,
                reloadAfterSubmit:false , 

               // How can i add Event id over here from grid 

                url:'http://www.eample.com/evenInfo',
                mtype:'PUT'


        }, // edit options 
        {
                height:280,
                reloadAfterSubmit:false
        }, // add options 
        {
                reloadAfterSubmit:false
        }, // del options
        {

        } // search options 


    ); 

Moreover i want to send data to server in a JSON format rather then as form data

Hunt
  • 8,215
  • 28
  • 116
  • 256

1 Answers1

1

I described here detailed what you should do in case of working with RESTful services. The callback onclickSubmit is the best place to modify URL dynamically and append the id to it.

The current code of jqGrid which are on the github set the DOM element of the grid as this. So you will be able to use the following

onclickSubmit: function (options, postdata) {
    options.url += '/' + encodeURIComponent(postdata.[this.id + "_id"]);
}

in the next (after 4.3.1) version of jqGrid. In the current version of jqGrid the code will be about the following

var $grid = $("#eventGrid");

// set defaults for Delete form
$.extend($.jgrid.del, {
    mtype: "DELETE",
    reloadAfterSubmit: false
    serializeDelData: function () {
        return ""; // don't send and body for the HTTP DELETE
    },
    onclickSubmit: function (options, postdata) {
        options.url += '/' + encodeURIComponent(postdata);
    }
});

// set defaults for Edit and Add forms
$.extend($.jgrid.edit, {
    height: 280,
    reloadAfterSubmit: false,
    onclickSubmit: function (options, postdata) {
        // options.gbox will be like "#gbox_eventGrid"
        var gridId = options.gbox.substr(6), // cut id from the gbox selector
            id = postdata.[gridId + "_id"];
        if (id !== "_empty") {
            options.url += '/' + encodeURIComponent(id);
        }
    },
    afterSubmit: function (responseData) {
        // in case of usage reloadAfterSubmit: false it's important
        // that the server returns id of the new added row
        // in the simplest form the server response should just contain
        // the id. In more complex response one should modify the next line
        // to extract the id only from the response
        return [true, '', responseData];
    }
});

// create jqGrid
$grid.jqGrid({
    // all other parameters
    editurl: "/eventInfo" // you should use relative paths
});

// create navigator layer
$grid.jqGrid('navGrid', '#pager', {/*navGrid options*/}, { mtype: 'PUT'}); 

In the above code I added the afterSubmit which is important because you use reloadAfterSubmit: false option.

Remark: I typed mostly the code above or used cut & paste to copy some parts from another old answers. So you should test the above code in your application.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • options.url prints null in console log so it doesnt append to eventInfo , though options.editurl has a valid url , why is that so – Hunt Mar 29 '12 at 11:43
  • Can you tell me , how can i send my data in a json format rather then as Form Data ? – Hunt Mar 29 '12 at 11:48
  • 1
    @Hunt: You should use `serializeRowData` which use `JSON.stringify`. See [the answer](http://stackoverflow.com/a/7365228/315935) for details. – Oleg Mar 29 '12 at 12:01
  • serializeRowData or serializeRowData , i guess for form dialog i will be needing serializeRowData right ? – Hunt Mar 29 '12 at 12:29
  • is it possible to show message when the edit is done successfully , i tried that in afterSubmit function by returning return [true, responseData.statusText, responseData] but the "Created" Message doesn't shown up – Hunt Mar 29 '12 at 12:40
  • 1
    @Hunt: Look at [the answer](http://stackoverflow.com/a/6803206/315935) and [the corresponding demo](http://www.ok-soft-gmbh.com/jqGrid/SimpleLocalGridWithMsgInEditForm.htm) which just simulate successful message which disappear after 3 seconds and with display permanent error to simulate how `errorTextFormat` can be implemented. You can move the part of the code from the demo inside of `afterSubmit`. – Oleg Mar 29 '12 at 12:50
  • Oleg , thanks a ton for the quick responses 1 for all , well i got last question why eveytime postdata returns the row id rather then the actual id of the data which is inside the table ,As for delete item i will be needing the actual id rather then row id – Hunt Mar 29 '12 at 12:57
  • @Hunt: I don't understand your question. Probably you *fill* the grid in not quite correct way. The id which will be sent to the server is the actual id. It should by no differences between "row id" and "actual id". Only if jqGrid import wrong data it have to generate the row id. – Oleg Mar 29 '12 at 13:09
  • Well, in my case the rowid is the row number starting from 1 , 2 and so on while my ids are 101 , 102 and so on. Instead of 101 or 102 jqGrid picks up , 1 or 2 – Hunt Mar 29 '12 at 13:16
  • @Hunt: One more general remark. I suppose you not full understand the mean of voting. You have right to vote up about 30 questions and answers **per day** (see [here](http://meta.stackexchange.com/a/5213/147495)). Voting is mostly not to increase somebody's reputation. It helps *other visitors* to find helpful information. If I have to post you link to existing answer it's mostly because you couldn't find it during searching. The searching engine use voting as the most important criteria. So if you want to help other to search better on stackoverflow *please vote up all helpful what you find*. – Oleg Mar 29 '12 at 13:16
  • @Hunt: The example shows that you use either wrong jqGrid parameters or wrong JSON input data. I suggest that you open new question and post both jqGrid definition and the JSON data (one line would be enough). I'll write you how you can fix the problem. By the way: Which version of jqGrid you use? – Oleg Mar 29 '12 at 13:18
  • @Hunt: Voting of comments is important only for persons who find the answer and read it. If somebody see some voted comment he will read this. If you will try **to search** for the text in the comment you will never find it. So only the text from the question or answers will be really indexed and the voting of questions and answers are almost the only important *to weight* the importance of the information. – Oleg Mar 29 '12 at 13:24
  • @Hunt: I personally have enough reputation which I can't ever use. It's pity to repeat and write the same answers on the same question. I have to include my own answers to favorites because even if I use "user:me" prefix in the search string and if I exactly know the answer which I want to find I can't find it very frequently. – Oleg Mar 29 '12 at 13:26