Forum

May 22nd, 2014
A A A

Please consider registering
guest

Log In Register

Register | Lost password?
Advanced Search

— Forum Scope —

  

— Match —

   

— Forum Options —

    

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

Topic RSS Related Topics
custom local sort with respect of the function as index
16/07/2010
16:59
OlegK
Germany
Member
Forum Posts: 1233
Member Since:
10/08/2009
Offline

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

19/07/2010
16:43
tony
Sofia, Bulgaria
Moderator
Forum Posts: 7693
Member Since:
30/10/2007
Offline

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 PHP, ASP.NET and ASP.NET MVC visit us at our commercial products site - trirand.net - by the very same guys that created jqGrid.
19/07/2010
21:13
OlegK
Germany
Member
Forum Posts: 1233
Member Since:
10/08/2009
Offline

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

19/07/2010
23:32
tony
Sofia, Bulgaria
Moderator
Forum Posts: 7693
Member Since:
30/10/2007
Offline

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

        _stripNum = /[\$,%]/g,
For professional UI suites for PHP, ASP.NET and ASP.NET MVC visit us at our commercial products site - trirand.net - by the very same guys that created jqGrid.
24/07/2010
12:02
cjm19682
Member
Forum Posts: 9
Member Since:
28/03/2010
Offline

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>

26/07/2010
13:39
OlegK
Germany
Member
Forum Posts: 1233
Member Since:
10/08/2009
Offline

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

26/07/2010
14:02
OlegK
Germany
Member
Forum Posts: 1233
Member Since:
10/08/2009
Offline

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

05/08/2010
15:50
tony
Sofia, Bulgaria
Moderator
Forum Posts: 7693
Member Since:
30/10/2007
Offline

Hello Oleg,

Thanks. As usuall very usefull - Added in 3.8.

Regards

Tony

For professional UI suites for PHP, ASP.NET and ASP.NET MVC visit us at our commercial products site - trirand.net - by the very same guys that created jqGrid.
07/08/2010
12:10
tony
Sofia, Bulgaria
Moderator
Forum Posts: 7693
Member Since:
30/10/2007
Offline

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 PHP, ASP.NET and ASP.NET MVC visit us at our commercial products site - trirand.net - by the very same guys that created jqGrid.
07/08/2010
21:46
OlegK
Germany
Member
Forum Posts: 1233
Member Since:
10/08/2009
Offline

Hello Tony!

Probably you mean sorttype as function (index as function not work starting with 3.7.2, but one way with sorttype is enough). It works perfect! The second parameter can be very usefull.

If I'll find some problems later I'll post there in the forum.

Thank you!
Oleg

09/08/2010
08:41
tony
Sofia, Bulgaria
Moderator
Forum Posts: 7693
Member Since:
30/10/2007
Offline
11

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 PHP, ASP.NET and ASP.NET MVC visit us at our commercial products site - trirand.net - by the very same guys that created jqGrid.
09/08/2010
10:09
OlegK
Germany
Member
Forum Posts: 1233
Member Since:
10/08/2009
Offline
12

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/08/2010
10:47
tony
Sofia, Bulgaria
Moderator
Forum Posts: 7693
Member Since:
30/10/2007
Offline
13

Thanks Oleg. Will look at this as soon as I have time.

Best Regsrds

Tony

For professional UI suites for PHP, ASP.NET and ASP.NET MVC visit us at our commercial products site - trirand.net - by the very same guys that created jqGrid.
13/11/2012
23:20
cbonick1
New Member
Forum Posts: 1
Member Since:
13/11/2012
Offline

Hi :

Enable search for tableToGrid. I need to do a local search for a specific record. These records in the memory I have brought from a php template, no desire to go back to look at php.

Grateful if you could help

thanks

Forum Timezone: Europe/Sofia

Most Users Ever Online: 215

Currently Online:
48 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

OlegK: 1233

markw65: 179

kobruleht: 144

phicarre: 132

YamilBracho: 124

Renso: 118

Member Stats:

Guest Posters: 447

Members: 11326

Moderators: 2

Admins: 1

Forum Stats:

Groups: 1

Forums: 8

Topics: 10535

Posts: 31142

Newest Members: Farooq Tahir, s2e_grid, Ron, ranjansudeep12, songcon2, marcosvmauri

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

Administrators: admin: 61

Comments are closed.
Privacy Policy   Terms and Conditions   Contact Information