Formatting value of column based on type.

Formatting value of column based on type.

SconesScones Posts: 6Questions: 0Answers: 0
edited July 2012 in General
I have added type detection plugin for a type (using jQuery.fn.dataTableExt.aTypes) returned by a Json call, is it possible to add a global value handler for that specific type?

Currently datatables shows the value [Object object], and i want to add a global handler to parse the object into a string value based on properties on the object, instead of using fnRender on each and every column that has this type.

Is this possible? If so, how?

Replies

  • SconesScones Posts: 6Questions: 0Answers: 0
    edited July 2012
    Solved it with this:

    [code]

    var dataTablesTypeFormatters = {
    timespan: function (value) {
    return formatTimeSpan(value);
    }
    };

    $.extend($.fn.dataTable.defaults, {
    "fnCreatedRow": function (nRow, aData, iDataIndex) {
    var aoColumns = $(this).dataTable().fnSettings().aoColumns;
    for (var iColumnIndex = 0; iColumnIndex < aoColumns.length; iColumnIndex++) {
    var oColumn = aoColumns[iColumnIndex];

    //If a fnRender is defined, just continue, the type is then already handled
    if (typeof oColumn.fnRender == 'function') { continue; }

    var sType = oColumn.sType;
    var mDataProp = oColumn.mDataProp;

    if (dataTablesTypeFormatters.hasOwnProperty(sType)) {
    var parsedValue = dataTablesTypeFormatters[sType](aData[mDataProp]);
    $(nRow).children().eq(iColumnIndex).html(parsedValue);
    }
    };
    }
    });

    [/code]

    This does however require that all columns are defined in aoColumns and have mDataProp.

    Suggestions for improvements are welcome.
This discussion has been closed.