0

JqGrid acting strange. I just create some master page which contains jqGrid refering to some table in my database.

This page is working pefectly, then i need to use the same logic and i copied exact page with additional php file and create some adjustment so it pointing to the right table.

In my master page (department page --which is the first page (works 100%)) the jqgrid function working normal, BUT in my second and third page which is based on the first page, the jqgrid is acting strange.

When i create new value or update some value, the jqgrid should be automatically reloads the grid with NEW data. But in my case, the grid reloads the data but NOT writing at all.

This strange behaviour is NOT happened in my first jQGrid page (dept page).

I also insert some screenshot to make it more clear

First, i reload the page. The firebug console says, it's load JSON data from server

Then simple add value to the grid, referring to some php file. The value is executed and then STORED in database. This method using POST method.

sending the POST data from the textbox to db

Then the jQgrid automatically GET the new data from database, and should be Write on the GRID. But in my CASE, the grid is not writing the data.

GET NEW Data and it should be write in grid, but it's not :(

As you can see the the bottom right of the screen shot, there are 11 values, from the 1st screenshot, there are only 10 values. So, the grid actually execute the INSERT statement, but when it reloads, it's broken.

Is there any possibilities how to overcome this?? Thank you.

EDITED: HTML code:

<table id="location"><tr><td /></tr></table>
<div id="pager-location"></div>

JavaScript code:

$(document).ready(function() {
    //alert("start");
    jQuery("#location").jqGrid({
        mtype:'GET',
        url:'functions/get_location.php',
        editurl:'functions/edit_location.php',
        datatype: "JSON",
        colNames:['Location ID','Location'],
        colModel:[
            {name:'idms_location',index:'idms_location', width:150, editable:true,add:true, del:true, key:true},
            {name:'location',index:'location', width:800,editable:true, add:true, del:true}     
        ],
        loadComplete: function () {
        alert("OK");
        },    
        loadError: function (jqXHR, textStatus, errorThrown) {
            alert('HTTP status code: ' + jqXHR.status + '\n' +
                  'textStatus: ' + textStatus + '\n' +
                  'errorThrown: ' + errorThrown);
            alert('HTTP message body (jqXHR.responseText): ' + '\n' + jqXHR.responseText);
        },
        rowNum:10,
        rowList:[5,10,15],
        pager: '#pager-location',
        sortname: 'idms_location',
        viewrecords: true,
        jsonReader: {repeatitems: true, idms_location: "idms_location" },
        sortorder: "asc",
        caption:"MSC Locations"
    });
    jQuery("#location").jqGrid('navGrid','#pager-location',{edit:true,add:true,del:true},{},{},{},{closeAfterSearch:true},{});
    jQuery("#location").jqGrid('gridResize',{minWidth:350,maxWidth:850,minHeight:80, maxHeight:350});
    //alert("end");
});
Oleg
  • 220,925
  • 34
  • 403
  • 798
randytan
  • 1,029
  • 5
  • 23
  • 52

1 Answers1

1

I verified the code which you used and found the reason. You had id duplicate problem in your code. You defined <table> element used for jqGrid as following

<table id="location"><tr><td /></tr></table>
<div id="pager-location"></div>

It has "location" as the id. Later you defined

colModel: [
    {name:'idms_location',index:'idms_location', width:150, editable:true,add:true, del:true, key:true},
    {name:'location',index:'location', width:800,editable:true, add:true, del:true}     
],

where the name location will be used as the column name. The problem is that the column name will be used to build id name of different elements of the grid. Moreover the form editing uses the column name direct as id value of the <input> field which represent location. After usage of Add form the following element

<input name="location" class="FormElement ui-widget-content ui-corner-all" id="location" role="textbox" type="text">

exists on the page with id="location" too. If thhe user close the form it will be hidden, but not destroyed. Because the edit form will be placed on the page before <table id="location"> the next $("#location tbody:first") used in the line don't find the table more and the grid stay empty.

What you should do is just rename <table id="location"> to something like <table id="grid-location">` or choose any other name. You should update the JavaScript code corresponded.

Other changes which should be done in the grid:

  • change jsonReader: {repeatitems: true, idms_location: "idms_location" } to jsonReader: {id: "idms_location" }.
  • add gridview: true option.
  • add autoencode: true option.
  • remove non-existing options add:true, del:true properties from colModel
  • remove index properties from colModel.
  • you should fix Content-Type HTTP header which you use in the server response with JSON data. It should be Content-Type: application/json instead of Content-Type: text/html which you use currently. It's just one line of PHP code.
  • you can remove {edit:true,add:true,del:true} options of navGrid - it's default options.
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • i see, it works now, BUT one more question, WHY is this working in departments page, but in the other it is not works? – randytan Jul 02 '13 at 09:04
  • @randytan: You used **different** names on Departments page. The table has `id="departments"` (with "s" at the end) and column name was `"department"` (without "s" at the end). – Oleg Jul 02 '13 at 09:07
  • OMG! just because 's'. That's makes me really confuse today. Thanks for a GREAT help oleg! Have a nice day. – randytan Jul 02 '13 at 09:09
  • @randytan: One more remark: you almost don't use voting of answers and questions. It's very important right because voting is the main criteria used by searching engine. You have right to vote 30 questions or answers **per day** (see [here](http://meta.stackexchange.com/a/5213/147495)). So if you want help other users to find some answer which you find *helpful* you should vote it up. I personally have enough reputation which I can't use. I want just help other users who have the same problems to find answers. You can vote not only the answers on your questions, but any information which helps – Oleg Jul 02 '13 at 09:12
  • i see, i just asking question here, and haven't read the stackoverflow 101. hahaha. Anywany oleg, it seems that if i want to add new values, it seems only 10 character is fit in. where i must add some script? – randytan Jul 02 '13 at 09:19
  • @randytan: Sorry, but I can't follow you. Which "new values" you mean? which 10 characters? – Oleg Jul 02 '13 at 09:21
  • yes, for example, i just want to add new values for location, i add the location-id and location name, but it seems that location name only allowed to store 10 characters. Because after i click submit, only first 10 character is stored in my db. – randytan Jul 02 '13 at 09:23
  • @randytan: I see no problems. I just added "USA 12345678901234567890" in the Location and it was successfully saved. – Oleg Jul 02 '13 at 09:27
  • it seems not working in EDIT function in my local machine on status tab, i will try to upload it first. please wait. – randytan Jul 02 '13 at 09:32
  • @randytan: By the way I see that you use font-awesome. I reccomend you to read in the case [the answer](http://stackoverflow.com/a/13865495/315935). – Oleg Jul 02 '13 at 09:50
  • @randytan: I strictly recommend you include `` with `"Content-Type"` on your page and with `"X-UA-Compatible"` having `"IE=edge"`. For example `` or `` (HTML5) and `` or set "X-UA-Compatible" in the HTTP header (see [here](http://msdn.microsoft.com/en-us/library/jj676913(v=vs.85).aspx)) – Oleg Jul 02 '13 at 09:56
  • hi, thanks for your reference guide, i just see that, and i will try to use that. anyway, it seems that my status page is error i can't edit the value or add the value more than 10 char, do you see status ID 55? just edit it to 111111112222222222 and the rest '2' is ignored, the rest page is working nicely, except the user. i haven't implemented it. I think something wrong with the php code. I will try to debugging it first. – randytan Jul 02 '13 at 10:10
  • thanks for your help @Oleg, I know the problem now. I just ask Database Team and they set the max char is 10. hahaha.. I appreciate for your great help! See ya – randytan Jul 02 '13 at 10:14
  • @randytan: I can see that it I enter `12345123451234512345` in the Status the response from the server contains the text `Failed : Data too long for column 'status' at row 1`, but the response contains **successful** status `HTTP/1.1 200 OK` which is wrong. You should returns some error HTTP status on errors on the server side. Only it it is really impossible (in PHP it's possible) one can use [afterSubmit](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:form_editing#events) callback to inform jqGrid that the response was error message. – Oleg Jul 02 '13 at 10:23
  • @randytan: In any way it's not a problem of jqGrid. It's problem of your server code or database. – Oleg Jul 02 '13 at 10:24
  • @randytan: You still don't included `header('Content-type: application/json');` in the server response. To set error code you can use `http_response_code(500);` or simply use `die()` statement. – Oleg Jul 02 '13 at 10:33
  • hi @oleg, thanks for your correction. I've add the function in my local machine, but haven't upload it to the server. :) – randytan Jul 02 '13 at 13:29
  • Hi oleg, can i ask you something, can i create two ids in one grid? because i've search and getting no result. like id1 = "email" and id2 = "location" ?? thanks for helping. – randytan Jul 04 '13 at 08:18
  • @randytan: No, it's not possible. I think you try to use id in the wrong way. Which problem you try to solve? Sometime it could be practical to assign multiple *classes* to an element. Multiple elements on the page can have multiple classes and every element can also have multiple classes. Probably you can describe the problem in new question? I will try to help you if I can. – Oleg Jul 04 '13 at 08:31
  • i have create new question here: http://stackoverflow.com/questions/17465747/two-parameter-for-deleting-a-row-in-jqgrid – randytan Jul 04 '13 at 08:44
  • is it possible like my post at new thread? if not, i will change my method to delete – randytan Jul 04 '13 at 10:04