表分拣机货币清理

Tablesorter currency cleaning

本文关键字:货币 拣机      更新时间:2023-09-26

我花了很多时间在 twick arround tablesorter 上让它处理诸如"R$ 3.400,95"之类的值不用说,我失败得很惨。我确实尝试添加一个headers: {2: {sorter:"currency"}}属性,但它根本停止工作。有人知道如何解决这个问题吗?

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'}}
});
});
其他标头工作

正常,但最后一个属性使该特定标头停止工作。

下面是 HTML 表:

<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>

我做了一个实验:如果我从单元格html中取出"R$",它可以毫无问题地阅读,但问题是,我不知道如何让它忽略"R$"并仍然将其留在表中(为了可读性)。

修改 'format' 方法:

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