use of https://editor.datatables.net/examples/api/duplicateButton.html but need to clear IdSrc field

use of https://editor.datatables.net/examples/api/duplicateButton.html but need to clear IdSrc field

loukinglouking Posts: 259Questions: 52Answers: 0

I want to make a copy of a row in the table, actually will be doing additional copying of database records on the server behind the scenes when the copied row is submitted.

I see https://editor.datatables.net/examples/api/duplicateButton.html which is just what I'm looking for. One problem is that the rowid (IdSrc field) is the same as the selected row. This causes a problem because I have dependent() fields which should be updated as if this were a create, not an edit (I use defaults for create and use the current values for edit, and I have been using rowid to determine if it is create or edit in the dependent ajax api).

Is there any way to reset the rows[0][rowid] value (e.g., to 0 or null) which will be used in the dependent ajax call when using the duplicateButton pattern?

Answers

  • loukinglouking Posts: 259Questions: 52Answers: 0

    I guess I found a workaround, or maybe this is just a different solution to the duplicate problem:

    function meeting_renew_button() {
        return function ( e, dt, node, config ) {
            // similar to https://editor.datatables.net/examples/api/duplicateButton.html,
            // but need to recreate the data because otherwise dependence() won't pick up defaults
            var data = dt.rows( {selected: true} ).data()[0];
            var fields = dt.editor().order();
            var newdata = {};
            for (i=0; i<fields.length; i++) {
                var field = fields[i];
                // skip a few fields which are should not be included in renewed record
                if (['gs_agenda', 'gs_minutes', 'gs_status', 'version_id'].includes(field)) continue;
                var splitfield = field.split('.');
                var value = data;
                while (splitfield.length > 0) {
                    var thisfield = splitfield.shift();
                    value = value[thisfield];
                }
                newdata[field] = value;
            }
            dt.editor()
                .create(false, {
                    title: 'Renew',
                    buttons: 'Renew Meeting'
                })
                .set(newdata)
                .open();
        }
    }
    
This discussion has been closed.