Create mulitple new records from user input

Create mulitple new records from user input

samiamsamiam Posts: 11Questions: 2Answers: 0

I'm trying to create a receiving form so when a users enters a qty of the parts received it will create multiple new records. For example, if they enter 10 in the Qty field it will create 10 new records of that item, not one record with Qty 10. I've been reading up on the new create event that allows the count parameter "create( [ count, ] [ show, ] [ options ] )" but I'm stumped on how to implement the event.

Here is my code:

    $(document).ready(function () {
        editor = new $.fn.dataTable.Editor({
            ajax: "/api/Inv",
            table: "#tblResults",
            formOptions: {
                main:
                    {
                        onEsc: 'none',
                        onBackground: 'none',
                        submit: 'changed'
                    }
            },
            fields: [
            {
                label: "Part Number:",
                name: "Inv.Part"
            },
            {
                label: "Qty:",
                name: "Qty"
            },
            {
                label: "Source:",
                name: "Inv.Source",
                type: "select"
            },
            {
                label: "Vendor:",
                name: "Inv.Vendor",
                type: "select"
            },
            {
                label: "Receive Date:",
                name: "Inv.ReceiveDate",
                type: "date",
                def: function () { return new Date(); },
                dateFormat: 'mm/dd/yy'
            }
            ]
        });


        editor.on('onInitEdit', function () {
            editor.hide('Inv.Part');
            editor.hide('Qty');
        });

        editor.on('onInitCreate', function () {
            editor.show('Inv.Part');
            editor.show('Qty');
        });

        table = $('#tblResults').DataTable({
            dom: 'liD<"float-right" B>tpr',
            ajax: "/api/Inv",
            bJQueryUI: true,
            columns: [
                { data: "Inv.Part" },
                { data: "Inv.Source" },
                { data: "Inv.Vendor" },
                { data: "Inv.ReceiveDate" }
            ],
            select: true,
            processing: true,
            filter: true,
            lengthMenu: [[20, 50, 100, 500, -1], [20, 50, 100, 500, "All"]],
            pageLength: 20,
            order: [[0, "asc"]],
            buttons: [
                {
                    extend: 'create',
                    editor: editor,
                    formTitle: "Receive Parts",
                    formButtons: ['Receive']
                },
                {
                    extend: 'edit',
                    editor: editor,
                    formTitle: "Edit Part(s)"
                },
                'selectAll',
                'selectNone',
                {
                    extend: 'colvis',
                    collectionLayout: 'fixed two-column',
                    postfixButtons: ['colvisRestore']
                }
            ]
        });

I tried adding onCreate event with a hardcoded Qty, in this case 3, like below.

        editor.on('onCreate', function() {
            editor.create(3, false);
        });

This sort of works but the form opens twice I'm assuming because the method is being called twice. Any ideas on how to achieve this with a user supplied Qty?

This question has an accepted answers - jump to answer

Answers

  • samiamsamiam Posts: 11Questions: 2Answers: 0
    edited September 2015

    Anyone have any ideas or an example? Thanks.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Hi,

    The create button should probably just be bypassed here, as all it really does is just call the create() method, but it will only ever call it to create a single record. So you need to call the create() method yourself - that can easily be done with a custom button. For example you might do something like:

    {
      text: 'Create',
      action: function () {
        editor.create( 10 );
      }
    }
    

    However, I'm not 100% sure that is what you want. That will create 10 records with identical information. I'm wondering if you might actually be better off with some server side events to take action on the value given - for example inserting 10 entries into a different table?

    Regards,
    Allan

  • samiamsamiam Posts: 11Questions: 2Answers: 0

    Thanks Allen. The 10 identical records is exactly what I need, users will add additional attributes once they are received. In your example how would I get the the Qty value from the editor form? So if the user enters 35 in the Qty field I would get 35 records in the table.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Answer ✓

    That's sort of my point - you need to know that information up front when you call create(). You could access the data directly at the server and manually insert the rows, but that wouldn't be using the create() method. If you do want to use that method you would probably need to use another Editor form to ask them how many items they want to create and then call create() with that information.

    Allan

  • samiamsamiam Posts: 11Questions: 2Answers: 0

    I understand, thanks for your help Allen!

This discussion has been closed.