How to transfer a field to another field

How to transfer a field to another field

lancwplancwp Posts: 90Questions: 20Answers: 1
edited July 2021 in Editor

When I use DataTables editor to receive data in the PHP backend,The value of GQ field is obtained by the difference between xdtime and wctime, not from the input value, My method is like this:

->fields(
            Field::inst( 'xdtime' ),
       Field::inst( 'wctime' ),                 
       Field::inst( 'gq' )
           ->setValue(round((strtotime(Field::inst( 'wctime' ))-strtotime(Field::inst( 'xdtime' )))/3600/24)),

but it's wrong,How is the right way to get it?

Greetings

This question has accepted answers - jump to:

Answers

  • lancwplancwp Posts: 90Questions: 20Answers: 1

    I want the value of the GQ field to come from the difference between the other two date fields

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    You need to use event handlers for this. For example you might use preCreate and preEdit to set the value based on the data that is passed into the event handlers for each row. That allows it to handle multi-row editing as well.

    Allan

  • lancwplancwp Posts: 90Questions: 20Answers: 1
    edited July 2021

    Thank you for your reply,But I have a key questions
    example:

    Editor::inst( $db, 'staff' )
        ->fields(
            Field::inst( 'first_name' ),
            Field::inst( 'last_name' ),
            Field::inst( 'position' ),
            Field::inst( 'email' ),
            Field::inst( 'office' ),
            Field::inst( 'created_by' )->set( Field::SET_CREATE ),
            Field::inst( 'last_updated_by' )->set( Field::SET_EDIT )
        )
        ->on( 'preCreate', function ( $editor, &$values ) {
            $editor
                ->field( 'created_by' )
                ->setValue( $_SESSION['username'] );
        } )
        ->on( 'preEdit', function ( $editor, $id, &$values ) {
            $editor
             **   ->field( 'last_updated_by' )
                ->setValue( time() );**
        } )
        ->process( $_POST )
        ->json();
    
    In this example ,
    
           ->field( 'created_by' )
                ->setValue( $_SESSION['username'] );
    

    In this example, the field “created_ By ” set to $_ Session ['username '] value, but if I want “ create_ By” is set to another field in the same table, such as "first_name",How do I rewrite this code?

    Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Is the data you want it to be sit to being submitted from the client-side? If so use $values['email'] for example.

    Allan

  • lancwplancwp Posts: 90Questions: 20Answers: 1
    edited July 2021
    ->on( 'preCreate', function ( $editor, &$values ) {
            $editor
                ->field( 'gq' )
                ->setValue((round(strtotime($values['wctime'])-strtotime($values['xdtime']))/3600/24));
        } )
         ->on( 'preEdit', function ( $editor, $id, &$values ) {
            $editor
               ->field('gq')
                ->setValue((round(strtotime($values['wctime'])-strtotime($values['xdtime']))/3600/24));
        } )
    

    Great. Thinks,Most of the problems have been solved. Editing and adding are OK. There are still problems in in-line editing. There is no problem in pop-up editing alone. However, when editing directly Inline, the post information cannot be received and an error," a system error has occurred will be reported. How can I modify it so that it can also be edited in-line

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    What is the response from the server when that happens? You can check using the network tab in your browser's inspector. The "A system error..." message means that the server responded with invalid JSON, so we need to know what it is actually responding with. Hopefully and error message that can be addressed.

    Allan

  • lancwplancwp Posts: 90Questions: 20Answers: 1

    A system error has occurred (more information). Yes, the error message I get in the browser now is an error message on line 120. The code on line 120 is:

    ->setValue((round(strtotime($values['wctime'])-strtotime($values['xdtime']))/3600/24));**

    Detailed errors are as follows:

    b>Notice</b>:  Undefined index: xdtime in <b>D:\web\admin\controllers\staff.php</b> on line <b>120</b><br />
    {"data":[{"DT_RowId":"row_5225","id":"5225","ydh":"yd20210704","hppic":"28","statu":"\u8fdb\u884c\u4e2d","hh":"hh20200704","customer":"B\u5ba2\u6237 ","brand":"FILA ","business":"A\u7ec4","nature":"XZ","size":"35","color":"5","pairs":"52","sumpairs":"152","xdtime":"2021-07-15","wctime":"2021-07-07","gq":"18814.666666667","xqk":"\u65b0\u7684","zcl":null,"zcql":null,"zc":null,"dd":"dd232","cx":"cx1","dzt":"dzt1","cjl":null,"cjql":null,"gp":null,"jl":null,"bs":null,"zb":null,"jytime":null,"bll":null,"bql":null,"fltime":null,"removed_date":null}],"debug":[{"query":"SELECT  `id` as 'id' FROM  `ocjd` WHERE `id` = :where_0 ","bindings":[{"name":":where_0","value":"5225","type":null}]},{"query":"UPDATE  `ocjd` SET  `wctime` = :wctime, `gq` = :gq, `removed_date` = :removed_date WHERE `id` = :where_0 ","bindings":[{"name":":wctime","value":"2021-07-07","type":null},{"name":":gq","value":"18814.666666667","type":null},{"name":":removed_date","value":null,"type":null},{"name":":where_0","value":"5225","type":null}]},{"query":"SELECT  `id` as 'id', `ydh` as 'ydh', `hppic` as 'hppic', `statu` as 'statu', `hh` as 'hh', `customer` as 'customer', `brand` as 'brand', `business` as 'business', `nature` as 'nature', `size` as 'size', `color` as 'color', `pairs` as 'pairs', `sumpairs` as 'sumpairs', `xdtime` as 'xdtime', `wctime` as 'wctime', `gq` as 'gq', `xqk` as 'xqk', `zcl` as 'zcl', `zcql` as 'zcql', `zc` as 'zc', `dd` as 'dd', `cx` as 'cx', `dzt` as 'dzt', `cjl` as 'cjl', `cjql` as 'cjql', `gp` as 'gp', `jl` as 'jl', `bs` as 'bs', `zb` as 'zb', `jytime` as 'jytime', `bll` as 'bll', `bql` as 'bql', `fltime` as 'fltime', `removed_date` as 'removed_date' FROM  `ocjd` WHERE `removed_date` IS NULL AND  `id` = :where_1 ","bindings":[{"name":":where_1","value":"5225","type":null}]},{"query":"SELECT  `id` as 'id', `filename` as 'filename', `filesize` as 'filesize', `web_path` as 'web_path', `system_path` as 'system_path' FROM  `files` WHERE `id` IN (:wherein1) ","bindings":[{"name":":wherein1","value":"28","type":null}]}]}
    
  • lancwplancwp Posts: 90Questions: 20Answers: 1

    Allan Thanks , I using inline editing

  • lancwplancwp Posts: 90Questions: 20Answers: 1
    Answer ✓
    $('#example').on( 'click', 'tbody td:not(:first-child)', function (e) {
            editor.inline( this, {
                onBlur: 'submit',
            submit: 'allIfChanged'
            } );
        } );
    

    I found it. Submit it with all the data. It's OK. Thanks

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Nice one - thanks for posting back.

    Allan

  • lancwplancwp Posts: 90Questions: 20Answers: 1

    thanks

Sign In or Register to comment.