Feature Request: custom currency symbols for types

Feature Request: custom currency symbols for types

WIR3DWIR3D Posts: 12Questions: 4Answers: 1
edited September 2014 in Free community support
$('#example').dataTable( {
  "columnDefs": [
    { "type": "num-fmt", "symbols":"R$", "targets": 0 }
  ]
} ); 

Should be self explanatory. for now I'm just using a modified plugin (the deprecated currency one) so no rush. just a suggestion.

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    Good suggestion - thanks for this. This is where DataTables currently defines the currency symbols.

    I did wonder about adding in R so it would match (for example) R$123 but it matches in any order, so a simple R would match which wouldn't be right!

    Without a more complex algorithm (prefix / postfix check for example) a sorting plug-in is the best way of doing it. Perhaps you might be willing to post your modification so others can also benefit?

    Allan

  • WIR3DWIR3D Posts: 12Questions: 4Answers: 1

    most definitely, let me just finish it. getting some false positives at the moment.

  • WIR3DWIR3D Posts: 12Questions: 4Answers: 1

    Honestly though, I wouldn't be worried about the order in which it matches the currency, as long as it matches the currency and doesn't have false positives

  • WIR3DWIR3D Posts: 12Questions: 4Answers: 1
    edited September 2014
    /**
     * This plug-in will add automatic detection for currency columns to 
     * DataTables. Note that only $, £ and € symbols are detected with this code,
     * but it is trivial to add more or change the current ones. This is best used
     * in conjunction with the currency sorting plug-in.
     * 
     * DataTables 1.10+ has currency sorting abilities built-in and will be
     * automatically detected. As such this plug-in is marked as deprecated, but
     * might be useful when working with old versions of DataTables.
     *
     *  @name Currency
     *  @summary Detect data of numeric type with a leading currency symbol.
     *  @deprecated
     *  @author [Allan Jardine](http://sprymedia.co.uk), Nuno Gomes
     *  @author modified by Rijnhard Hessel
     */
    
    (function(){
    
    var currencies = 'R';
    // Change this list to the valid characters you want
    var validChars = '0123456789' + '.,';
    
    // Init the regex just once for speed - it is "closure locked"
    var
        str = jQuery.fn.dataTableExt.internal._fnEscapeRegex( validChars ),
        re = new RegExp('^['+currencies+'][ ]?['+str+']+$');
    
    
    jQuery.fn.dataTableExt.aTypes.unshift(
       function ( data )
        {
            if (typeof data === 'string' && re.test(data) ) {
                return 'randcurrency';
            }
    
            return null;
        }
    );
    
    }());
    
    
  • WIR3DWIR3D Posts: 12Questions: 4Answers: 1

    it needs some more tweaking to be more generic, I haven't figured a way to handle dynaimic type names on the sorter side.

  • WIR3DWIR3D Posts: 12Questions: 4Answers: 1
    /**
     * This plug-in will provide numeric sorting for currency columns (either 
     * detected automatically with the currency type detection plug-in or set 
     * manually) while taking account of the currency symbol ($ or £ by default).
     *
     * DataTables 1.10+ has currency sorting abilities built-in and will be
     * automatically detected. As such this plug-in is marked as deprecated, but
     * might be useful when working with old versions of DataTables.
     *
     *  @name Currency
     *  @summary Sort data numerically when it has a leading currency symbol.
     *  @deprecated
     *  @author [Allan Jardine](http://sprymedia.co.uk)
     *  @author modified by Rijnhard Hessel
     *
     *  @example
     *    $('#example').dataTable( {
     *       columnDefs: [
     *         { type: 'currency', targets: 0 }
     *       ]
     *    } );
     */
    
    jQuery.extend( jQuery.fn.dataTableExt.oSort, {
        "randcurrency-pre": function ( a ) {
            a = (a==="-") ? 0 : a.replace( /[^\d\-\.]/g, "" );
            return parseFloat( a );
        },
    
        "randcurrency-asc": function ( a, b ) {
            return a - b;
        },
    
        "randcurrency-desc": function ( a, b ) {
            return b - a;
        }
    } );
    
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    Thanks for sharing with us. That is almost identical to the formatted numbers plug-in :-)

    Allan

This discussion has been closed.