Forum
16:59
10/08/2009
Hi Tony!
I had some problems with using of selects in the jqGrid 3.7.x where is set formatter:'select' on a column (and holding integer values in the data table instead of strings). As a workaround I find out a solution where I used function as a value of index property of the corresponding column.
Here is a small example which reproduce the problem and the workaround:
var categories = ["sport", "science"];
var subcategories = ["football", "formel 1", "physics", "mathematics"];
var mydata = [
{id:0, Name:"Lukas Podolski", Category:0, Subcategory:0},
{id:1, Name:"Michael Schumacher", Category:0, Subcategory:1},
{id:2, Name:"Albert Einstein", Category:1, Subcategory:2},
{id:3, Name:"Blaise Pascal", Category:1, Subcategory:3}
];
var grid = jQuery("#list").jqGrid({
data: mydata,
datatype: 'local',
colModel: [
{ name: 'Name', width: 200 },
{ name: 'Category', width: 200, formatter:'select', //sorttype:'int',
index: function(obj) {
return categories[obj.Category];
},
edittype:'select', editoptions: { value: categories }
},
{ name: 'Subcategory', width: 200, formatter:'select', //sorttype:'int',
index: function(obj) {
return subcategories[obj.Subcategory];
},
edittype:'select', editoptions: { value: subcategories} }
],
sortname: 'Name',
viewrecords: true,
rownumbers: true,
sortorder: "desc",
pager: '#pager',
caption: "Custom Local Sort"
}).navGrid('#pager', { edit: false, add: false, del: false, search: false, refresh:false });
Because I didn't seen the usage of of index property as function I want ask you to conform, that it is OK to do this.
Best regards
Oleg
16:43
Moderators
30/10/2007
Hello Oleg,
Thanks - this is a good recommendation, but it can not be realized currently.
The maydata in your case is stored into the grid as it is. Think of it as saved json request, which will be called with diffrent conditions.
If you perform sort we get the values from mydata and not the displayed one from the formatters.
Instead this is very usefull feature.
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.
21:13
10/08/2009
Sorry Tony, I understand that the current version jqGrid not use formatters for local data. Nevertheless atfer some debugging and examine of code, it seems to me that I do found a solution of the problem which I posted. This is the using of functions as index. The example which I posted seems good working. I asked the question mostly to be sure that the usage of functions as index is and will be a supported feature of jqGrid. It seems to work very good with local data. Please try the code example and say your opinion about. If you like this, it can be an oficial documanted feature of jqGrid and one could use it for any kind of custom sorting of local data.
Thanks
Oleg
23:32
Moderators
30/10/2007
Oleg,
Just found a very simple solution. Will publish it tommorow.
Also the idea is to define not a index, but a sorttype as function.
Just tested and work ok.
{ name: 'Category', width: 200, formatter:'select',
sorttype: function(cell) {
return categories[cell];
// celll represent values from 0 to 1 as of your data
},
Regards
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.
12:02
28/03/2010
I really need to make this idea of a custom sorttype work, and tried using Tony's idea, but can't make it work. Please, can anyone help!
I am using jqGrid 3.7.2 with local data. The code below is my best attempt at getting it to work - I can't make it call the custom sorting function though. The idea is to sort the 'Posn' field in the order 'GK'->'DEF'->'MID'->'STR'. Here is the code I'd like to get working:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Table Testbed</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/south-street/jquery-ui.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="/thirdParty/jqGrid/ui.jqgrid.css" >
<script type="text/javascript" src="/thirdParty/jqGrid/grid.locale-en.js"></script>
<script type="text/javascript" src="/thirdParty/jqGrid/jquery.jqGrid.min.js"></script>
<script type="text/javascript">
$().ready(function()
{
tableToGrid("#playerTable",
{
datatype: "local",
sortable: true,
hidegrid: false,
multiselect: false,
altRows: true,
height: "100%",
width: "155px",
shrinkToFit: true,
rowNum: 100,
colNames: ['Posn','Name'],
colModel: [
{name:'Posn', index:'Posn', width:100, sorttype:
function(cell)
{
if (cell=='GK') return '0';
if (cell=='DEF') return '1';
if (cell=='MID') return '2';
if (cell=='STR') return '3';
}
},
{name:'Name', index:'Name', width:200, sorttype:"text"}
]
});
});
</script>
</head>
<body>
<table id="playerTable">
<thead>
<tr><th>Posn</th><th>Name</th></tr>
</thead>
<tbody>
<tr><td>GK</td><td>Almunia</td></tr>
<tr><td>GK</td><td>Fabianski</td></tr>
<tr><td>DEF</td><td>Campbell</td></tr>
<tr><td>DEF</td><td>Clichy</td></tr>
<tr><td>MID</td><td>Denilson</td></tr>
<tr><td>MID</td><td>Diaby</td></tr>
<tr><td>STR</td><td>Arshavin</td></tr>
<tr><td>STR</td><td>Bendtner</td></tr>
</tbody>
</table>
</body>
</html>
13:39
10/08/2009
Hi cjm19682,
you should use the latest version of jqGrid published on http://github.com/tonytomov/jq.....ree/master after the release of 3.7.2. Then you will be able to use sorttype as function.
Best regards
Oleg
14:02
10/08/2009
Hi Tony!
I am now back from the vocation and could test the new release of jqGrid with sorttype as function. Thank you very much! It works!
One more information about my original suggestion with the usage of index as a function. It works good with jqGrid 3.7 and 3.7.1, but don't work more on 3.7.2. The function from the index will be forward (in the version 3.7.1) to the expr parameter of getAccessor function. A working example with jqGrid 3.7.1 you can see here http://www.ok-soft-gmbh.com/jq.....alSort.htm
The solution with sorttype as function I find very good. The most advantage which I see in the place is because sorttype are have sence only with the local data, but index on the other side not only for the local data.
The only restriction of the current implementation of sorttype as function is that it has only one parameter cell (getAccessor function has obj parameter instead), but no other information about the object which are sorted. This chould be not enough for some advance scenarios. So what do you think about the changing of the following lines
ab = $.jgrid.getAccessor(v,by);
if(ab === undefined) { ab = ""; }
ab = findSortKey(ab);
_sortData.push({ 'vSort': ab,'index':i});
to
ab = $.jgrid.getAccessor(v,by);
if(ab === undefined) { ab = ""; }
ab = findSortKey(ab,v);
_sortData.push({ 'vSort': ab,'index':i});
Then findSortKey will have additional second parameter with full row contain (only without current id). Probably id as a third parameter could be also helpful.
What do you think about, Tony?
Best regards
Oleg
15:50
Moderators
30/10/2007
Hello Oleg,
Thanks. As usuall very usefull - Added in 3.8.
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.
12:10
Moderators
30/10/2007
Hello Oleg,
Could you please try with the latest from GitHub - I mean to test a index as function.
Kind 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.
08:41
Moderators
30/10/2007
Hello Oleg,
No I mean this
http://www.ok-soft-gmbh.com/jq.....alSort.htm
with the latest from GitHub.
I just have test it (getting your code) and it works with the latest 3.8
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.
10:09
10/08/2009
Hi Tony!
You are right, the version seems work without any problem, but I made the test with another javascript which use filterToolbar function. A working version you can find here http://www.ok-soft-gmbh.com/jq.....Filter.htm. It is an answer on the question http://stackoverflow.com/quest.....ery-jqgrid. The same javascript which use 3.8 and index as function have at the beginning the problem in the line 736 of grid.custom.js:
$(elem).attr({name:cm.index || cm.name, id: "gs_"+cm.name});
and then in the line 540
v = $("select[name="+nm+"]",$t.grid.hDiv).val();
where nm are set in the line 536 as
nm = this.index || this.name;
You can see the problem online http://www.ok-soft-gmbh.com/jqGrid/CustomLocalSort4.htm.
Best regards
Oleg
10:47
Moderators
30/10/2007
Thanks Oleg. Will look at this as soon as I have time.
Best Regsrds
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: 715
Currently Online:
51 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