Using Editor to Order by

Using Editor to Order by

Loren MaxwellLoren Maxwell Posts: 387Questions: 94Answers: 10

There must be an example of this but I can't seem to find it.

I want to return a record set already ordered through SQL and disable the ordering on a table.

I know how to disable ordering on a table, but how do I add something like ->order('field_name') to the Editor?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,731Questions: 1Answers: 10,110 Site admin
    Answer ✓

    Actually no - the Editor libraries assume that either client-side or server-side processing will be used to do the sorting of the data, so it doesn't provide a way to specify an SQL order by for the table as a whole.

    One option to get around that would be to use a VIEW where you can specify an order and then just let the Editor libraries SELECT from that view.

    Allan

  • Loren MaxwellLoren Maxwell Posts: 387Questions: 94Answers: 10

    Just wanted to follow up. I used the query object to get the sort that I wanted.

    Only drawback is that the table doesn't reorder when I edit or add a new entry, which is a relatively minor issue for me. When the page refreshes it's correct.

    Anyway, here's my code if anyone else is looking to do the same:

    <?php
    
    // DataTables PHP library
    include "../../Editor/php/DataTables.php";
    
    // Alias Editor classes so they are easy to use
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate;
    
    // Log function
    include "_logchange.php";
    
    //Trim input function
    include "_triminput.php";
    
    
    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'sa_region_champions' )
        ->fields(
    
            Field::inst( 'season_name' )
                ->setFormatter( function ( $val, $data, $opts ) { return trimInput( $val ); } )
                ->validator( 'Validate::notEmpty' ),
            Field::inst( 'organization' )
                ->setFormatter( function ( $val, $data, $opts ) { return trimInput( $val ); } ),
            Field::inst( 'class' )
                ->setFormatter( function ( $val, $data, $opts ) { return trimInput( $val ); } )
                ->validator( 'Validate::notEmpty' ),
            Field::inst( 'region' )
                ->setFormatter( function ( $val, $data, $opts ) { return trimInput( $val ); } )
                ->validator( 'Validate::notEmpty' ),
            Field::inst( 'team_name' )
                ->setFormatter( function ( $val, $data, $opts ) { return trimInput( $val ); } )
                ->validator( 'Validate::notEmpty' )
        )
        ->where( function ( $q ) {
           $q
            ->where( 'season_name', $_POST['season'] )
            ->where( 'organization', $_POST['organization'] )
            ->where( 'class', $_POST['class'] )
            ->order( 'region' );
              
        } )
    
        // Log functions
        ->on( 'postCreate', function ( $editor, $id, $values, $row ) {
            logChange( $editor->db(), $editor->table()[0], 'create', $id, $values );
        } ) 
        ->on( 'postEdit', function ( $editor, $id, $values, $row ) {
            logChange( $editor->db(), $editor->table()[0], 'edit', $id, $values );
        } )
        ->on( 'postRemove', function ( $editor, $id, $values ) {
            logChange( $editor->db(), $editor->table()[0], 'delete', $id, $values );
        } )
    
        ->process( $_POST )
        ->json();
    
This discussion has been closed.