DataTables logo DataTables

via Ad Packs
Formatting value of column based on type.
  • SconesScones
    Posts: 6
    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?
  • SconesScones
    Posts: 6
    Solved it with this:

    
    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);
                }
            };
        }
    });
    
    

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

    Suggestions for improvements are welcome.
This discussion has been closed.
← All Discussions

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Support

Get useful and friendly help straight from the source.

In this Discussion