postCreate isn't working

postCreate isn't working

daniloragodanilorago Posts: 19Questions: 4Answers: 0

Dear all,

Once again I'm here. Finally my project was approved and then I will explore a lot this tool after buying.
Now my issue is that my postCreate isn't working, and I dont know why.
It's working the DataTables, the creation, edit and delete. And I dont have any error on screen or on console.
On my postCreate I call a function to insert some information on another table, of notification!
Somebody has some tip?

PHP

<?php
// >>> SESSION (PROJECT ID)
session_start();
$Int__ID__Project = $_SESSION["ID__Project"];
// <<< SESSION (PROJECT ID)

include("DataTables/php/lib/DataTables.php");
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;

function Fc__Notification($db, $action, $id, &$values){
    $db->insert('Notification', array(
        'ID__Login'                 => $_SESSION["ID__Login"],
        'Notification__ID'          => $id,
        'Notification__Table'       => 'ERP',
        'Notification__Action'      => '4',
        'Notification__Date'        => date("Y-m-d"),
        'Notification__Title'       => 'New ERP',
        'Notification__Description' => $values['ERP']['ERP__Name'],
        'Notification__Status'      => '1'
    ));
}

Editor::inst($db, 'ERP', 'ID__ERP')
    ->fields(
        Field::inst('ERP.ID__ERP'),
        Field::inst('ERP.ERP__Name')
            ->validator(Validate::notEmpty(ValidateOptions::inst()
                ->message('Error: Fill the Name field!')
            ))
            ->validator(Validate::minLen(3,ValidateOptions::inst()
                ->message('Error: Name must have more than 3 characters!')
            ))
            ->validator(Validate::maxLen(100,ValidateOptions::inst()
                ->message('Error: Name must have less than 100 characters!')
            ))
        ,
        Field::inst('ERP.ERP__Description')
            ->validator(Validate::maxLen(1000,ValidateOptions::inst()
                ->message('Error: Description must have less than 1000 characters!')
            ))
        ,
        Field::inst('ERP.ERP__Date')->setValue(date("Y-m-d")),
        Field::inst('ERP.ID__Login')->setValue($_SESSION['ID__Login'])
            ->options(
                Options::inst()
                    ->table('Login')
                    ->value('ID__Login')
                    ->label('Login__Name')
            )
            ->validator(Validate::notEmpty(ValidateOptions::inst()
                ->message('Error: Fill the Login field!')
            ))
        ,
        Field::inst('Login.Login__Name')
    )
    ->leftJoin('Login', 'Login.ID__Login', '=', 'ERP.ID__Login')
    ->on('postCreate', function($editor, $id, &$values, &$row){
        Fc__Notification($editor->db(), 'create', $id, $values);
    })
    ->process($_POST)
    ->json();

<?php
>
?>


-------------------------------------------------------------------------------------
HTML

<script>
        addEventListener("DOMContentLoaded", function () {
            var editor = new DataTable.Editor( {
                ajax: 'datatables--erp.php',
                table: '#DT__List',
                formOptions: {
                    main: {
                        focus: null,
                        submit: 'changed'
                    }
                },
                fields: [
                    {
                        "label": "ID__ERP:",
                        "name": "ERP.ID__ERP",
                        "type": "readonly",
                        "attr": {
                            "disabled": true
                        }
                    },
                    {
                        "label": "ERP__Name:",
                        "name": "ERP.ERP__Name",
                        "type": "text",
                        "attr": {
                            "maxLength": 100
                        }
                    },
                    {
                        "label": "ERP__Description:",
                        "name": "ERP.ERP__Description",
                        "type": "textarea",
                        "attr": {
                            "maxLength": 1000
                        }
                    },
                    {
                        "label": "ERP__Date:",
                        "name": "ERP.ERP__Date",
                        "type": "readonly",
                        "attr": {
                            "disabled": true
                        }
                    },
                    {
                        "label": "ID__Login:",
                        "name": "ERP.ID__Login",
                        "type": "readonly",
                        "attr": {
                            "disabled": true
                        }
                    }
                ]
            } );

            var table = new DataTable('#DT__List', {
                ajax: 'datatables--erp.php',
                columns: [
                    {
                        "data": "ERP.ID__ERP"
                    },
                    {
                        "data": "ERP.ERP__Name"
                    },
                    {
                        "data": "ERP.ERP__Description"
                    },
                    {
                        "data": "ERP.ERP__Date"
                    },
                    {
                        "data": "Login.Login__Name",
                        "editField": "ERP.ID__Login"
                    }
                ],
                colReorder: true,
                fixedColumns: {
                    start: 1
                },
                fixedHeader: true,
                layout: {
                    topStart: {
                        buttons: [
                            {
                                extend: 'create',
                                text: '<i class="fa fa-plus"></i> Add',
                                className: 'btn btn-outline-primary btn-wave',
                                editor: editor
                            }, {
                                extend: 'edit',
                                text: '<i class="fa fa-edit"></i> Edit',
                                className: 'btn btn-outline-primary btn-wave',
                                editor: editor
                            }, {
                                extend: 'remove',
                                text: '<i class="fa fa-trash"></i> Delete',
                                className: 'btn btn-outline-primary btn-wave',
                                editor: editor
                            }, {
                                extend: 'colvis',
                                text: '<i class="fa fa-search"></i> Columns',
                                className: 'btn btn-outline-primary btn-wave',
                                dropIcon: false,
                                editor: editor
                            }, {
                                extend: 'collection',
                                text: '<i class="fa fa-file-export"></i> Export',
                                className: 'btn btn-outline-primary btn-wave',
                                buttons: ['copy', 'excel', 'csv', 'pdf', 'print'],
                                dropIcon: false
                            }
                        ]
                    }
                },
                order: [[0, 'desc']],
                pageLength: 20,
                paging: true,
                scrollX: true,
                scrollY: 550,
                select: {
                    attr: {
                        className: 'selected-row'
                    },
                    selector: 'td:first-child'
                }
            });
            // >>> ID
            editor.on('open', function(e, mode, action){
                if((action === 'create')||(action === 'edit')){
                    this.hide(['ERP.ID__ERP']);
                    this.hide(['ERP.ERP__Date']);
                    this.hide(['ERP.ID__Login']);
                } else {
                    this.show(['ERP.ID__ERP']);
                    this.show(['ERP.ERP__Date']);
                    this.show(['ERP.ID__Login']);
                }
            })
            editor.on('preOpen', function(e, mode, action) {
                var Str__Field_Name = e.currentTarget.s.includeFields[0];
                if (Str__Field_Name === 'ERP.ERP__Date') {
                    return false;
                }
                if (Str__Field_Name === 'ERP.ID__Login') {
                    return false;
                }
            })
            // >>> LOAD
            editor.on('submitSuccess', () => {
                $('#DT__List').DataTable().ajax.reload();
            });
            // >>> UPDATE
            table.on('click', 'tbody td:not(:first-child)', function (e) {
                editor.inline(this, {
                    onBlur: 'submit'
                });
            });
            // >>> NOTIFICATION
            $('#DT__List').on('click', 'tbody td', function () {
                editor.inline(this, {
                    submit: 'allIfChanged'
                });
            });
        });
        $.extend( true, DataTable.Buttons.defaults, {
            dom: {
                container: {
                    className: 'btn-group'
                },
                button: {
                    className: 'btn btn-secondary'
                },
                collection: {
                    tag: 'div',
                    className: 'dropdown-menu',
                    closeButton: false,
                    button: {
                        tag: 'a',
                        className: 'dropdown-item',
                        active: 'active text-white',
                        disabled: ''
                    }
                },
                splitWrapper: {
                    tag: 'div',
                    className: '',
                    closeButton: false
                },
                splitDropdown: {
                    tag: 'button',
                    text: '',
                    className: 'btn btn-secondary',
                    closeButton: false,
                    align: 'split-left',
                    splitAlignClass: 'dt-button-split-left'
                },
                splitDropdownButton: {
                    tag: 'button',
                    className: 'btn btn-secondary',
                    closeButton: false
                }
            },
            buttonCreated: function ( config, button ) {
                return config.buttons ?
                    $('<div class="btn-group"/>').append(button) :
                    button;
            }
        } );
        DataTable.Buttons.defaults.dom.button.className = 'btn-group';
        DataTable.ext.buttons.collection.className = 'dropdown-toggle';
        DataTable.ext.buttons.collection.rightAlignClassName = 'dropdown-menu-right';
    </script>

Replies

  • allanallan Posts: 64,142Questions: 1Answers: 10,584 Site admin

    I see no reason why your postCreate handler wouldn't be called there. I'd suggest adding some logging into the function, as it really looks to me that it should be getting called.

    I don't actually see how you are triggering a create action on the client-side. I see the edit action through an inline edit (you should listen for postEdit for edit actions), but not a create action?

    Allan

  • daniloragodanilorago Posts: 19Questions: 4Answers: 0

    you are alwasy right man
    Now, hahahaha, I was able to create a new one, sending information to the other table perfectly, but my issue is on postEdit and on postRemove, that I have the error: A system error has ocurred
    Then, I got the error:
    <br/><b>Notice</b>: Undefined index: ERP__Name in <b>/home/itxm/public_html/datatables--erp.php</b> on line <b>39</b><br/>{
    "data": [
    {
    "DT_RowId": "row_11",
    "ERP": {
    "ID__ERP": "11",
    "ERP__Name": "CTMS",
    "ERP__Description": "AAA AAA AAA",
    "ERP__Date": "2025-03-12",
    "ID__Login": "1"
    },
    "Login": {
    "Login__Name": "Danilo Rago"
    }
    }
    ],
    "options": {
    "ERP.ID__Login": [
    {
    "label": "Danilo Rago",
    "value": "1"
    }
    ]
    }
    }

    The strange thing is that it's working, the name is right, but the dont work on Edit and Remove

    on my PHP I have:

    // calling functions (work on create, but not on edit)
    ->on('postCreate', function($editor, $id, &$values, &$row){
            Fc__Notification($editor->db(), 'create', $id, $values);
        })
        ->on('postEdit', function($editor, $id, &$values, &$row){
            Fc__Notification($editor->db(), 'edit', $id, $values);
        })
        ->on('postRemove', function($editor, $id, &$values, &$row){
            Fc__Notification($editor->db(), 'delete', $id, $values);
        })
    
    --------------------------------------------------
    // function that is calling
    function Fc__Notification($db, $action, $id, &$values){
        if($action=='create'){
            $db->insert('Notification', array(
                'ID__Login'                 => $_SESSION["ID__Login"],
                'Notification__ID'          => $id,
                'Notification__Table'       => 'ERP',
                'Notification__Action'      => '3',
                'Notification__Date'        => date("Y-m-d"),
                'Notification__Title'       => 'Created ERP',
                'Notification__Description' => $values['ERP']['ERP__Name'],
                'Notification__Status'      => '1'
            ));
        }elseif($action=='edit'){
            $db->insert('Notification', array(
                'ID__Login'                 => $_SESSION["ID__Login"],
                'Notification__ID'          => $id,
                'Notification__Table'       => 'ERP',
                'Notification__Action'      => '2',
                'Notification__Date'        => date("Y-m-d"),
                'Notification__Title'       => 'Edited ERP',
                'Notification__Description' => $values['ERP']['ERP__Name'],
                'Notification__Status'      => '1'
            ));
        }elseif($action=='delete'){
            $db->insert('Notification', array(
                'ID__Login'                 => $_SESSION["ID__Login"],
                'Notification__ID'          => $id,
                'Notification__Table'       => 'ERP',
                'Notification__Action'      => '1',
                'Notification__Date'        => date("Y-m-d"),
                'Notification__Title'       => 'Deleted ERP',
                'Notification__Description' => $values['ERP']['ERP__Name'],
                'Notification__Status'      => '1'
            ));
        }
    }
    

    @allan really nice job of you, I was able to solve a lot of issues on my site, earned the project and I bought the solution

  • daniloragodanilorago Posts: 19Questions: 4Answers: 0

    @allan

    I have the code:

    $('#DT__List').on('click', 'tbody td', function (e) {
                    editor.inline(this, {
                        submit: 'allIfChanged'
                    });
                });
    

    I made a huge search, but I dont have more ideas

  • daniloragodanilorago Posts: 19Questions: 4Answers: 0

    with printr it's sending only:
    Array
    (
    [ERP] => Array
    (
    [ERP__Description] => AAAAAA AAAAA AAA
    )

    )

    This menas that it's not respecting the: submit: 'allIfChanged'

  • allanallan Posts: 64,142Questions: 1Answers: 10,584 Site admin

    You've got two click events that are triggering inline editing:

                table.on('click', 'tbody td:not(:first-child)', function (e) {
                    editor.inline(this, {
                        onBlur: 'submit'
                    });
                });
                // >>> NOTIFICATION
                $('#DT__List').on('click', 'tbody td', function () {
                    editor.inline(this, {
                        submit: 'allIfChanged'
                    });
    

    The first one will run first, then the second one will do nothing since the cell is already inline editing mode. Combine them together:

                table.on('click', 'tbody td:not(:first-child)', function (e) {
                    editor.inline(this, {
                        onBlur: 'submit',
                        submit: 'allIfChanged'
                    });
                });
    

    Allan

  • daniloragodanilorago Posts: 19Questions: 4Answers: 0

    Worked fine, thanks a lot @allan

Sign In or Register to comment.