Question regarding adding a new item in a datatable? - Page 2

Question regarding adding a new item in a datatable?

2»

Answers

  • webpointzwebpointz Posts: 126Questions: 30Answers: 4

    Hi Allan, almost there...

    So, I put the following code :

            if ( isset($_POST['action']) && $_POST['action'] === 'create' ) {
            
                $rowid = ltrim($data['row']['DT_RowId'],"row_");
            
            $db->insert( 'seh_itemstock', [ 'itemid' => $rowid, 'itemname' => $_POST['data']['seh_kititems']['description'], 'insertdate' => date('Y-m-d H:i:s'), 'userid' => $_SESSION['userid'], 'stockcount' => $_POST['data']['seh_kititems']['quantity'] ] );      
            
        }
    

    When I create a new item, it logs the id now to the "itemstock" table correctly...BUT, now I get an error if I do an "edit/update":

    Notice:  Undefined index: id in php/lib/Editor/Editor.php on line 523
    
    {"error":"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '48' for key 'PRIMARY'"}
    
    

    Am I using the wrong syntax to detect a newly created record?

  • webpointzwebpointz Posts: 126Questions: 30Answers: 4

    Basically it's now working EXCEPT when I do an EDIT/UPDATE I get the error in my previous comment.

    Here is my PHP file:

    <?php
    
    session_start();
    
    /*
     * Editor server script for DB table table_products
     * Created by http://editor.datatables.net/generator
     */
    
    
    // DataTables PHP library and database connection
    include( "lib/DataTables.php" );
    
    
    // Alias Editor classes so they are easy to use
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Join,
        DataTables\Editor\Validate;
    
    
    // Build our Editor instance and process the data coming from _POST
    $editor = Editor::inst( $db, 'seh_kititems', 'id' )
        ->fields(
            Field::inst( 'seh_kititems.id' ),
            Field::inst( 'seh_kititems.referenceid' ),
            Field::inst( 'seh_kititems.description' )
                ->validator( 'Validate::notEmpty' ),
            Field::inst( 'seh_kititemtypes.name' ),
            Field::inst( 'seh_kititems.type' )
                        ->options( 'seh_kititemtypes', 'id', 'name' )
                        ->validator( 'Validate::notEmpty' ),
                Field::inst( 'seh_kititems.quantity' ),
            Field::inst( 'seh_kititems.warning' )
        )
        
        ->leftJoin( 'seh_kititemtypes', 'seh_kititemtypes.id', '=', 'seh_kititems.type' );
    
        $data = $editor
            ->process( $_POST )
            ->data();
            
            if ( Editor::action( $_POST ) === Editor::ACTION_CREATE ) {
    
            $create_rowid = ltrim($data['row']['DT_RowId'],"row_");
            
            $db->insert( 'seh_itemstock', [ 'itemid' => $create_rowid, 'itemname' => $_POST['data']['seh_kititems']['description'], 'insertdate' => date('Y-m-d H:i:s'), 'userid' => $_SESSION['userid'], 'stockcount' => $_POST['data']['seh_kititems']['quantity'] ] );   
        }
        
            if ( Editor::action( $_POST ) === Editor::ACTION_EDIT ) {
    
            $create_rowid = $_POST['data']['seh_kititems']['id'];
            
            $db->insert( 'seh_itemstock', [ 'itemid' => $create_rowid, 'itemname' => $_POST['data']['seh_kititems']['description'], 'insertdate' => date('Y-m-d H:i:s'), 'userid' => $_SESSION['userid'], 'stockcount' => $_POST['data']['seh_kititems']['quantity'] ] );   
        }   
            
        echo json_encode( $data );
    
            ]
        }
    } );
    

    } );

    }(jQuery));

    ```

  • webpointzwebpointz Posts: 126Questions: 30Answers: 4
    edited October 2015

    Here is my JS file:

    I have THREE buttons above the Datatable : "New" "Edit" "Add Quantity".
    The new and edit are normal modal boxes to add and update. The Add Quantity is a custom button that only allows quantities to be added.

    The NEW and ADD QUANTITY work fine but the EDIT throws the error.

    The ADD QUANTITY uses the "stockEditor" function to add more quantity.

    ```
    /*
    * Editor client script for DB table table_products
    * Created by http://editor.datatables.net/generator
    */

    var editor; // use a global for the submit and return data rendering in the examples

    (function($){

    $(document).ready(function() {

    var editor = new $.fn.dataTable.Editor( {
    
        "ajax": "php/table.seh_kititems.php",
        "table": "#seh_kititems",
        "idSrc": "id",
    
        "i18n": {
            "edit": {
            "button": "Edit Item",
            "title":  "Update Item",
            "submit": "Update Item"
            },
            "create": {
            "button": "Add Item",
            "title":  "Add New Item",
            "submit": "Add Item"
            }},     
    
        "fields": [
            {
                "label": "Item ID:",
                "name": "seh_kititems.id",
                "type": "hidden"
            },      
            {
                "label": "Ref ID:",
                "name": "seh_kititems.referenceid"
            },
            {
                "label": "Description:",
                "name": "seh_kititems.description"
            },
            {
                "label": "Type:",
                "name": "seh_kititems.type",
                "type": "select"
                },              
            {
                "label": "Quantity:",
                "name": "seh_kititems.quantity"
            },
            {
                "label": "Quantity Warning:",
                "name": "seh_kititems.warning"
            }
    
        ]
    } );
    
    editor.on( 'onInitEdit', function () {
      editor.disable('seh_kititems.quantity');
    } );    
    
        var stockEditor = new $.fn.dataTable.Editor( {
    
            ajax: "php/table.seh_kititems.php",
            table: "#seh_kititems",
            fields: [ 
                {label: "Item ID:",
                    name:  "seh_kititems.id",
                type: "hidden"
                },          
            {label: "Add Quantity:",
                    name:  "addstock"
                },        
                {label: "Current Quantity:",
                    name:  "seh_kititems.quantity",
                type: "hidden"
                },
                {label: "Ref ID:",
                    name:  "seh_kititems.referenceid",
                type: "hidden"
                },
                {label: "Description:",
                    name:  "seh_kititems.description",
                type: "hidden"
                }                           
                ]
    
        });
    
    function callModal(aData) {
        $("#itemid").val(aData);
        $("#itemIdHere").text(aData);
        $("#myModal").modal({
        persist: true       
            });
    }
    
    
    editor.on( 'preSubmit', function ( e, data, action ) {
    
      if ( action === 'edit' ) {
    
            if ( !$.isNumeric(data.data.seh_kititems.quantity) ) {
    
                this.error('A valid number is required for Quantity');
                return false;
            }   
    
            if ( !$.isNumeric(data.data.seh_kititems.warning) ) {
    
                this.error('A valid number is required for Quantity Warning');
                return false;
            }                     
      }
    
      if ( action === 'create' ) {
    
            if ( !$.isNumeric(data.data.seh_kititems.quantity) ) {
    
                this.error('A valid number is required for Quantity');
                return false;
            }   
    
            if ( !$.isNumeric(data.data.seh_kititems.warning) ) {
    
                this.error('A valid number is required for Quantity Warning');
                return false;
            }   
      }   
    
    } );       
    
    editor.on( 'onInitEdit', function () {
      editor.disable('seh_kititems.quantity');
    } );    
    
    editor.on( 'onInitCreate', function () {
      editor.enable('seh_kititems.quantity');
    } );    
    
    
    
    stockEditor.on( 'preSubmit', function ( e, data, action ) {
    
    var currStockval = parseInt(data.data.seh_kititems.quantity);
    var addStockval  = parseInt(data.data.addstock);
    
    
    var newStockval  = addStockval += currStockval;
    
      if ( action === 'edit' ) {
    
            if ( !$.isNumeric(addStockval) ) {
    
                this.error('A valid number is required for Add Quantity');
                return false;
            }     
    
        data.data.seh_kititems.quantity = newStockval;
        data.data.addstock = addStockval;
      }
    } );  
    
    
    
    
    var table = $('#seh_kititems').DataTable( {
        "dom": "Tfrtip",
        "ajax": "php/table.seh_kititems.php",
        "order": [[ 0, "asc" ]],
        "columnDefs": [ {
            "targets": 0,
            "data": null                    
            } ],            
    
        "columns": [
    
            {
                "data": "seh_kititems.referenceid"
            },
            {
                "data": "seh_kititems.description"
            },
            { 
                "data": "seh_kititemtypes.name"
            },              
            {
                "data": "seh_kititems.quantity",
                "className": "dt-right"
            },
            {
                "data": "seh_kititems.warning",
                "className": "dt-right"
            }           
        ],
    
    
        tableTools: {
            sRowSelect: "os",
            aButtons: [
                { sExtends: "editor_create", editor: editor },
                { sExtends: "editor_edit",   editor: editor },
                {
                    sExtends: 'select_single',
                    sButtonClass: 'marginLeft',
                    sButtonText: 'Add Quantity',
                    fnClick: function () {
                        if ( table.row( '.selected' ).length !== 0 ) {
                            // Show the quantity update form
                            stockEditor
                                .title( 'Add Quantity' )
                                .buttons( { label: 'Add Quantity', fn: function imageUpload() { this.submit(); } } )
                                .edit( table.row( '.selected' ).node() );
    
    
    
                        }
                    }
                },
                {
                    sExtends: 'select_single',
                    sButtonClass: 'marginLeft',
                    sButtonText: 'Add Image',
                    fnClick: function () {
    
                        if (this.fnGetSelected().length===1) {
    
                        var aData = table.cell('.selected', 0).data();
    
            callModal (aData);
    
    
                        }
                    }
                }              
    
  • webpointzwebpointz Posts: 126Questions: 30Answers: 4

    I have resolved it now by creating a custom UPDATE button.

    Thanks for all your help.

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Hi,

    Good to hear you've got it working now!

    Allan

This discussion has been closed.