Editor Create 2 new records simultaneously

Editor Create 2 new records simultaneously

vincmeistervincmeister Posts: 136Questions: 36Answers: 4

Hello Allan,

Is it possible to create 2 new records simultaneously using 1 create action ?
I want to split quantity field value into 2 new record and edit the changes field on old record

Let's say i have this data:
ID, item_name, quantity, changes, old_id

{
  "data": [
    [
      "1",
      "book",
      "100",
      "0",
      "0"
    ],
    [
      "2",
      "pencil",
      "50",
      "0",
      "0"
    ]
}

user select the row_1, and want to change the quantity from 100 to 60
my scenario:

  1. user input the new quantity (let's say 60)
  2. editor on hidden field, set default value for the rest field by duplicating data from row_1
  3. user click create button, editor will create 2 new rows like example data below (row_4 and row_5)
  4. on php, postCreate will update the row_1, field changes to 1 (like my question here )
->on('postCreate',function( $editor, $id, $values, $row ) {
            $editor->db()
                ->query('update', 'tr_receiving_detail')
                ->set('changes', 1)
                ->where('id', $values['tr_receiving_detail']['old_id'])
                ->exec();
            })

The data will be:

{
  "data": [
    [
      "1",
      "book",
      "100",
      "1",
      "0"
    ],
    [
      "2",
      "pencil",
      "50",
      "0",
      "0"
    ],
    [
      "3",
      "book",
      "60",
      "0",
      "1"
    ],
    [
      "4",
      "book",
      "40",
      "0",
      "1"
    ]
}

Please advise, thank you

Danny

This question has an accepted answers - jump to answer

Answers

  • Tom (DataTables)Tom (DataTables) Posts: 139Questions: 0Answers: 26

    Yes it is possible for Editor to create multiple rows using the create() but this is API only.

    In this case it looks like what you want to do is using the postCreate function again and insert the row using that.

    After this you would need to use ajax.reload() in order to show the extra row.

    Thanks

    Tom

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4
    edited June 2016

    Hello Tom,

    In this case it looks like what you want to do is using the postCreate function again and insert the row using that.

    How to call the postCreate function again? I already using postCreate to update, as i mention on number 4 above. Maybe link for example or reference

    Please advise, thank you

    danny

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    Two options:

    1. Just chain another ->on( 'postCreate', function ... ) to your Editor instance. Like in Javascript you can listen for the same event multiple times
    2. Insert the extra query into your existing function.

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    Hello Allan & Tom

    I've got an error Uncaught TypeError: Cannot read property 'replace' of undefined dataTables.editor.min.js:66 when i click the Create button

    Here's my code:

    I'm using a create button to submit the new 2 rows

    { extend: 'create',   
                            editor: trDOEditEditor,
                            text: 'Edit Lines'
                        }
    

    This is the trDOEditEditor, user only fill the New Quantity field

    var trDOEditEditor = new $.fn.dataTable.Editor( {
                    ajax: {
                        edit:'function/tr-packing-slip.php'
                    },
                    table: '#tblDeliveryOrder',
                    fields: [{
                                label: "ID",
                                name: "baru.id",
                                //type: "hidden"
                            },{
                                label: "Item:",
                                name: "tr_receiving_detail.item_code",
                                type:"select2"
                            }, {
                                label: "Old Quantity:",
                                name: "tr_receiving_detail.old_quantity"
                            }, {
                                label: "New Quantity:",
                                name: "tr_receiving_detail.quantity"
                            },{
                                label: "Customer:",
                                name: "tr_receiving_detail.customer_id",
                                type: "select2"
                            },{
                                label: "User Code:",
                                name: "tr_receiving_detail.enduser_code",
                            },{
                                label: "End User Name:",
                                name: "tr_receiving_detail.enduser_name",
                            },{
                                label: "Reference:",
                                name: "tr_receiving_detail.reference"
                            },{
                                label: "Receiving ID:",
                                name: "tr_receiving_detail.tr_in_id"
                            },{
                                label: "Changes:",
                                name: "tr_receiving_detail.changes",
                                //type: "hidden"
                            },{
                                label: "Old Id:",
                                name: "tr_receiving_detail.old_id",
                                //type: "hidden"
                            },{
                                label: "DOM No:",
                                name: "tr_receiving_detail.dom_no",
                                //type: "hidden"
                            }
                        ]
                } );
    

    Populate init value for the editor's field

    trDeliveryOrderDataTable.on( 'select', function (d) {
                    d.row_id = trDeliveryOrderDataTable.row( { selected: true } ).data().tr_receiving_detail.quantity;
                    trDOEditEditor.field( 'tr_receiving_detail.item_code' ).def( trDeliveryOrderDataTable.row( { selected: true } ).data().tr_receiving_detail.item_code );
                    trDOEditEditor.field( 'tr_receiving_detail.old_quantity' ).def( trDeliveryOrderDataTable.row( { selected: true } ).data().tr_receiving_detail.quantity );
                    trDOEditEditor.field( 'tr_receiving_detail.customer_id' ).def( trDeliveryOrderDataTable.row( { selected: true } ).data().tr_receiving_detail.customer_id );
                    trDOEditEditor.field( 'tr_receiving_detail.enduser_code' ).def( trDeliveryOrderDataTable.row( { selected: true } ).data().tr_receiving_detail.enduser_code );
                    trDOEditEditor.field( 'tr_receiving_detail.enduser_name' ).def( trDeliveryOrderDataTable.row( { selected: true } ).data().tr_receiving_detail.enduser_name );
                    trDOEditEditor.field( 'tr_receiving_detail.reference' ).def( trDeliveryOrderDataTable.row( { selected: true } ).data().tr_receiving_detail.reference );
                    trDOEditEditor.field( 'tr_receiving_detail.tr_in_id' ).def( trDeliveryOrderDataTable.row( { selected: true } ).data().tr_receiving_detail.tr_in_id );
                    trDOEditEditor.field( 'tr_receiving_detail.changes' ).def( trDeliveryOrderDataTable.row( { selected: true } ).data().tr_receiving_detail.changes );
                    trDOEditEditor.field( 'tr_receiving_detail.old_id' ).def( trDeliveryOrderDataTable.row( { selected: true } ).data().tr_receiving_detail.id );
                    trDOEditEditor.field( 'tr_receiving_detail.dom_no' ).def( trDeliveryOrderDataTable.row( { selected: true } ).data().tr_receiving_detail.dom_no );
                } );
    

    when user click submit button, 2 records will create with 2 different data on tr_receiving_detail.quantity field.
    1st value from the user input
    2nd value from original quantity - 1st value
    I'm using this code to genereate the 2nd value

    trDOEditEditor.on( 'preSubmit', function ( e, data, action ) {
                        var q2 = (trDOEditEditor.val('tr_receiving_detail.old_quantity')) - (trDOEditEditor.val('tr_receiving_detail.quantity'));
                    })
    

    my php:

    // to update the old record
    ->on('postCreate',function( $editor, $id, $values, $row ) {
                $editor->db()
                    ->query('update', 'tr_receiving_detail')
                    ->set('tr_receiving_detail.changes',1)
                    ->where('tr_receiving_detail.id', $values['tr_receiving_detail']['old_id'])
                    ->exec();
                })
    
    // to create the 2nd submit         
    ->on('postCreate',function( $editor, $id, $values, $row ) {
                $editor->db()
                    ->query('insert', 'tr_receiving_detail')
                    ->set('tr_receiving_detail.item_code', $values['tr_receiving_detail']['item_code'])
                    ->set('tr_receiving_detail.quantity', $_GET['q2'])
                    ->set('tr_receiving_detail.customer_id', $values['tr_receiving_detail']['customer_id'])
                    ->set('tr_receiving_detail.enduser_code', $values['tr_receiving_detail']['enduser_code'])
                    ->set('tr_receiving_detail.enduser_name', $values['tr_receiving_detail']['enduser_name'])
                    ->set('tr_receiving_detail.reference', $values['tr_receiving_detail']['reference'])
                    ->set('tr_receiving_detail.changes', $values['tr_receiving_detail']['changes'])
                    ->set('tr_receiving_detail.dom_no', $values['tr_receiving_detail']['dom_no'])
                    ->set('tr_receiving_detail.old_id', $values['tr_receiving_detail']['old_id'])
                    ->set('tr_receiving_detail.tr_in_id', $values['tr_receiving_detail']['tr_in_id'])
                    ->set('tr_receiving_detail.tr_return', $values['tr_receiving_detail']['tr_return'])
                    ->exec();
                })
    

    Please advise, thank you
    Danny

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    Hi Danny,

    Can you link to the page showing the issue so we can attempt to debug it please. It sounds like the Javascript error is occurring from the JSON return, but really I'm not sure without being able to see it.

    If you comment out the second postCreate so that stop the replace error from occurring?

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    Hi Allan,

    Here's the live link
    Step:
    1. Select Delivery Order
    2. Select a line from the loaded data
    3. Click Edit Lines Button
    4. Fill the new quantity

    Pleas advise, thank you

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    Thanks for the link. Could you use the non-min version of Editor please? The page is taking about 5 minutes to load at the moment, so I'll try to debug it later.

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    hello Allan

    please try this link
    load time problem maybe from the IX server problem

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    Thanks - that's much faster.

    The issue is that although the "Edit" button is clicked on a "Create" form is shown (demonstrated by the form title: Create new entry). In your Editor initialisation you have:

    ajax: {
      edit: "function/tr-packing-slip.php"
    }
    

    Nothing about create, which is the mode the form is in, hence the error.

    I'm not sure if the bug is that create should be specified or if the form should be put into edit mode.

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4

    Hi Allan,

    Thanks for the correction. Changing edit to create, no error, but still no result as expected. Please take a look, thank you

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin
    Answer ✓

    No data is being returned from the server:

    {"data":[]}
    

    I can only presume that is due to a where condition being applied to the table. When Editor creates a new row (as it is being instructed to do here) it should automatically read that new row and return the values to the client-side.

    Allan

  • vincmeistervincmeister Posts: 136Questions: 36Answers: 4
    edited July 2016

    Hi Allan,

    Thanks for the clue
    The problem is on the select2 filter value so the data return []

    I add ajax data on editor d.kode_distro = $("#SelectDO").val(); and works perfectly as i expected. It's solves now. You're the best :) Many thanks

    Danny

This discussion has been closed.