Duplicate button

Duplicate button

domlendomlen Posts: 3Questions: 1Answers: 0
edited July 8 in Free community support

I am trying to create a duplicate function, using the existing "Create" functionality and setting field defaults to existing values. I works fine, except for joined tables:

Here is the (not working button):

{
extend: 'selectedSingle',
text: '<i class="fa fa-clone"></i>',
action: function ( e, dt, node, config ) {
selected_user_site = table.cell(table.rows( { selected: true } ), 4).data();
editor
.create({
buttons: ['<i class="fa fa-save"></i>'],
})
.set(4, selected_user_site);
},
}

The problem lies with getting the value from the selected row. > table.cell(table.rows( { selected: true } ), 4).data() returns "Singapore" instead of the underlying value "5".

Answers

  • kthorngrenkthorngren Posts: 21,418Questions: 26Answers: 4,968
    edited July 9

    returns "Singapore" instead of the underlying value "5".

    Does this mean the cell contains a select element?

    You may need to use cell().node() to get the value of the select input, for example:

    selected_user_site = $( 'select', table.cell(table.rows( { selected: true } ), 1).node() ).val()
    

    If this doesn't help then please provide a simple test case showing what is contained in the cells so we can offer suggestions.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • domlendomlen Posts: 3Questions: 1Answers: 0

    I've reproduced on the live.datatables.net site:
    https://live.datatables.net/paduwane/1/

    When you select the second record and click on duplicate, the console shows "London", instead of the underlying value 2.

    Using the node() doesn't work either, as it refers to the <td> element, rather than the select option.

  • kthorngrenkthorngren Posts: 21,418Questions: 26Answers: 4,968

    I see, you are using the Editor. I think you will need to use Editor API's to get the select value. One option is to use edit() with get(), for example:
    https://live.datatables.net/paduwane/2/edit

    There may be other / better ways to do this.

    Kevin

  • rf1234rf1234 Posts: 3,010Questions: 88Answers: 421

    I use a "duplicate" button too. It is based on something Allan posted somewhere a while ago.

    The following button copies a "selected" record ("select" extension required!), edits it and switches to "create" mode. It does not copy the files associated with the copied record. And it sets a couple of global variables that I need for certain things :smile:

    Maybe that helps.

    //custom button for edit / create = Allan's "duplicate" button = ctrManagement table
    $.fn.dataTable.ext.buttons.duplicate = {
        extend: "selected",
        text: editCreateLabel,
        action: function ( e, dt, node, config ) {
            duplicate = true;
            // Start in edit mode, and then change to create
            editor
                .title($.fn.dataTable.Editor.defaults.i18n.create.title)
                .buttons({
                        label: $.fn.dataTable.Editor.defaults.i18n.create.submit,
                        className: 'btn-showall-color',
                        fn: function () {
                            this.submit();
                        }
                    })
                .edit( dt.rows( {selected: true} ).indexes() )
                .mode( 'create' );
            editor.val( "file[].id", [] );
            editor.message( function () { return  lang === 'de' ? 
                'Das Formular ist bereits mit den Daten des kopierten Vertrags \n\
                 vorausgefüllt (ohne Dokumente!). Sie können die Daten nach Belieben anpassen. \n\
                 Beim Speichern wird eine laufende Nummer für den neuen Vertrag \n\
                 erzeugt. <br><strong>Wichtig: Vertrags-Kategorien, Termine, Beschreibung \n\
                 und Kommentare werden nicht aus dem kopierten Vertrag übernommen.</strong><br>&nbsp;'   :
                'The form has been pre-filled with the data of the copied contract\n\
                 (without documents!). \n\
                 You can modify the data as you like. Upon saving a contract \n\
                 a serial number will be generated for the new contract. <br><strong>Important: \n\
                 contract categories, events, description and comments are not \n\
                 being copied.</strong><br>&nbsp;';
            });
            var table = dt;
            editor
                .one('close', function(){ //important to do it only once! because code is executed on every button click!!
                    duplicate = false;
                    table.rows().deselect(); //otherwise the source record remains selected
                });
        },
        className: "duplicateButton"
    };
    
    
  • rf1234rf1234 Posts: 3,010Questions: 88Answers: 421

    This is how I use it in "buttons":

    {   extend: "duplicate", editor: ctrEditor, className: "editorOnly", name: "duplicateContract" },
    
  • domlendomlen Posts: 3Questions: 1Answers: 0

    Thank you!

    @kthorngren answer did the trick!

    I will test @rf1234's a bit later. Thanks also.

Sign In or Register to comment.