Forum
Topic RSS
17:23
19/03/2010
OfflineUsing jqGrid 3.6.4
In addition to my previously reported problems with having periods in the ID, simply using a function to escape the periods and colons is not a complete solution. Many places in the code of jqGrid appear to use the unescaped ID and I would imagine changing this would be a major undertaking. However, there are a multitude of characters that are technically invalid to be used in HTML element ID attributes, which are limited by the SGML NAME standard (must start with a letter and contain only letters, numbers, hyphens, underscores, colons and dots. However, from a data standpoint, the ID that the server (i.e. a database schema) uses could contain just about any character. Obviously these two paradigms are going to create a HUGE problem. Unless one subscribes to the notion that EVERY db table contain an auto-increment/sequence as the PK (I won't get into debating the merits or lack thereof here).
As I see it, the only two possibilities for fixing this error are:
A) the developer must find someway to encode the ID so that it does not contain any characters which may cause problems.
B) jqGrid must not base HTML element IDs on the ID from the data.
I shall look into option A and report back my progress. Base64 encoding has SOME possibilities. Unfortunately, Base64 encoded output contains 3 illegal characters: +, / and = (= used for padding). Those could be replaced with valid characters, except the only characters allowed for an ID attribute that aren't already part of Base64 encoding are hyphens, underscores, periods and colons. Since periods and colons are out, we only have two possible values to replace 3 characters.
Option B is beyond my perview. However, not being familiar with the code (and lacking a strong enough background in JS) I wonder if it might be possible to use generated IDs rather than using the row_id as the basis of creating the element iDs? Having just started with jqGrid, I'm not sure where this might introduce a problem. For subgrids, the function defined in subGridRowExpanded() is passed the subgrid_id (the div created by jqGrid to contain the subgrid) as well as the row_id. Obviously this means the subgrid_id does not need to be based on the row_id.
If someone could point me to where in the code the div ID is determined, I would be happy to test this out by changing the derived ID to a generated ID.
I suppose I could change all of my thousands of existing DB tables to use sequences for PK but besides grating on my sense of what are and aren't DB best practices and the amount of work involved, this doesn't solve the problem for anyone else that may run afoul of this issue.
18:16
19/03/2010
OfflineOK, I think I've come up with a solution to Option A.
As mentioned, Base64 includes 3 characters that are NOT valid in element IDs: '+', '\' and '=' and we have only two available characters to use to replace them: '-' and '_'.
However, the '=' only appears on the END of the encoding, as padding characters to even the bit count. So, the solution I've devised replaces the '+' and '\' with '_' and '-' respectively and replaces the trailing '='s with the count of the number of '='. Since the number of equal signs appearing is either 0, 1 or 2, we know we are never adding more than one character.
Obviously, to decode this, we simply need to replace the last character with the number of =s it designates, replace the '_' and '-' with '+' and '\' respecively and base64 decode the resulting string to get back our original ID.
The following two functions in PHP will do just that:
function idencode($s) {
$r = base64_encode($s);
// replace + and /
$r = str_replace('+', '_', $r);
$r = str_replace('/', '-', $r);
// replace trailing "=" with the count of number of "="
$eqpos = strpos($r, '=');
if($eqpos===false) {
$count = 0;
}
else {
$count = strlen($r) - $eqpos;
// remove =
$r = str_replace('=', '', $r);
}
// add count
$r .= $count;
return $r;
}
function iddecode($s) {
print "$s\n\n";
// replace "=" at end
$count = substr($s, -1);
$r = substr($s, 0, -1);
if($count>0) {
for($i=0;$i<$count;$i++) {
$r .= '=';
}
}
// replace - and _
$r = str_replace('_', '+', $r);
$r = str_replace('-', '/', $r);
return base64_decode($r);
}
So far, my tests using this with jqGrid have been successful.
Tony, you might want to consider adding this to the documentation.
09:00
Moderators
30/10/2007
OfflineHello,
Thank you for the investigations and recommendations.
I think that there will be a long disscusion on should we have id or not, should we have on every DB table
uniquie id or not. All these are up to developer and for the concrete project.
jqGrid actually have a lot of features and and you actually can not care about the ids.
If the id is not set or can not be found we use a counter.
I think that you can use the following code from jqGrid
jqID : function(sid){
sid = sid + "";
return sid.replace(/([\.\:\[\]])/g,"\\$1");
},
This function can be easy extended with characters what you want to be replaced - i.e after you load jqGrid js files you can do
$.extend($.jgrid,{
jqID : function (id) {
// your code here
}
Best 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.
Most Users Ever Online: 994
Currently Online:
55 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.comModerators: tony: 7721, Rumen[Trirand]: 81
Administrators: admin: 66
Log In
Home