Adding rows in local table editor

Adding rows in local table editor

sliekenssliekens Posts: 97Questions: 17Answers: 2
edited December 2016 in Free community support

Adding rows to a table without ajax configured does not work in Editor 1.6.1
https://editor.datatables.net/examples/simple/noAjax.html

The following statement throws because there is no original data to extend.

// Get the original row's data, so we can modify it with new values.
// This allows Editor to not need to submit all fields
var rowData = originalData[ key ].data;

Uncaught TypeError: Cannot read property 'data' of undefined

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,836Questions: 1Answers: 10,518 Site admin
    Answer ✓

    Darn - that was daft of me. Sorry about that.

    The function should be:

    Editor.prototype._submitTable = function ( data, success, error )
    {
        var that = this;
        var action = data.action;
        var out = { data: [] };
        var idSet = DataTable.ext.oApi._fnSetObjectDataFn( this.s.idSrc );
    
        // Nothing required for remove - create and edit get a copy of the data
        if ( action !== 'remove' ) {
            var originalData = this._dataSource( 'fields', this.modifier() );
    
            $.each( data.data, function ( key, vals ) {
                var toSave;
    
                // Get the original row's data, so we can modify it with new values.
                // This allows Editor to not need to submit all fields
                if ( action === 'edit' ) {
                    var rowData = originalData[ key ].data;
                    toSave = $.extend( true, {}, rowData, vals );
                }
                else {
                    toSave = $.extend( true, {}, vals );
                }
    
                // Create a new id for `create` and use the existing one for edit
                idSet( toSave, action === 'create' ?
                    +new Date() +''+ key :
                    key
                );
    
                out.data.push( toSave );
            } );
        }
    
        success( out );
    };
    

    That will be in Editor 1.6.2.

    Thanks for letting me know about this!

    Allan

  • sliekenssliekens Posts: 97Questions: 17Answers: 2
    edited December 2016

    Glad I could help. Thanks for the fix.

    (one of the code comments is still wrong, by the way)

  • sliekenssliekens Posts: 97Questions: 17Answers: 2
    edited December 2016

    Does _submitTable execute before or after preSubmit? In the latter case, it would be prudent to only create a new id if preSubmit didn't already create one.

    var idSrc = this.s.idSrc;
    
    // ...
    
    if (action === 'edit') {
        idSet(toSave, key);
    } else {
        if (toSave[idSrc]) {
            idSet(toSave, toSave[idSrc]);
        } else {
            idSet(toSave, +new Date() + '' + key);
        }
    }
    
    
  • allanallan Posts: 63,836Questions: 1Answers: 10,518 Site admin

    It executes after preSubmit.

    What you suggest sounds eminently sensible to me - thanks! I'll also get that in for 1.6.2.

    Allan

This discussion has been closed.