Support for Arrays of objects

Support for Arrays of objects

DavidGarrettDavidGarrett Posts: 2Questions: 1Answers: 0
edited January 2015 in Free community support

Is there a reason why initializing the data property with an array of Classes works, but refreshing does not?

//class representing a row
function searchResultItem() {
        var self = this;

        self.name = 'David';
    };

var searchResults = [ new searchResultItem() ];

//works:
var dt = $('#results').dataTable({
                    data: searchResults,
                    ...

//fails:
dt.fnClearTable();
dt.fnAddData(searchResults);

referring to: http://datatables.net/forums/discussion/21408/columns-and-data-using-well-formed-objects

As the discussion above suggests, changing fnAddData to allow non-plain objects works, but I'd rather not change the core.
I couldn't find a way to override this function in a secondary js file.

        this.fnAddData = function( data, redraw )
        {
            var api = this.api( true );
        
            /* Check if we want to add multiple rows or not */
            //var rows = $.isArray(data) && ($.isArray(data[0]) || $.isPlainObject(data[0])*/) ?
            var rows = $.isArray(data) && ($.isArray(data[0]) || data[0] instanceof Object) ?

                        ...

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin

    I think this is probably a limitation of fnAddData. It tried to handle both adding a single row and multiple rows in a single function call and it just didn't work.

    Can you update to v1.10 which as row.add() and rows.add() to address exactly this.

    Allan

  • DavidGarrettDavidGarrett Posts: 2Questions: 1Answers: 0
    edited January 2015

    That worked, but why limit fnAddData by requiring the array to be made up of PlainObjects, when api.rows.add can take an array of Classes ?

    var rows = $.isArray(data) && ( $.isArray(data[0]) || $.isPlainObject(data[0]) ) ?
                    api.rows.add( data ) :
                    api.row.add( data );
    
  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin
    Answer ✓

    That worked, but why limit fnAddData by requiring the array to be made up of PlainObjects

    Simply that is how it worked in 1.9 and the old fnAddData API method will not receive any further updates from how it previously worked. The new API is where new features are added, such as in this case.

    Allan

This discussion has been closed.