Forum

November 2nd, 2014
A A A
Avatar

Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

The forums are currently locked and only available for read only access
sp_Feed Topic RSS sp_TopicIcon
How can the key name 'id' be changed in saveRow()
13/03/2009
08:58
Avatar
jnavratil
Member
Members
Forum Posts: 23
Member Since:
12/02/2009
sp_UserOfflineSmall Offline

I am writing a generic table editor for browsing my database.  My convention is to create primary key names using the table name (Users_UID, for example) and 'id' can be a column name of no particular interest.  When I call saveRow, I pass additional parametric data including the name of the primary key field which changes from table to table.

Obviously this parametric data cannot collide with column names of the table so I choose keys named 'jqGridTable', 'jqGridUIDCol', etc. which I keep as reserved words.  In my jsonReader parameter to jqGrid I coded

id : 'jqGridUIDCol'

in hopes that this value would be used as the key into $_POST.  It works reading, but no writing.

How can the output key name be changed so that saveRow emits $_POST['jqGridUIDCol'] instead of $_POST['id'] ?

13/03/2009
10:11
Avatar
jnavratil
Member
Members
Forum Posts: 23
Member Since:
12/02/2009
sp_UserOfflineSmall Offline

Tony,

I was able to achieve my desired result by changing line 122 of grid.inlinedit.js (should that name be inlineedit?) from...

if(tmp) { tmp["id"] = rowid; ....

to

if(tmp) { tmp[$t.p.jsonReader.id] = rowid; ....

but I don't think this is a real solution as it relies on the presence of the jsonReader object.  Still, something like this is needed to get rid of the magic 'id', don't you think?  Perhaps a test for a name before defaulting to 'id'?

-- John

13/03/2009
10:21
Avatar
tony
Sofia, Bulgaria
Moderator
Members

Moderators
Forum Posts: 7721
Member Since:
30/10/2007
sp_UserOfflineSmall Offline

Hello,

IMHO this solution is not applicable. One approach is to create a hidden coulmn and post it. Another is to use the extraparam to pass additional data to the post.

See docs

Regards

Tony

For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.

13/03/2009
15:14
Avatar
jnavratil
Member
Members
Forum Posts: 23
Member Since:
12/02/2009
sp_UserOfflineSmall Offline

Tony,

The problem is that if I have a table with a column named 'id', that column cannot be edited.  Generally, speaking the POST data sent to the server in insert and update are the column names and their values, but anything jqGrid adds to this data('id', e.g.) is added into the same namespace where collision is possible and special handling is required as indicated by the $exceptions list of fields in the server.php example.

For specific tables where 'id' is a column name, special handling at the server could select 'id' as somethingElse but it seems a more general solution is to separate these name spaces.

One possibility is to send only two variables via Ajax; the row data and other meta-data.  Or, perhaps, to put all row data into a json string or XML document and pass it as a single variable.

In this way, jqGrid would never have a conflict with a column name.  Do you agree?

Thanks!

-- John

16/03/2009
06:11
Avatar
tony
Sofia, Bulgaria
Moderator
Members

Moderators
Forum Posts: 7721
Member Since:
30/10/2007
sp_UserOfflineSmall Offline

Hello,

Before edit the row get the id of the column - let say myid, then

myextraparam = {jqGridUIDCol:myid}

$("#mygrid").saveRow(myid, false, myurl, myextraparam);

When you post the value from jqgrid use  jqGridUIDCol instead of id

Hope this helps

REgards

Tony

For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.

16/03/2009
12:23
Avatar
jnavratil
Member
Members
Forum Posts: 23
Member Since:
12/02/2009
sp_UserOfflineSmall Offline

Hello, Tony!

I do understand what you are saying. I have already used the extra parameters to communicate both the table name and primary column name.  The problem is purely in the collision between the jqGrid use of the 'id' keyword for the primary key value and a database column of the same name.

Here is an example using the 'invheader' test table.  My saveRow() code is...

jQuery(gridName).saveRow(rowID, '', 'tableEditor.php',

    { jqGridOp : 'updateRow', jqGridTable : tableName, jqGridUIDCol : UIDColumnName },
    aftersavefunc);

saveRow() generates a POST array which looks like
{"invdate"=>"2009-02-10", "client_id"=>"10", "amount"=>"123.45", "tax"=>"10.11", "total"=>"133.56", "note"=>"Test 1", "id"=>"1", "jqGridOp"=>"updateRow", "jqGridTable"=>"invheader", "jqGridUIDCol"=>"invid"}

So I use my 'jqGridUIDCol' as the name of the primary key column and the 'id' column as its value.  Problem solved as long as there is no other column in the table with the name 'id'.  If I change the name of the 'client_id' column in the database to 'id' and my jsonReader to

jsonReader: {
    root: 'rows',
    page: 'page',
    total: 'total',
    records: 'records',
    repeatitems: true,
    cell: 'cell',
    id: 'jqGridUIDVal'
},

The POST array generated by saveRow is {"invdate"=>"2009-02-10", "id"=>"1", "amount"=>"123.45", "tax"=>"10.11", "total"=>"133.56", "note"=>"Test 1", "jqGridOp"=>"updateRow", "jqGridTable"=>"invheader", "jqGridUIDCol"=>"invid"}.

Notice that there are now only 9 elements in the array.  The 'id' column again contains the primary key value, but the value for the non-key 'id' column (the one which was 'client_id') is lost because of the name collision.

I can see several ways to work around this.

(1) Modify the server code to detect and avoid conflicting names.  PRO - no change to jqGrid.  CON - must know all keywords ('oper', for example) used during an update or insert operation (the $exclusions) and change server code if jqGrid changes.

(2) Modify jqGrid to reference a keywords object when generating the GET/POST data which could be rewritten by the user if needed.  For example

var keywords {jqGridID : "id", jqGridOper : "oper"...};

PRO - no change to installed code.  Only minor modifications to jqGrid. All keywords are present in an object which can be transmitted to the server for exclusion handling.  CON - Modifications to jqGrid needed.

(3) Add a rowset (or other) keyword to contain all the row col/value pairs.  Leave other keywords/values as currently generated and deprecate.

PRO - complete separation of namespaces between rowset data and metadata. Existing code only lightly affected (must know to ignore the 'rowset' keyword).  Once converted, server code is more effectively separated from jqGrid.  CON - existing code may be affected.  More substantial code change to jqGrid.  Not as simple an interface to the server.

IMHO, choice (3) is the best.  Choice (2) does provide enough data to be able to detect the problem and a means for an easy work-around.

Thanks!

-- John

17/03/2009
01:55
Avatar
tony
Sofia, Bulgaria
Moderator
Members

Moderators
Forum Posts: 7721
Member Since:
30/10/2007
sp_UserOfflineSmall Offline

Hello,

Maybe you should try with the option key:true too, which have higger priority.

Regards

Tony

For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.

17/03/2009
08:57
Avatar
jnavratil
Member
Members
Forum Posts: 23
Member Since:
12/02/2009
sp_UserOfflineSmall Offline

Tony,

I added the { key : true } to my colModel object for my key column giving...

{ name: 'invid', index: 'invid', editable: false, key: true, width: 66, align: 'right' }

... and reran my test with a non-key column named 'id' (not the primary key).  The results were the same.  The POST array had an 'id' key with the primary key in it.  There was no 'invid' key.

Did I understand your suggestion correctly?

Thanks,

-- JOhn

20/03/2009
12:39
Avatar
jnavratil
Member
Members
Forum Posts: 23
Member Since:
12/02/2009
sp_UserOfflineSmall Offline

So, I guess option (1) is it.  I will prefix all my data with 'xx' and hope jqGrid never uses a key beginning with 'xx'.

Still it seems asymmetric as the jsonReader requires a separation of the 'id' from the 'cell' data.  It just doesn't work that way when writing.

Still, it's a great plugin!  Thanks!

Forum Timezone: Europe/Sofia

Most Users Ever Online: 715

Currently Online:
53 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

OlegK: 1255

markw65: 179

kobruleht: 144

phicarre: 132

YamilBracho: 124

Renso: 118

Member Stats:

Guest Posters: 447

Members: 11373

Moderators: 2

Admins: 1

Forum Stats:

Groups: 1

Forums: 8

Topics: 10592

Posts: 31289

Newest Members:

, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.com

Moderators: tony: 7721, Rumen[Trirand]: 81

Administrators: admin: 66

Comments are closed.
Privacy Policy   Terms and Conditions   Contact Information