Trying to use multiset to create several rows at once

Trying to use multiset to create several rows at once

dhDTforwebdhDTforweb Posts: 27Questions: 6Answers: 0

This code shows a form to create a record. Then it shows another form to create another record. The second record is repeated three times in the database.
Problems:
the multiset values (Charlie, etc) don't get put in the three records. They all take whatever was entered for dataname in the second form (blank or otherwise)
I don't want the first form to appear, just the one that will make three records.

    editor.on( 'create', function ( e ) {
        editor.create( 3, 'Add new record', 'Save' );
        editor.field( 'dataname' )
            .multiSet( 0, 'Allan' )
            .multiSet( 1, 'Bob' )
            .multiSet( 2, 'Charlie' );

            //alert( 'New row added' );
    } );

I am calling the button with this

buttons: [
    { extend: 'create', editor: editor }
],

The code is from this example
https://editor.datatables.net/reference/api/create()
Any ideas? Thanks,

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    edited October 2018

    Hi @dhDTforweb ,

    The problem was because you're doing the multiSet() in the create, which is responding to the first create. If you make a new button, as in this example here, then that will do the trick,

    Cheers,

    Colin

  • dhDTforwebdhDTforweb Posts: 27Questions: 6Answers: 0

    Perfect! Thanks!
    Now I need to pass one of the column values into the new form. I want all new records to have the same projectid.

    columns: [
    
                {
                    "data": "projectid",
                            "visible": false
                           
        
            },
    

    I tried this but the values don't appear

          {
            extend: "create", 
            text: "make three",
        data: 'projectid',
            action: function ( e, dt, node, config ) {
              editor.create( 3, 'Add new record', 'Save' );
              editor.field( 'projectid' )
                .multiSet( 0, data )
                .multiSet( 1, data  )
                .multiSet( 2, data  );
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @dhDTforweb ,

    I might be getting confused, but if you want them all the same, you don't need multiSet(), you can just set the field like this (see example here),

    editor.create( 3, 'Add new record', 'Save' ).set( 'position', 'the same' );
    

    Cheers,

    Colin

  • dhDTforwebdhDTforweb Posts: 27Questions: 6Answers: 0

    Thanks. What if the page didn't show all the employees, but only the employees of a certain city (i.e. the apex call sends a city_ID to the page that generates the json. That page filters out all employees that are not in that city)?

    Then, hard coding the city name wouldn't be feasible; I need to pull the city name from the returned json code and load it into each of the three new records.

    This is to save typing in the city on every record when you create a bunch of new records. In the final form, there will be four or more fields to be set like this.

    Thanks for your help. I will keep working on it. It should just be a matter of setting a variable equal to a particular field in the returned json code, and then telling muliset to use that variable value when it creates each record.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @dhDTforweb ,

    Ha, I feel like I'm playing catchup here - I answer your question, then there's more information :)

    It might be best if you restart and say what you want from the beginning. I appreciate you want to keep the city_ID the same, but if you step back and describe the process, that would help us to understand and suggest a solution.

    Cheers,

    Colin

  • dhDTforwebdhDTforweb Posts: 27Questions: 6Answers: 0

    Sorry for the confusion. I hope this explanation helps:
    Here is the page I am working on:
    https://quiet-wildwood-42059.herokuapp.com/index.php/Building
    The data hierarchy is:
    Project ID
    ____Building ID
    _______Equipment ID
    __________Sub unit ID
    Notice that building 1 has project id 1.
    Click on building ID 1, then equipment ID 1 (twice)
    You should now be on this page
    https://quiet-wildwood-42059.herokuapp.com/index.php/SubUnit/viewSubUnitOfEquipment/1
    Equipment 1 has building id 1 and project id 1 (these could be any numbers
    Equipment 14 could be in project 37 and have sub unit 45, etc.)

    I want a button here that will create a bunch of subunits with the project, building, and equipment id's prefilled with the ids used for the equipment that is currently displayed. The other fields can be blank

    I am looking for something like this:

    ajax: {
                url: '/index.php/Ajax/subUnitOfEquipment',
                type: "POST",
                var currentProjectID = the value of the field 'projectid' from the returned json data  
    

    And then

              editor.create( 3, 'Add new record', 'Save' );
              editor.field( 'projectid' )
                .multiSet( 0, currentProjectID )
                .multiSet( 1, currentProjectID  )
                .multiSet( 2, currentProjectID  );
    

    Thanks for all your help so far. I will keep working on it

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin

    What if the page didn't show all the employees, but only the employees of a certain city (i.e. the apex call sends a city_ID to the page that generates the json. That page filters out all employees that are not in that city)?

    When you create a record, it doesn't matter if it wouldn't be shown by the filtering condition. It will still be created, just not shown.

    Then, hard coding the city name wouldn't be feasible; I need to pull the city name from the returned json code and load it into each of the three new records.

    I'm not exactly sure how the "city" field fits in since I don't see it on the page, nor was it mentioned previously, however, if that information is in the returned JSON, then yes, you could set it that way.

    Do you need user interaction with the second form, or is it just being submitted immediately? If the later, it would be better to just do all this on the server-side, rather than round-tripping back to the client again.

    Allan

This discussion has been closed.