Ad

OctoberCMS, How To Sort Order Of The Records Data Set (json Structure) By Option From Previous Page?

- 1 answer

Is anyone know how to sort order of this records data set by value from previous page?

Eg.

1st Page (Previous page): Dropdown select option value : 1,2,3

when

selected 1 : will make records sorting as default,

selected 2 : will make sorting by interest number,

selected 3 : will make sorting by name

2nd Page (This page)

{% for offer in offers %}

re-order as selected option

2nd page's records data set (json structure)

Ad

Answer

first its hard to sort JSON instead what we can do we can convert it to array and then we can sort it,

and i also suggest to move this JSON code in code section of page.

so now in your page code section

// this method will execute at start of page
function onStart() {   

    // you can replace your json data here inside single quotation
    $offers = '[
        { "name" : "a"},
        { "name" : "c"},
        { "name" : "b"}
    ]';    

    // this is orders based on incoming param
    $sortingOrders = [
        // default (no sorting at all)
        1 => 'default', 
        // sort ascending on names
        2 => ['field' => 'name' , 'direction' => 'asc'], 
        // sort ascending on numbers
        3 => ['field' => 'number' , 'direction' => 'asc'],        
        // extra sort descending on names
        4 => ['field' => 'name' , 'direction' => 'desc'], 
    ];    

    // conver json data in-to array
    $arrayOffers = json_decode ($offers, true);    

    // taking paramert of sorting (you can use your own logic here)
    // $order will be 1 2 3 or 4 passed from previous page
    $order = $this->param('order');

    // we do some checks if given order exists
    if( $order && isset($sortingOrders[$order]) && is_array($sortingOrders[$order]) ) {

        // if order is exists we do sorting
        $this->sortBy($sortingOrders[$order]['field'], $arrayOffers, $sortingOrders[$order]['direction']);
    }

    // assign data to page
    $this['arrayOffers'] = $arrayOffers;
}

// helper function will sort the data
function sortBy($field, &$array, $direction = 'asc')
{
    // array is pass by refenence so it will modify it directly

    // sorting logic
    usort($array, function($a, $b) use ($field, $direction) {

        if(!isset($a[$field])) {
            return 0;
        }

        $a = $a[$field];
        $b = $b[$field];
        if ($a == $b) return 0;
        $direction = strtolower(trim($direction));
        if($direction == 'asc') {
            return $a < $b ? -1 : 1;
        }     
        return $a > $b ? -1 : 1;     
    });

    return true;
}

now inside your page Markup section just put this code which will just display data sorted, as arrayOffers is already sorted.

<ul>
    {% for offer in arrayOffers %}
        <li> {{offer.name}}</li>            
    {% endfor %}
</ul>

Now you can use /testing/:order? as page slug

and then you can use this page as

/testing will show default order

/testing/1 will show default order

/testing/2 will show sort asc on names

Ad
source: stackoverflow.com
Ad