Solved Datatables CSV import using join table

The Problem : Dot nation

Solution

function ref(obj, str) {
    return str.split(".").reduce(function(o, x) { return o[x] }, obj);
}   

var mapped = ref(data, field.name());


Using this way to read { tablename: { field1, field2 } } It will turn to read tablename.field1 and tablename.field2

Modified Code from Official CVS Import

    selectEditor.on('submitComplete', function (e, json, data, action) {

        // Use the host Editor instance to show a multi-row create form allowing the user to submit the data.
        editor.create( csv.length, {
            title: 'Confirm import',
            buttons: 'Submit',
            message: 'Click the <i>Submit</i> button to confirm the import of '+csv.length+' rows of data. Optionally, override the value for a field to set a common value by clicking on the field below.'
        } );

        // Solved the dot notation bug.
        function ref(obj, str) {
            return str.split(".").reduce(function(o, x) { return o[x] }, obj);
        }    
    
        for ( var i=0 ; i<fields.length ; i++ ) {
            var field = editor.field( fields[i] );

            // var mapped = data[ field.name() ];
            var mapped = ref(data, field.name());
 
            for ( var j=0 ; j<csv.length ; j++ ) {
                field.multiSet( j, csv[j][mapped] );
            }
        }
    } );
}    

Tags:

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.