sort priority

Talk about Sorting & Paging problems, issues etc.

Moderator: alexandrleonenko

wonoz
Senior
Senior
Posts: 41
Joined: Oct 14th, '10, 13:43

sort priority

Postby wonoz » Dec 20th, '10, 13:28

I got a specific need..
I have to put a record always in the top of the table (at the first line).
I can identify this record by the field "priority".
Only one record have a priority egal to 1, the others are 0.

First, I sorted the data grid by the priority :

Code: Select all

$sql = "SELECT id, name, priority, myField FROM myTable";

$default_order_field = "priority, name";
$default_order_type = "DESC, ASC";

By default, it works.
Now, my problem is if the user want to sort by another field. For example, by "myField". The "ORDER BY priority" is broken and the high priority valued doesn't appear in the first line.

I think there is two ways to resolve this :
- 1) If it's possible, the grid must launch two sql request :
- first "SELECT id, name, priority, myField FROM myTable WHERE priority = 1";
- then "SELECT id, name, priority, myField FROM myTable WHERE priority = 0";
Then make a pile with both results... I don't think that it would be easy, tbh.
- 2) Maybe the best solution : hardcode the fact that the grid must sort by "priority" at first, then by the header selected by user.
How/where can I do this ? Did you have a better idea ?

administrator
Site Admin
Site Admin
Posts: 6004
Joined: Jan 7th, '09, 23:18
Contact:

Re: sort priority

Postby administrator » Dec 21st, '10, 09:15

Whatever you do, if i want to sort by another column you will loose this record from the top.
Possible solutions: turn Off sorting for another columns by "sortable"=>"false"

wonoz
Senior
Senior
Posts: 41
Joined: Oct 14th, '10, 13:43

Re: sort priority

Postby wonoz » Dec 21st, '10, 10:07

I must allow the use of the sorting function :/

There is no way to add the priority constraint in datagrid.class ?
I suppose there is a function who takes the header name who's get clicked by the user, and then who creates the sort statement.
I don't really know how/where, but it could look like this :

Code: Select all

$this->sqlSort = " ORDER BY priority desc, ".$sort_field_name." ".$req_sort_type;

With something like this, the sort will be done first with the priority, then with the field selected by the user

thank for caring ;)

[joke]If it's no possible, no problem, I'll just get fired before xmas :lol:[/joke]

wonoz
Senior
Senior
Posts: 41
Joined: Oct 14th, '10, 13:43

Re: sort priority

Postby wonoz » Dec 21st, '10, 14:07

Ok, I tried some things in datagrid.class, at "function DataSource".
In "create ORDER BY part of sql statment", I edited the line
"else{ $this->sqlSort = " ORDER BY priority DESC, ".$sort_field_name." ".$req_sort_type; }" :

Code: Select all

if($add_order_by){
                if($req_sort_field_type == "numeric"){
                    $this->sqlSort = " ORDER BY ABS(".$sort_field.") ".$req_sort_type;     
                    $numeric_sort = true;
                }else{
                    $sort_field_name = $sort_field;
                    // [#0014-2] fixed bug 08.04.2010
                    if($this->dbHandler->phptype == "mssql"){
                        $sql_parts = explode(" ", $this->sql);
                        if(isset($sql_parts[$sort_field-1])){
                            $sort_field_name = str_replace(",", "", $sql_parts[$sort_field-1]);
                            $this->sqlSort = " ORDER BY ".$sort_field_name." ".$req_sort_type;
                        }else{
                            $this->sqlSort = " ".$this->GetOrderByList();
                        }
                    }else{
                        $this->sqlSort = " ORDER BY priority DESC, ".$sort_field_name." ".$req_sort_type;   
                    }
                }               
            }else{
                $this->sqlSort = ", ".$sort_field." ".$req_sort_type;
            }           

It seems to work for now.. But I don't know if I must edit the other lines who contain the key words "order
by"

administrator
Site Admin
Site Admin
Posts: 6004
Joined: Jan 7th, '09, 23:18
Contact:

Re: sort priority

Postby administrator » Dec 22nd, '10, 10:12

it's a good solution


Return to “Settings: Sorting & Paging”