Help with sorting groups

QuestionsCategory: How TosHelp with sorting groups
Steve Borruso asked 5 years ago

Hello Abu,

I have a Subgrid that utilizes grouping based on a column named – “med_names” and also sorts rows within each group based on another column named – “times” (the subgrid gets data loaded from clicking a row in the Master grid ).

This has all worked fine … but now I’d like to control the sort order of these “groups”, with respect to each other, based on the value of another column – “prn” (Y|N),  preferably without having to somehow do it myself in JS.

I’ve messed around with sortname/sortorder but this seems to have no effect –

$grid[“sortname”] = “prn asc,times”;
$grid[“sortorder”] = “asc”;

That always results in the following parms being sent to the server –

  1. sidx:  med_name asc, prn asc,times
  2. sord:  asc

Is there anyway to control the sort order of the groups themselves based on another column value ?

Thanks,

Steve

 

 

 

5 Answers
Abu Ghufran Staff answered 5 years ago

Grouped column will always get first sort order. In working demo (link), i’ve used grouping with Ship_country but sorted with Ship_via field first. For that i have made a new column ship_country_2:

$g->select_command = “select *,concat(ship_via,’/’,ship_country) as ship_country_2 from orders”;

And make group field based on ship_country_2:

$grid[“groupingView”][“groupField”] = array(“ship_country_2“);

It will give desired result, but the display will also have like 1/Argentina, 1/Belgium. So we can remove it by:

$grid[“groupingView”][“formatDisplayField”] = array(
“function (displayValue, cm, index, grp) { displayValue = displayValue.substring(displayValue.indexOf(‘/’)+1); return displayValue[0].toUpperCase() + displayValue.substring(1).toLowerCase(); }”
);

Complete working demo here:

https://gist.github.com/gridphp/635e61165b066d6d7294021f39fc8ca2

_________________________
Abu Ghufran - Dev Team
Grid 4 PHP Framework
 
Steve Borruso answered 5 years ago

Thanks Abu,

I implemented  what you suggested above in regard to sorting the group names and that solved the display issue of sorting the groups when the grid is loaded.

Here’s what I specified – $g3->select_command = SELECT *,concat(prn,’/’,med_name) as sort_prn_meds FROM medications WHERE first ….

The issue I’m seeing with this solution is if I now do an inline edit on one of the rows I get a SQL error stating that the column with the name – sort_prn_meds doesn’t exist (which it doesn’t).

The Save request is sending sort_prn_meds as a parameter to the server when I save the row edit.

Any ideas ?

Thanks,

Steve

Steve Borruso answered 5 years ago

Still not working …

I tried adding this in hopes of catching the edit operation on the backend to remove the “sort_prn_meds ” parm from the POST data …

$med_e[“on_update”] = array(“strip_concat_from_SQL”, null, true);

$g3->set_events($med_e);

function strip_concat_from_SQL($mydata) {
if ($_POST[“oper”] == “edit”) {
unset($_POST[‘sort_prn_meds’]);
}

}

But I still get this SQL error –

Couldn’t execute query. Unknown column ‘sort_prn_meds’ in ‘field list’ – UPDATE medications SET ….

Not sure how I can alter the actual SQL command, to remove this non existant column name, before the UPDATE is executed

 

Abu answered 5 years ago

Try this inside function:

unset($mydata[“params”][‘sort_prn_meds’]);

You can also do print_r / die on $mydata to see what is being used to make update query.

 

Steve Borruso answered 5 years ago

That did the trick. The SQL concat “column name” now get’s stripped on the backend from the parms used to build the SQL UPDATE command.

Thanks for all the help with this !!

Your Answer

4 + 11 =

Login with your Social Id:

OR, enter

Attach code here and paste link in question.
Attach screenshot here and paste link in question.



How useful was this discussion?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate it.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?