Sorting Image and hyperlink columns in a table using JQuery Sorter plugin

狂风中的少年 提交于 2019-12-02 04:05:18

It looks like you'll need to use a combination of a specialized parser and textExtraction. Check out this demo which uses the following code:

// add parser through the tablesorter addParser method 
// running =>1 stopped =>2 error =>3
$.tablesorter.addParser({
    // set a unique id 
    id: 'status',
    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(/running.png/, 1)
            .replace(/stopped.png/, 2)
            .replace(/error.png/, 3);
    },
    // set type, either numeric or text 
    type: 'numeric'
});

$('table').tablesorter({

    headers: {
        1: { sorter: 'status' }
    },

    textExtraction: function(node) {
        var $n = $(node).children();
        return ($n[0].nodeName === "IMG") ? $n.attr('src') : $n.text();    
    }


});​

I know this is maybe a bit quick and dirty but I have a generated page serverside. Checking e.g. (if field('reg') = 1, then display ok.png, if not display not_ok.png)

So I write the number by the image and it sorts by the number. I use a style to make the number 1px so it does not show.

css: .mini{size:1px}`

html:

<td><img src="img/ok.png" alt="ok" /><span class="mini">1</span></td>
<td><img src="img/not_ok.png" alt="ok" /><span class="mini">0</span></td>

Sorting is ok and I did not have to fiddle with the JS. You could use any number to sort by.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!