How to use Editor with ServerSide=true when using combined columns (render) on the display side?

How to use Editor with ServerSide=true when using combined columns (render) on the display side?

fitzfallstarfitzfallstar Posts: 5Questions: 2Answers: 0
edited February 2016 in Free community support

I've posted this question to stackoverflow as well, but I was hoping the community could help me out. I'm looking to purchase Editor but I don't want to get the License if I can't figure out how to make it work the way I want it to.

I'm trying to use the basic initialisation example of:

{ data: null, render: function ( data, type, row ) {
                // Combine the first and last names into a single table field
                return data.first_name+' '+data.last_name;
            }

Here is my Server Code

// DataTables PHP library
include( "./js/Editor-PHP/php/DataTables.php" );

// Alias Editor classes so they are easy to use
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Join,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate;

// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'datatables_demo' )
    ->fields(
        Field::inst( 'first_name' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'last_name' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'position' ),
        Field::inst( 'email' ),
        Field::inst( 'office' ),
        Field::inst( 'extn' ),
        Field::inst( 'age' )
            ->validator( 'Validate::numeric' )
            ->setFormatter( 'Format::ifEmpty', null ),
        Field::inst( 'salary' )
            ->validator( 'Validate::numeric' )
            ->setFormatter( 'Format::ifEmpty', null ),
        Field::inst( 'start_date' )
            ->validator( 'Validate::dateFormat', array(
                "format"  => Format::DATE_ISO_8601,
                "message" => "Please enter a date in the format yyyy-mm-dd"
            ) )
            ->getFormatter( 'Format::date_sql_to_format', Format::DATE_ISO_8601 )
            ->setFormatter( 'Format::date_format_to_sql', Format::DATE_ISO_8601 )
    )
    ->process( $_POST )
    ->json();

with serverSide set to true. When I do this sorting by the Name column, and searching at all, breaks the table giving the following error. "DataTables warning: table id=example - Unknown field: (index 0)"

Am I missing something in my code? Here is a link to my current example code. If you change the sort to anything other than Name it shows the data and sorts fine.

This question has an accepted answers - jump to answer

Answers

  • glendersonglenderson Posts: 231Questions: 11Answers: 29

    In your columns setup, instead of data:null, change it to data:"Name". I think the table cannot sort on a Null definition.

  • fitzfallstarfitzfallstar Posts: 5Questions: 2Answers: 0

    Hey, I just tried that, with no luck. I also tried adding "first_name" to see what that would do, and it made the Name column "undefined undefined" for both of them.

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin

    Hi,

    Add:

    searchable: false,
    orderable: false
    

    to the first column's definition.

    You will also need to use order to set a default ordering column that is not the first one.

    Those two changes should make it work okay.

    Allan

  • fitzfallstarfitzfallstar Posts: 5Questions: 2Answers: 0

    Ok Allan, I've added those two lines. I even added two hidden columns, a first_name and last_name so that I can search by those columns still. Is there a way to use sorting on that name column? or is that not possible with the current build? Thanks for the help, this has gotten me a lot further than anything I had tried before!

  • allanallan Posts: 63,761Questions: 1Answers: 10,510 Site admin
    Answer ✓

    Because the rendering happens on the client-side, and you have server-side processing enabled (i.e. the sorting is done on the server-side) there is simply no way to do a sort on that client-side data.

    You could use data: 'last_name' which would sort on the last name, but not the first, which might be better than nothing (it might also just ask to questions about the sorting being wrong though!).

    The only way to get client-side rendered data to sort correctly is to sort it on the client-side.

    Allan

This discussion has been closed.