Trying to change single create editor instance to multi entry creation based on field value

Trying to change single create editor instance to multi entry creation based on field value

Martyn.sMartyn.s Posts: 7Questions: 2Answers: 0

Hello everyone,

As the title says, I'm trying to figure out if I can change an editor instance created with the .create() function to a multi row / entry creation instance from one that would only create one.

Is this possible? I've tried looking through documentation and I am still not sure. Many thanks!

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 3,116Questions: 91Answers: 429
    edited June 10

    to a multi row / entry creation instance from one that would only create one.

    What does this really mean? Do you want to create multiple identical rows? How many and why? What is your use case in terms of desired business outcome?

    Have you read this?
    https://editor.datatables.net/manual/multi-row#Create-new-rows

  • Martyn.sMartyn.s Posts: 7Questions: 2Answers: 0

    Not multiple identical rows, but each row would have a different identifier / serial number. I'm trying to let the user select the quantity of how many entries they want with an input field, and then adjust the form to create that many entries into the database, each with a different serial number (i.e 001-1-1, then 001-1-2, etc). Otherwise, the multiple entries will remain the same.

    This is to allow for the capacity to bulk add as many new serial numbers to the database as the user wants without needing to go back and refill the form over and over again

  • kthorngrenkthorngren Posts: 22,035Questions: 26Answers: 5,082
    edited June 10

    Possibly use inlineCreate() to create rows in the current table. They will need to be added one at a time. Your input to choose the number of rows to add can trigger an event handler that calls inlineCreate(). Use something like submitSuccess to decrement the inputted value and call inlineCreate() if needed. I built a simplistic example showing this:
    https://live.datatables.net/guwafemu/621/edit

    Also see this example.

    There may be other better options/solutions that others might provide.

    Kevin

  • allanallan Posts: 64,593Questions: 1Answers: 10,683 Site admin

    create() can indeed be used to create multiple rows at a time - it has an optional count parameter that can be passed in.

    If the sequence is created client-side based on user input, have it throw up the form as normal then use initSubmit with the multi-row editing API to set the values.

    If it is a database generated sequence, even better - just submit the form as normal and the server-side scripts will insert the rows indicated.

    I use exactly this approach for the booking of a Village Hall that I help run when we have repeating events (weekly classes).

    Allan

  • Martyn.sMartyn.s Posts: 7Questions: 2Answers: 0

    Hi Allan,

    I mostly follow but could you provide an example of the code used on the initSubmit event? I've been trying to follow it using the cult-row Editing API and multiset() but I am having no luck

  • allanallan Posts: 64,593Questions: 1Answers: 10,683 Site admin
    Answer ✓
    editor.on('initSubmit', function (e, action) {
      if (action === 'create') {
        let keys = editor.ids();
    
        for (let i=0 ; i<keys.length ; i++) {
          editor.field('seq').multiSet(keys[i], 'seq-val-' + i);
        }
      }
    });
    
    • Line 2: Make sure it only runs on create actions
    • Line 3: Get a list of the ids - this tells us how many rows are being creating. In create mode this is just an array of indexes starting from 0.
    • Line 5: For the field seq, for each row being created set a value.

    Hope that helps.

    Allan

  • Martyn.sMartyn.s Posts: 7Questions: 2Answers: 0

    It's helped and worked a charm! Thank you :)

Sign In or Register to comment.