1

私は現在、オリンピックの代替メダル数に取り組んでおり、tablesorter-plugin を使用して、ユーザーがデータをさまざまな角度から見ることができるようにしています。

行の正しい順序については、次の点に固執します。2 つの国が同じ量の金メダルを持っている場合、銀メダルを見てください。銀メダルの多い国が1位、銀メダルの少ない国が2位となります。

tablesorter を使用してこれを達成するにはどうすればよいですか?

http://www.benedictzinke.de/olympiaでソースを見ることができます。

今では、10選手あたりの金メダルの後にソートされています. 少なくとも 1 つの金メダルを獲得した国のすべてが適切に分類されています。しかし、金メダルのない国の列はぐちゃぐちゃ。

4

2 に答える 2

0

tablesorterプラグインを使用して、独自のパーサーの作成に関するドキュメントを参照する必要があります。ここでそれを参照してください:http://tablesorter.com/docs/example-parsers.html

あなたが求めているものは、ドキュメントで使用されている例にほぼ正確に適合しているようです。便宜上、ドキュメントのコードを以下にコピーします。

// add parser through the tablesorter addParser method 
$.tablesorter.addParser({ 
    // set a unique id 
    id: 'grades', 
    is: function(s) { 
        // return false so this parser is not auto detected 
        return false; 
    }, 
    format: function(s) { 
        // format your data for normalization 
        return s.toLowerCase().replace(/good/,2).replace(/medium/,1).replace(/bad/,0); 
    }, 
    // set type, either numeric or text 
    type: 'numeric' 
}); 

$(function() { 
    $("table").tablesorter({ 
        headers: { 
            6: { 
                sorter:'grades' 
            } 
        } 
    }); 
});  

あなたの目的のために、コードは次のようになります:

// add parser through the tablesorter addParser method 
$.tablesorter.addParser({ 
    // set a unique id 
    id: 'medals', 
    is: function(s) { 
        // return false so this parser is not auto detected 
        return false; 
    }, 
    format: function(s) { 
        // format your data for normalization 
        // Note the 'i' added after the medal type, that makes it case insensitive.
        return s.toLowerCase().replace(/gold/i,2).replace(/silver/i,1).replace(/bronze/i,0); 
    }, 
    // set type, either numeric or text 
    type: 'numeric' 
}); 

$(function() { 
    $("table").tablesorter({ 
        headers: { 
            6: {  // Replace '6' with the number of the column that your medals are in
                sorter:'medals' 
            } 
        } 
    }); 
}); 
于 2012-08-07T20:15:38.373 に答える