pepebe
2/6/2012 - 4:47 PM

MIGX - Add sortby and sortdir to MIGX output.

MIGX - Add sortby and sortdir to MIGX output.

Just a tiny hack to enable sorting a MIGx by ANY ot it's columns.

Add the following lines below /* get default properties */ near the top:

$sortBy = $modx->getOption('sortBy', $scriptProperties, '');
$sortOrder = $modx->getOption('sortOrder', $scriptProperties, 'asc');


Just before $idx = 0; (arround line #122) add this bit of code:

// v1.1
            if( array_search( $sortBy , array_Keys($items) ) !== FALSE ) {          
                foreach($items as $k=>$v) {
                    $b[$k] = $v[$sortBy];
                }
                    switch( $sortDir ) {
                    case'asc':
                        asort( $b );
                        break;
                    case'desc':
                        arsort( $b );
                        break;
                    default:
                        asort( $b );
                        break;
                    }
 
                foreach($b as $key=>$val) {
                    $c[$key] = $items[$key];
                }               
                $items = $c;
            }

Edit: Please note that v1.0 was a cut'n'paste accident. This one works.


Now you can call getImageList with 2 new parameters:

&sortDir=`asc_or_desc`
&sortBy=`add_dataIndex_here`

Regards,

pepebe

P.S.: You may Please PM me if you have a trouble with this. I'm using this on a page that will be going live in a few days and it would be great to solve all issues that might show up...