Весовая сортировка с помощью Jquery Tablesorter2.0 - PullRequest
0 голосов
/ 13 октября 2011

Я пытаюсь отсортировать таблицу, в которой есть столбцы, такие как 385 фунтов 12 унций, 357 фунтов 8 унций, 84 фунта 11 унций и т. Д. Я использую ниже, чтобы применить сортировку

 jQuery(document).ready(function()
    {
        jQuery(".world-records").tablesorter(
                {widgets: ['zebra']},
                {dateFormat: "us"},
                {headers:{5:{sorter:"Weight"}}}
        );

        // add parser for weight
        $.tablesorter.addParser({ 
    // set a unique id 
    id: 'Weight', 
    is: function(s) { 
        // return false so this parser is not auto detected 
        return false; 
    }, 
    //format the column for sorting
    format: function(s) { 
        var array,wieght_decimal; 
        array = s.split(" ");

        //we should always have lbs even if it's zero
        wieght_decimal = parseInt(array[0]);

        //if we have ounces, add it to the lbs
        if(array[2]){
             wieght_decimal = wieght_decimal + 0.0625*parseInt(array[2]); 
        }

        return wieght_decimal; 
    }, 
    // set type, either numeric or text 
    type: 'numeric' 
}); 
    });

Ниже приведен формат таблицы

<table class="world-records" class="tablesorter">
    <thead>
        <tr class="table-titles" style="border-bottom:blank;">
            <th class="headerSortDown">
                <span>
                    Brand
                </span>
            </th>
            <th class="headerSortDown" id="date-sort">
                <span>
                    Date
                </span>
            </th>
            <th class="headerSortDown">
                <span>
                    Angler
                </span>
            </th>
            <th class="headerSortDown">
                <span>
                    Species
                </span>
            </th>
            <th class="headerSortDown" id="Weight">
                <span>
                    Weight
                </span>
            </th>
            <th class="headerSortDown">
                <span>
                    Lure
                </span>
            </th>
            <th class="headerSortDown">
                <span>
                    LineClass
                </span>
            </th>
            <th class="headerSortDown">
                <span>
                    Status
                </span>
            </th>
        </tr>
    </thead>
    <tbody>
        <tr class="odd">
            <td>
                <span class="logotxt">
                    vmc
                </span>
                <img alt="" style="clear: left;" class="logo small" src="logo-vmc.jpg?$staticlink$"
                />
            </td>
            <td>
                12/01/1996
            </td>
            <td>
                Robert Busby
            </td>
            <td>
                Albacore Tuna
            </td>
            <td>
                50 lb 911 oz
            </td>
            <td>
                Magnum
            </td>
            <td>
                M-37 kg (80 lb)
            </td>
            <td class="last-td">
                Approved
            </td>
        </tr>
        <tr class="even">
            <td>
                <span class="logotxt">
                    rapala
                </span>
                <img style="clear: left;" class="logo small" src="logo-rapala.jpg?$staticlink$"
                alt="" />
            </td>
            <td>
                1/12/1887
            </td>
            <td>
                Hampl, Christopher
            </td>
            <td>
                Barracuda, Pacific
            </td>
            <td>
                7 lb 7 oz
            </td>
            <td>
                Magnum GM
            </td>
            <td>
                M-Junior
            </td>
            <td class="last-td">
                Approved
            </td>
        </tr>
        <tr class="odd">
            <td>
                <span class="logotxt">
                    storm
                </span>
                <img alt="" style="clear: left;" class="logo small" src="logo-storm.jpg?$staticlink$"
                />
            </td>
            <td>
                12/31/2001
            </td>
            <td>
                Hampl, Maximilian
            </td>
            <td>
                Barracuda, Pacific
            </td>
            <td>
                6 lb 9 oz
            </td>
            <td>
                Magnum 14 GM
            </td>
            <td>
                M-Junior
            </td>
            <td class="last-td">
                Retired
            </td>
        </tr>
        <tr class="even">
            <td>
                <span class="logotxt">
                    sufix
                </span>
                <img alt="" style="clear: left;" class="logo small" src="logo-sufix.jpg?$staticlink$"
                />
            </td>
            <td>
                11/31/1992
            </td>
            <td>
                Hampl, Christopher
            </td>
            <td>
                Barracuda, Pacific
            </td>
            <td>
                7 lb 7 oz
            </td>
            <td>
                Magnum GM
            </td>
            <td>
                M-10 kg (20 lb)
            </td>
            <td class="last-td">
                Approved
            </td>
        </tr>
        <tr class="odd">
            <td>
                <span class="logotxt">
                    luhrjensen
                </span>
                <img alt="" style="clear: left;" class="logo small" src="logo-luhrjensen.jpg?$staticlink$"
                />
            </td>
            <td>
                5/18/2001
            </td>
            <td>
                Hampl, Christopher
            </td>
            <td>
                Skipjack, black
            </td>
            <td>
                5 lb 9 oz
            </td>
            <td>
                Rapala 14 Mackerel
            </td>
            <td>
                M-Junior
            </td>
            <td class="last-td">
                Approved
            </td>
        </tr>
        <tr class="even">
            <td>
                <span class="logotxt">
                    triggerx
                </span>
                <img alt="" style="clear: left;" class="logo small" src="logo-triggerx.jpg?$staticlink$"
                />
            </td>
            <td>
                1/14/1999
            </td>
            <td>
                Toivonen, Ville
            </td>
            <td>
                Zander
            </td>
            <td>
                10 lb 10 oz
            </td>
            <td>
                Shad Rap B
            </td>
            <td>
                06 kg (12 lb)
            </td>
            <td class="last-td">
                Approved
            </td>
        </tr>
        <tr class="odd">
            <td>
                <span class="logotxt">
                    bluefox
                </span>
                <img alt="" style="clear: left;" class="logo small" src="logo-bluefox.jpg?$staticlink$"
                />
            </td>
            <td>
                10/05/2008
            </td>
            <td>
                Montero Sanchez, Luis
            </td>
            <td>
                Machaca (Sabalo pipon)
            </td>
            <td>
                5 lb 0 oz
            </td>
            <td>
                CountDown 44
            </td>
            <td>
                All-Tackle
            </td>
            <td class="last-td">
                Approved
            </td>
        </tr>
        <tr class="even">
            <td>
                <span class="logotxt">
                    terminator
                </span>
                <img alt="" style="clear: left;" class="logo small" src="logo-terminator.jpg?$staticlink$"
                />
            </td>
            <td>
                11/02/2001
            </td>
            <td>
                Arostegui, Martini
            </td>
            <td>
                Gar, alligator
            </td>
            <td>
                21 lb 0 oz
            </td>
            <td>
                Rapala
            </td>
            <td>
                03 kg (6 lb)
            </td>
            <td class="last-td">
                Approved
            </td>
        </tr>
        <tr class="odd">
            <td>
                <span class="logotxt">
                    williamson
                </span>
                <img alt="" style="clear: left;" class="logo small" src="logo-williamson.jpg?$staticlink$"
                />
            </td>
            <td>
                09/16/1886
            </td>
            <td>
                Casal, Xavier
            </td>
            <td>
                Grouper, broomtail
            </td>
            <td>
                24 lb 0 oz
            </td>
            <td>
                X-Rap Magnum 30
            </td>
            <td>
                M-15 kg (30 lb)
            </td>
            <td class="last-td">
                Approved
            </td>
        </tr>
        <tr class="even">
            <td>
                <span class="logotxt">
                    rapala
                </span>
                <img style="clear: left;" class="logo small" src="logo-rapala.jpg?$staticlink$"
                alt="" />
            </td>
            <td>
                09/05/2002
            </td>
            <td>
                Nabozny, Stan
            </td>
            <td>
                Barracuda, Guinean
            </td>
            <td>
                13 lb 12 oz
            </td>
            <td>
                CountDown 18
            </td>
            <td>
                M-02 kg (4 lb)
            </td>
            <td class="last-td">
                Approved
            </td>
        </tr>
        <tr class="odd">
            <td>
                <span class="logotxt">
                    vmc
                </span>
                <img alt="" style="clear: left;" class="logo small" src="logo-vmc.jpg?$staticlink$"
                />
            </td>
            <td>
                07/16/2002
            </td>
            <td>
                Nabozny, Stan
            </td>
            <td>
                Barracuda, Guinean
            </td>
            <td>
                15 lb 7 oz
            </td>
            <td>
                X-Rap
            </td>
            <td>
                M-03 kg (6 lb)
            </td>
            <td class="last-td">
                Approved
            </td>
        </tr>
        <tr class="even">
            <td>
                <span class="logotxt">
                    storm
                </span>
                <img alt="" style="clear: left;" class="logo small" src="logo-storm.jpg?$staticlink$"
                />
            </td>
            <td>
               11/16/2001
            </td>
            <td>
                Nabozny, Stan
            </td>
            <td>
                Barracuda, Guinean
            </td>
            <td>
                17 lb 3 oz
            </td>
            <td>
                CountDown 18
            </td>
            <td>
                M-06 kg (12 lb)
            </td>
            <td class="last-td">
                Approved
            </td>
        </tr>
        <tr class="odd">
            <td>
                <span class="logotxt">
                    sufix
                </span>
                <img alt="" style="clear: left;" class="logo small" src="logo-sufix.jpg?$staticlink$"
                />
            </td>
            <td>
                07/21/2003
            </td>
            <td>
                Nabozny, Stan
            </td>
            <td>
                Barracuda, Guinean
            </td>
            <td>
                15 lb 6 oz
            </td>
            <td>
                CountDown 18
            </td>
            <td>
                M-06 kg (12 lb)
            </td>
            <td class="last-td">
                Retired
            </td>
        </tr>
    </tbody>
</table>

Но это не работает для формата веса. Кто-нибудь может подсказать, как применить этот формат для сортировки?

1 Ответ

2 голосов
/ 13 октября 2011

Вы можете расширить сортировщик таблиц с помощью пользовательского анализатора столбцов.Смотри документы здесь .Вам просто нужно преобразовать текст в десятичную или что-то легко сортируемое.

Вот что я придумала, взломав их на их примере.Я не проверял это, но это должно быть очень близко к тому, что вы хотите.Вот рабочий JSFiddle http://jsfiddle.net/UFgy2/3/

// add parser through the tablesorter addParser method 
$.tablesorter.addParser({ 
    // set a unique id 
    id: 'weights', 
    is: function(s) { 
        // return false so this parser is not auto detected 
        return false; 
    }, 
    //format the column for sorting
    format: function(s) { 
        var array,weight_decimal; 

        //trim s cause it returns all kinds of garbage white spaces 
        array = jQuery.trim(s).split(" ");

        //we should always have lbs even if it's zero
        weight_decimal = parseInt(array[0]);

        //if we have ounces, add it to the lbs
        if(array[2]){
             weight_decimal = weight_decimal + 0.0625*parseInt(array[2]); 
        }

        return weight_decimal; 
    }, 
    // set type, either numeric or text 
    type: 'numeric' 
}); 



//apply your sorter to the column
$(function() { 
    $("#dataTable").tablesorter({ 
        headers: { 
            6: { 
                sorter:'weights' 
            } 
        } 
    }); 
}); 
...