If checkbox in the editor field became selected, change the content of the editor field and store it

If checkbox in the editor field became selected, change the content of the editor field and store it

bitmacherbitmacher Posts: 15Questions: 6Answers: 0

Hello everybody

I've been sitting on a more complex topic for days.

I have a lot of input fields with prices in an editor form (e.g. db.reg_price and db.red_price). You can enter this by hand. Underneath there is the possibility to activate a checkbox. If this has been activated, the array should be taken from another table with these prices (reg_price and red_price without db. ).

Up to this point, this also works very well. Even the data in the table contain the values (reg_price and red_price) after saving the form. BUT! These values are not updated in the database in the original variables (db_reg_price and db_red_price). Only when you go into the editor field again and save again, does it take the values into the database.

  1. Is there a possibility to update both variables with this new values from the second table?

I made some tests with ->on('preEdit', function and also with postEdit and so on in the staff.php. Also with editor.on( 'preSubmit', function in the Javascript. But I don't find the right solution.

  1. Is there a possibility to block the fields in editor form if the checkbox were activated?

I'll be at one's wit's end....

Thanks a lot.
Mac

Javascript:

var editor; 

$(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
fields: [
   {
                type: "select",
                label: "Select price package:",
                name: "event.db_price_select_form",
                placeholder: "Please select"
            }, {
                label: "Regular price:",
                name: "event.db_reg_price",
                fieldInfo: "Format: 16.00",
                    
                }, {
                label: "Reduced price:",
                name: "event.db_red_price",
                fieldInfo: "Format: 16.00",
            },
.....

var table = $('#example').DataTable( {

 columns: [
            
            { data: "event.db_price_select",
                render: function (data, type, row) {
                    if (row.event.db_price_select == 1) {
                        return 'Yes';
                    } else {
                        return 'No';
                    }
                }
            },
            { data: "event.name_price_pack" },

            { data: "event.db_reg_price",
            render: function (data, type, row) {
                    if (row.event.db_price_select == 1) {
                        row.event.db_reg_price = row.price.reg_price;
                        return row.price.reg_price;
                    } else {
                        return row.event.db_reg_price;
                    }
                }
            },
......

staff.php:

......
        Field::inst( 'event.db_price_select_form' )
            ->options( Options::inst()
                ->table( 'price' )
                ->value( 'id' )
                ->label( array ( 'name_price_pack', 'reg_price', 'red_price' ))
            )
            ->validator( Validate::dbValues() ),
            Field::inst( 'price.name_price_pack' ),
            Field::inst( 'price.reg_price' ),
            Field::inst( 'price.red_price' ),

        Field::inst( 'event.db_reg_price' ),
        Field::inst( 'event.db_red_price' ),

->leftJoin( 'price', 'price.id', '=', 'event.db_price_select_form' )

.....

Answers

  • allanallan Posts: 63,208Questions: 1Answers: 10,415 Site admin

    Hi,

    If I understand correctly - you want to save data into the leftJoined row as well. Is that correct?

    If so, you need to include price.id in the Field list and then include it in the client-side Editor fields as a hidden field. That way Editor can know which row in the linked table it should edit.

    Allan

  • bitmacherbitmacher Posts: 15Questions: 6Answers: 0

    Hi Allan,

    I don't know if I explained it correctly and think it's the other way round.
    I have 2 tables with variables:

    table event - id | db_price_select | db_price_select_form | db_reg_price | db_red_price
    table price - id | name_price_pack | reg_price | red_price

    In Editor form there are 2 fields "db_reg_price" and "db_red_price" from table "event". You can fill out the fields, save form and everything is okay.

    Second step: There is a checkbox ("db_price_select") in the Editor form and you can activate this box and have the possibility of a select field ("db_price_select_form") for several price bundles from table price.

    If the checkbox is activated the prices from the selected bundle ("red_price" and "reg_price") have to override the original field "db_reg_price" and "db_red_price" in the table event.

    If it's not clear enough I can give you an access to the database to have a look on it. It's in german but I can explain you.

    Thanks a lot.

    Best regards
    Mac

  • bitmacherbitmacher Posts: 15Questions: 6Answers: 0

    Hello Allan,

    I have tried several functions in Javascript and PHP. But I think I'm absolutely on the wrong way..... Maybe I'm just thinking too complicated right now. Are there any solution?

    .....
    Field::inst( 'event.db_reg_price' )
            ->setFormatter( function ($val, $data, $field) {
                    $post = $_POST;
                    if ( $post['action'] === 'create' || $post['action'] === 'edit' ) {
                        if ( $post['data']['event.db_price_select'] === '1' ) {
                        $post['data']['event.db_reg_price'] === ['data']['price.reg_price'];
                        }
                    }
                } ),
    .....
    

    Thanks a lot.
    Mac

  • allanallan Posts: 63,208Questions: 1Answers: 10,415 Site admin

    HI Mac,

    Try this:

    ->on( 'preCreate', function ($editor, $values) {
      if ( $values['event']['db_price_select'] === '1' ) {
        $editor->field('event.db_reg_price').setValue( $values['price']['reg_price'] );
      }
    } ),
    

    That uses the preCreate Editor server-side event - you'd want to do something similar for the preEdit event.

    Note that for this to work you would need to have a price.reg_price field (possibly hidden?) in your client-side Editor form, since that is where the value is being read from. The other option would be to query the database and get the value that way.

    Allan

  • bitmacherbitmacher Posts: 15Questions: 6Answers: 0

    Hi Allan,

    thanks for the answer. I will check that and will give my response.

    Mac

This discussion has been closed.