Giovanni Di Toro
Giovanni Di Toro

Reputation: 809

Tablesorter currency cleaning

I spent quite a good time twicking arround tablesorter to get it to work with values such as :"R$ 3.400,95" Needless to say, I failed miserably. I did try to add a headers: {2: {sorter:"currency"}} property , but it just stopped working at all. Any one has any idea how to solve this?

The Javascript:

$.tablesorter.addParser({ 
    // set a unique id 
    id: 'thousands',
    is: function(s) { 
        // return false so this parser is not auto detected 
        return false; 
    }, 
    format: function(s) {
        // format your data for normalization 
        return s.replace('$','').replace(/,/g,'');
    }, 
    // set type, either numeric or text 
    type: 'numeric' 
}); 


$(function() {
// call the tablesorter plugin
$("#ver_saida").tablesorter({
    decimal: ",",
    dateFormat: 'uk',
    headers:{2:{sorter:'thousands'}}
});

});

The other headers work fine, but that last property makes that particular header stop working.

Here is the HTML table:

<table id="ver_saida" class="tablesorter">
    <thead>
        <tr>
            <th class="sorter-shortDate dateFormat-ddmmyyyy tablesorter-header">Data<span>n</span></th>
            <th>Descrição<span>n</span></th>
            <th>Valor<span>n</span></th>
            <th>Situação<span style="margin-left:2em;">n</span></th>
        </tr>
    </thead>
    <tr class="pago">
        <td class="datout">17/05/2012</td>
        <td class="desout">atraso teste</td>
        <td class="valout"> R$45,46</td>
        <td class="situacao">pago</td>
        <td class="delCel"><button class="deletar" href="financeiro_deletar.php?id=36">Deletar</button></td>
    </tr>
    <tr class="npago late">
        <td class="datout">13/06/2012</td>
        <td class="desout">IPVA macerati</td>
        <td class="valout"> R$5.565,62</td>
        <td class="situacao">não pago</td>
        <td class="delCel"><button class="deletar" href="financeiro_deletar.php?id=38">Deletar</button></td>
    </tr>
<table>

I made an experiment: if I take out the "R$" from the cell html it reads with no problem , but the thing is , I don1t know how to make it ignore the "R$" and still leave it in the table ( for readability porposes).

Upvotes: 1

Views: 286

Answers (1)

dievardump
dievardump

Reputation: 2503

Modify the 'format' method with:

format: function(s) {
        // format your data for normalization 
        return parseFloat(s.replace(/[^0-9,]/g, '').replace(',', '.'), 10);
    },

Upvotes: 2

Related Questions