Row added not shown

Row added not shown

jgcaudetjgcaudet Posts: 82Questions: 7Answers: 0

Hi Allan
Versions : Editor v1.4.2 + DataTables 1.10.7
I add a row and it isn,t shown till I press F5
I have serverSide parameter to false. If I change serverSide to true it works but I would keep serverSide = false.
Before I used DataTables 1.9+ and Editor 1.2.3 and it worked.

Replies

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Hi,

    Are you using the Editor PHP libraries? If so, could you use your browser's developer console to check what is being returned fraom the Ajax call that Editor makes when creating the new row. It should be JSON data that describes the newly created row.

    Allan

  • jgcaudetjgcaudet Posts: 82Questions: 7Answers: 0

    Yes I use Editor php libraries
    create.php returns {"row":null}
    This is my create.php :

    include( "partes.php" );
    $editor->process($_POST)
    ->json();

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Okay, if it is returning row: null then it means that when the libraries try to read the newly inserted row back from the database, they can't.

    Most commonly that is because of a where condition that prevents it from being read. Might that be the case here?

    Allan

  • jgcaudetjgcaudet Posts: 82Questions: 7Answers: 0

    No.

  • jgcaudetjgcaudet Posts: 82Questions: 7Answers: 0
    edited July 2015
    $editor = Editor::inst( $db, 'clientes', 'cliid')
        ->fields(
            Field::inst( 'cliid' )->set(false),
            Field::inst( 'clinom' )->validator( 'Validate::required' ),
            Field::inst( 'clinomc' )->validator( 'Validate::required' ),
            Field::inst( 'clitlf' ),
            Field::inst( 'cliemail' )
        )
    ;
    
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    I presume that is the partes.php file and it then immediately goes on to process the input data and return the JSON to the client side?

    What database are you using?

    I assume that cliid is not a parameter that is submitted as part of the Editor form? That would seem to be the one stand out point for me.

    There is something that is stopping Editor from being able to read the row back after it has been written to the database - that is what the row:null means. We need to figure out what is causing that.

    Allan

  • jgcaudetjgcaudet Posts: 82Questions: 7Answers: 0
    edited July 2015

    I,m using MYSQL
    I gonna give you a more simple example in my application that doesn,t work.

    File create.php :

    include( "tipos.php" );
    $editor->process($_POST)->json();
    

    File tipos.php

    include( dirname(__FILE__)."/../DataTables.php" );
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Join,
        DataTables\Editor\Validate;
    
    $editor = Editor::inst( $db, 'tipos')
        ->fields(
            Field::inst( 'id' )->set(false),
            Field::inst( 'tipo' )->validator( 'Validate::required' )
        )
    ;
    

    Call :

    editor = new $.fn.dataTable.Editor( {
        "ajax": {
        create: {type: 'POST', url: "/php/tipos/create.php" },
        edit:   {type: 'PUT', url: "/php/tipos/edit.php?id=_id_" },
        remove: {type: 'DELETE', url: "/php/tipos/remove.php?id=_id_" }
        },
    
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Does it work if you remove Field::inst( 'id' )->set(false)? Are you showing the primary key in your table?

    Allan

  • jgcaudetjgcaudet Posts: 82Questions: 7Answers: 0

    Yes I,m showing. It,s an autoincrement field.
    If I remove it works.
    But I want to show it in my datatables and my editor. Before I update to the last version of datatables it worked.Is there any way to solution this?

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    But I want to show it in my [...] my editor

    This is a key point that I didn't realise before. So you have the primary key in your Editor editing form? Is it readonly?

    There are a few options available:

    1. I suspect if you removed the ->set( false ) it might actually work as expected.
    2. Remove the primary key from the Editor form
    3. Use preSubmit to remove the data.id parameter from the submitted data (delete json.data['id'];).

    The issue is being caused by the fact that newer versions of Editor allow the primary key to be edited (personally I think that is almost always a very bad idea, but there were a few use cases whereby it was required for others, which is why that is included now).

    The test Editor uses is to check if the primary key is in the submitted data - this is possibly a little flawed. It should perhaps check if the field is read only as well. I will look at adding that check for 1.5.

    Regards,
    Allan

  • jgcaudetjgcaudet Posts: 82Questions: 7Answers: 0

    Only works to me with option 2.
    But I will thank you if you solve it, in order that I can see the primary key in Editor (readonly, disabled, ....)

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Hi,

    I've just committed the change required to have this work as expected, which will be available in Editor 1.5. In the meantime, if you want to apply the change required to the 1.4.2 libraries there are two places where a change is required:

    1) In the _create method - search for the comment "// Was the primary key sent and set? Unusual, but it is possible" and replace the block of code immediately following it with:

            $pkeyField = $this->_find_field( $this->_pkey, 'name' );
            if ( $pkeyField && $pkeyField->apply( 'edit', $values ) ) {
                $id = $values[ $this->_pkey ];
            }
    

    2) In the _update method search for the comment "// Was the primary key altered as part of the edit? Unusual, but it is" and replace with vlock of code immediately following it with:

            $pkeyField = $this->_find_field( $this->_pkey, 'name' );
            $getId = $pkeyField && $pkeyField->apply( 'edit', $values ) ?
                $values[ $this->_pkey ] :
                $id;
    

    That will ensure that Editor doesn't get itself confused if the primary key value is submitted, but not used.

    Thanks for letting me know about this one!

    Allan

  • jgcaudetjgcaudet Posts: 82Questions: 7Answers: 0

    Hi
    I modify that and I get an error " Undefined variable: values on line ..." in this two lines:
    "if ( $pkeyField && $pkeyField->apply( 'edit', $values ) ) { ..."
    and " $getId = $pkeyField && $pkeyField->apply( 'edit', $values ) ? ... "

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Oh bother - yes, sorry. Replace $values with $this->_formData in both cases and that should do it.

    Allan

This discussion has been closed.