Help getting row reorder to work when defining fields using tablename

Help getting row reorder to work when defining fields using tablename

PaulVickeryPaulVickery Posts: 11Questions: 3Answers: 0

Hi All

Please can you help me as I am trying to get row reordering to work. I have succesfully replicated the example at https://editor.datatables.net/examples/extensions/rowReorder but when I change the field names to "table.fieldname" which I need to do as I am joining this table with another, then I get an error that one of the key values in the $value array is not present. I am not that clued up as to find out what the actual key values being submitted by editor are.

Javascript file below:

var editor; // use a global for the submit and return data rendering in the examples

$(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
        ajax:  '../php/table.test.php',
        table: '#test',
        fields: [ {
                label: 'Order:',
                name: 'audiobooks.readingOrder',
                fieldInfo: 'This field can only be edited via click and drag row reordering.'
            }, {
                label: 'Title:',
                name:  'audiobooks.title'
            }, {
                label: 'Author:',
                name:  'audiobooks.author'
            }, {
                label: 'Duration (seconds):',
                name:  'audiobooks.duration'
            }
        ]
    } );

    var table = $('#test').DataTable( {
        dom: 'Bfrtip',
        ajax: '../php/table.test.php',
        columns: [
            { data: 'audiobooks.readingOrder', className: 'reorder' },
            { data: 'audiobooks.title' },
            { data: 'audiobooks.author' },
            { data: 'audiobooks.duration', render: function ( data, type, row ) {
                return parseInt(data/60, 10)+'h '+(data%60)+'m';
            } }
        ],
        columnDefs: [
            { orderable: false, targets: [ 1,2,3 ] }
        ],
        rowReorder: {
            dataSrc: 'audiobooks.readingOrder',
            editor:  editor
        },
        select: true,
        buttons: [
            { extend: 'create', editor: editor },
            { extend: 'edit',   editor: editor },
            { extend: 'remove', editor: editor }
        ]
    } );

    editor
        .on( 'postCreate postRemove', function () {
            // After create or edit, a number of other rows might have been effected -
            // so we need to reload the table, keeping the paging in the current position
            table.ajax.reload( null, false );
        } )
        .on( 'initCreate', function () {
            // Enable order for create
            editor.field( 'audiobooks.readingOrder' ).enable();
        } )
        .on( 'initEdit', function () {
            // Disable for edit (re-ordering is performed by click and drag)
            editor.field( 'audiobooks.readingOrder' ).disable();
        } );
  } );

server side script below:

        <?php

        // DataTables PHP library
        include( "lib/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;

        // Build our Editor instance and process the data coming from _POST
        Editor::inst( $db, 'audiobooks' )
            ->fields(
                Field::inst( 'audiobooks.title' )->validator( 'Validate::notEmpty' ), // project
                Field::inst( 'audiobooks.author' )->validator( 'Validate::notEmpty' ), //users
                Field::inst( 'audiobooks.duration' )->validator( 'Validate::notEmpty' ), // active
                Field::inst( 'audiobooks.readingOrder' )->validator( 'Validate::numeric' ) // project_order
            )

            ->on( 'preCreate', function ( $editor, $values ) {
                if (! $values['audiobooks.readingOrder']) {
                    // If no value submitted, then use the max+1 as the new value
                    $next = $editor->db()->sql('select IFNULL(MAX(audiobooks.readingOrder)+1, 1) as next FROM audiobooks')->fetch();
                    $editor->field('audiobooks.readingOrder')->setValue($next['next']);
                }
                else {
                    // On create update all the other records to make room for our new one
                    $editor->db()
                        ->query( 'update', 'audiobooks' )
                        ->set( 'audiobooks.readingOrder', 'audiobooks.readingOrder+1', false )
                        ->where( 'audiobooks.readingOrder', $values['audiobooks.readingOrder'], '>=' )
                        ->exec();
                }
            } )
            ->on( 'preRemove', function ( $editor, $id, $values ) {
                // On remove, the sequence needs to be updated to decrement all rows
                // beyond the deleted row. Get the current reading order by id (don't
                // use the submitted value in case of a multi-row delete).
                $order = $editor->db()
                    ->select( 'audiobooks', 'audiobooks.readingOrder', array('id' => $id) )
                    ->fetch();

                $editor->db()
                    ->query( 'update', 'audiobooks' )
                    ->set( 'audiobooks.readingOrder', 'audiobooks.readingOrder-1', false )
                    ->where( 'audiobooks.readingOrder', $order['audiobooks.readingOrder'], '>' )
                    ->exec();
            } )

            ->process( $_POST )
            ->json();

Failing with:

Warning: Undefined array key "audiobooks.readingOrder" in C:\xampp\htdocs\php\table.test.php on line 25

which I believe fails here:

 ->on( 'preCreate', function ( $editor, $values ) {
    if (! $values['audiobooks.readingOrder']) {
        // If no value submitted, then use the max+1 as the new value

Hopefully a simple one, and thank you in advance.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Answer ✓

    Change:

    if (! $values['audiobooks.readingOrder']) {
    

    to:

    if (! $values['audiobooks']['readingOrder']) {
    

    it is a nested array - not a string at that point.

    Allan

  • PaulVickeryPaulVickery Posts: 11Questions: 3Answers: 0

    Alan, thank you very much indeed, all sorted.

Sign In or Register to comment.