Recover field value in Editor

Recover field value in Editor

Tony STony S Posts: 26Questions: 4Answers: 0

Hello
How can I recover the value of NFAC in editor ?

fields: [
            {label: 'ID:', name: "invoice.id", type: "hidden"},
            {label: 'NFAC:', name: "invoice.nbi", type: "hidden", def: function (data) {return invoice.id + "L"; } }
        ]

This question has accepted answers - jump to:

Answers

  • rf1234rf1234 Posts: 2,946Questions: 87Answers: 416

    https://editor.datatables.net/reference/api/field().label()
    in case you mean the value of the label "NFAC"

    https://editor.datatables.net/reference/api/field().val()
    in case you mean the value of the field

    https://editor.datatables.net/reference/api/field().name()
    in case you mean the name of the field

    ... and many more ... just use the search field in the upper right corner.

  • Tony STony S Posts: 26Questions: 4Answers: 0

    In fact at the time of creation I would like to recover the invoice.id value to inject it
    into the second field ( invoice.nbi) but I can't do it !

    How can i get the invoice.id value ?

  • rf1234rf1234 Posts: 2,946Questions: 87Answers: 416
    edited March 2020

    Did you check the Editor event list?
    https://editor.datatables.net/manual/events

    editor
        .on ( 'initSubmit', function ( e, action ) {     
            if ( action === "create" ) {
                this.set( { 'invoice.nbi': this.val('invoice.id') } );
            }
        })
    

    Of course you can also use the "preSubmit" event but then you don't have the form fields available any longer which is a clear disadvantage.

  • Tony STony S Posts: 26Questions: 4Answers: 0

    Thank you rf1234, the method is good but does not work with me because I think the id is generated by MySQL (AUTO INCREMENT), I do not know if there is a solution!

  • rf1234rf1234 Posts: 2,946Questions: 87Answers: 416

    But then it is something you need to resolve server side! Nothing you can do @ the front end.

    Sounds like you want to establish a foreign key relationship. I would take a look at the Editor join examples that do not use a link table.

    Otherwise please post a test case as per the forum rules.

  • Tony STony S Posts: 26Questions: 4Answers: 0

    Actually i was thinking of creating an invoice number field and giving it a special shape based on the unique id number.
    example:
    invoice.id = 1 --> invoice.nbi = IN20-0001
    invoice.id = 2 --> invoice.nbi = IN20-0002
    invoice.id = 33 --> invoice.nbi = IN20-0033
    ...

    editor.on ( 'initSubmit', function ( e, action ) {    
            if ( action === "create" ) {
                this.set( { ' invoice.nbi': 'FA' + moment().format('YY')+ '-' + ('000' + this.val(' invoice.id').substr( -4, 4 )) } );
    
  • rf1234rf1234 Posts: 2,946Questions: 87Answers: 416

    Ok, you can only do that server side in your case because you need to get the last insert id from MySQL first. Take a look at the respective server side events for PHP, .NET or whatever you are using. What is your server side language?

  • Tony STony S Posts: 26Questions: 4Answers: 0

    PHP

  • rf1234rf1234 Posts: 2,946Questions: 87Answers: 416
    edited March 2020 Answer ✓

    Question is: Why would you save this if there is a clear rule how to generate this field's value based on the id? So if you would like to show it in your data table as well it would be easiest to retrieve it from the server.

    a) you don't save the field but just retrieve it as an alias of the id field.

    Editor::inst( $db, 'invoice' )
    ->field(
        Field::inst( 'invoice.id' )->set( false ),
        Field::inst( 'invoice.id AS invoice.nbi' )->set( false ) 
            ->getFormatter( function($val, $data, $opts) {
                return 'IN' . date('y') . '-' . substr(sprintf("%04d", $val), -4);
            }),
    

    b) you save the field by updating your table row based on the last insert id (if your nbi field has NULL as the default in MySQL):

    Editor::inst( $db, 'invoice' )
    ->field(
        Field::inst( 'invoice.id' )->set( false ),
        Field::inst( 'invoice.nbi' )->set( false ) 
            ->getFormatter( function($val, $data, $opts) {
                if ( is_null($val) ) {
                   return '';
                }
            }),
    ....................
    ->on( 'postCreate', function ( $editor, $id, $values, $row ) {            
        $editor->db()->raw()
           ->bind( ':nbi',  'IN' . date('y') . '-' . substr(sprintf("%04d", $id), -4)) )
           ->bind( ':id', $id )
           ->exec( 'UPDATE invoice 
                       SET nbi = :nbi 
                     WHERE id = :id' );
        } ),
    

    https://editor.datatables.net/reference/event/postCreate

  • Tony STony S Posts: 26Questions: 4Answers: 0

    rf1234 you for your help and your kindness :) .
    The ALIAS method is interesting, but I give up, and I keep the client side method without registration.

  • rf1234rf1234 Posts: 2,946Questions: 87Answers: 416
    Answer ✓

    ok - I personally like the alias method a lot: Easy and no hassle client side. But that is probably a question of taste. Good luck!

  • Tony STony S Posts: 26Questions: 4Answers: 0

    Thank you very much again, your method works perfectly :)

This discussion has been closed.