If is it safe to add columns on the fly with cloning?

If is it safe to add columns on the fly with cloning?

mehanymehany Posts: 2Questions: 1Answers: 0
edited October 2016 in Free community support

The situation is new columns needs to be added to a table powered by ajax. As of now, as far as I know, there is no way to add new columns to the table through Datatable API for tables that have "serveside" property enabled. The only way I managed to make it possible is with the bellow code but I think it could break under certain conditions. Basically using

('xhr.dt', function(e, settings, json, xhr)

function dtAddColumn(settings, xhr){
    var clonedRow = cloneObject(settings.aoColumns[settings.aoColumns.length-1]);
    clonedRow.idx=settings.aoColumns.length;
    clonedRow.mData=settings.aoColumns.length;
    clonedRow.aDataSort=[settings.aoColumns.length];
    clonedRow.sTitle="savings";

    var newRow = cloneObject(settings.aoPreSearchCols[settings.aoPreSearchCols.length-1]);
    newRow.bCaseInsensitive = true;
    newRow.bRegex = false;
    newRow.bSmart = true;
    newRow.sSearch = "";
    settings.aoPreSearchCols.push(newRow);
}

clone object function

function cloneObject(source) {
    var key,value;
    var clone = Object.create(source);

    for (key in source) {
        if (source.hasOwnProperty(key) === true) {
            value = source[key];

            if (value!==null && typeof value==="object") {
                clone[key] = cloneObject(value);
            } else {
                clone[key] = value;
            }
        }
    }

    return clone;
}

I simplified my code but the idea is to gain access to the data returned and generate additional columns based on certain values returned in the data object.

Answers

This discussion has been closed.