1

I am new to jqGrid and need some help on form add/edit/delete functionality. Havent found any relevant resources so far. My grid is displaying pop up on add/edit, also populating data on clicking edit, however I am not sure what should be javascript code to invoke the Web api to POST/PUT/DELETE the data.

Details below:

JSON data:

[{"Id":1,"BankId":2,"BankName":"State bank","EmployeeId":2539,"EmployeeName":"John C.","JoiningDate":"2005-07-05T00:00:00","SalaryAmount":50000.0,"Comments":""},
{"Id":2,"BankId":2,"BankName":"State bank","EmployeeId":2232,"EmployeeName":"xxx","JoiningDate":"2001-12-23T00:00:00","SalaryAmount":30000.0,"Comments":"test"},
{"Id":3,"BankId":4,"BankName":"National bank","EmployeeId":2322,"EmployeeName":"yyyy","JoiningDate":"2002-09-23T00:00:00","SalaryAmount":90000.0,"Comments":""},
{"Id":4,"BankId":3,"BankName":"Punjab bank","EmployeeId":2432,"EmployeeName":"ppp","JoiningDate":"2003-01-31T00:00:00","SalaryAmount":60000.0,"Comments":" "},
{"Id":5,"BankId":1,"BankName":"Bank of Maharashtra","EmployeeId":2892,"EmployeeName":"zzz y.","JoiningDate":"2000-10-11T00:00:00","SalaryAmount":80000.0,"Comments":"test 2"}
]

Javascript for jqGrid:

    jQuery(document).ready(function () {
        jQuery("#employeeSalarysGrid").jqGrid({
            height: 250,
            url: 'http://localhost:50570/api/Test/GetEmployeeSalaries',
            mtype: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            serializeGridData: function (postData) {
                return JSON.stringify(postData);
            },
            jsonReader: {
                root: function (obj) { return obj; },
                page: function (obj) { return 1; },
                total: function (obj) { return 1; },
                records: function (obj) { return obj.length; },
                id: "0",
                cell: "",
                repeatitems: false
            },
            datatype: "json",
            colNames: ['Id', 'Bank Name', 'Employee name', 'Joining date', 'Salary amount', 'Comments'],
            colModel: [
                { name: 'Id', align: "center", hidden:true},
            { name: 'BankName', align: "center", editable: true },
            { name: 'EmployeeName', align: "center", editable: true },
            { name: 'JoiningDate', align: "center", editable: true },
            { name: 'SalaryAmount', align: "center", editable: true },
            { name: 'Comments ', align: "center", editable: true }
            ],
            gridview: true,
            autoencode: true,
            ignorecase: true,
            loadonce: true,
            sortname: "InstallmentDate",
            sortorder: "asc",
            viewrecords: true,
            rowNum: 10,
            rowList: [10, 15, 20],
            pager: '#employeeSalarysPager',
            caption: "Employee Salary list"
        });

         $("#employeeSalarysGrid").jqGrid('navGrid', '#employeeSalarysPager',
            {
                add: true,
                edit: true,
                del: true
            },
            editOption,
            addOption,
            delOption);

        var editOption =
            {
                width: 400, height: 290, left: 20, top: 30,
                reloadAfterSubmit: false, jqModal: false, editCaption: "Edit Record",
                bSubmit: "Submit", bCancel: "Cancel", closeAfterEdit: true,
                mtype: "POST",
                url: 'http://localhost:50570/api/Test/'
            };

        var addOption = {
            width: 400, height: 290, left: 20, top: 30,
            reloadAfterSubmit: false, jqModal: false, addCaption: "Add Record",
            bSubmit: "Submit", bCancel: "Cancel",
            closeAfterAdd: true,
            mtype: "PUT",
            url: 'http://localhost:50570/api/Test/'
        };

        var delOption = {
            caption: "Delete",
            msg: "Delete selected record(s)?",
            bSubmit: "Delete", bCancel: "Cancel",
            mtype: "DELETE",
            url: 'http://localhost:50570/api/Test/'
        };

    });

Server side API signatures:

 public HttpResponseMessage Post(int id, DTOTest value)
 public HttpResponseMessage Put(DTOTest value)
 public HttpResponseMessage Delete(int id)

Please let me know what is wrong with the code. Methods are not getting invoked. Am i missing anything in html code for jqGrid, OR are the signatures on server code needs to be modified? Looking forward for some pointers.

Many thanks,

Abhilash

AbSharp
  • 119
  • 2
  • 6
  • 20

2 Answers2

1
`var URL = 'rest/book';`


...
var delOptions = { onclickSubmit: function(params, postdata) { params.url = URL + '/' + postdata; } };

you mean this?


it might need "editurl" in your grid instead of "url" in the del(add/edit)Option,like this:

...

height: 250,
editurl: 'http://localhost:50570/api/Test/',
url: 'http://localhost:50570/api/Test/GetEmployeeSalaries',
mtype: "GET",
contentType: "application/json; charset=utf-8",

... Have a try ?

Daniel Li
  • 36
  • 4
  • Thanks GreenHand for detailed response. It helps, however i need some more help on handling add/edit/delete. Where do you set to url of web api and action (POST/PUT/DELETE)? How do you post the newly added object (for ADD) OR modified object (for EDIT) to the web api? – AbSharp Mar 19 '14 at 04:17
  • @GreenHand- I have updated code in original post, to show you specific area where i need help. – AbSharp Mar 19 '14 at 04:38
  • @Abhilash I found src at this site,and hope it could help you: http://www.javacodegeeks.com/2011/07/jqgrid-rest-ajax-spring-mvc-integration.html – Daniel Li Mar 19 '14 at 06:28
  • I tried that, but that too didnt work. I get message saying "No url is set". Could you please try building out the sample with the same code that I have provided? Many thanks! – AbSharp Mar 19 '14 at 12:32
  • In which situation you got the mes?and,may i have you js src? – Daniel Li Mar 19 '14 at 13:00
  • I have mentioned all the code in original post. And I am using latest version of jqQuery 4.6.0 from - http://www.trirand.com/blog/?page_id=6 Please let me know if you need any more details from me. Thank you! – AbSharp Mar 19 '14 at 13:05
  • @Greenhand- Nope, that too didnt work. Could you please try out demo using my code and share the code if the demo works? – AbSharp Mar 23 '14 at 17:15
0

You need to add URL parameter in your editOption, addOption, deleteOption

var editOption = {
    width:400,
    height:290,
    left:20,
    top:30,
    reloadAfterSubmit:false,
    jqModal:false,
    editCaption: "Edit Record",
    bSubmit: "Submit",
    bCancel: "Cancel",
    closeAfterEdit:true,
    url:'http://localhost:50570/api/Test/EditEmployee'
};

var addOption = {
    width:400,
    height:290,
    left:20,
    top:30,
    reloadAfterSubmit:false,
    jqModal:false,
    addCaption: "Add Record",
    bSubmit: "Submit",
    bCancel: "Cancel",
    closeAfterAdd:true,
    url:'http://localhost:50570/api/Test/AddEmployee'
};

var delOption = {
    caption: "Delete",
    msg: "Delete selected record(s)?",
    bSubmit: "Delete",
    bCancel: "Cancel",
    url:'http://localhost:50570/api/Test/DeleteEmployee'
};
IT ppl
  • 2,626
  • 1
  • 39
  • 56
  • 1
    Please see this link : http://www.codeproject.com/Articles/609442/Using-JqGrid-in-ASP-NET it will help you for sure – IT ppl Mar 19 '14 at 05:40
  • Thanks @IT ppl, however I had already referred this link, it works with ashx handlers where as I need to invoke simple Web API. I have updated the original post now to include web api signatures as well. – AbSharp Mar 19 '14 at 12:49