How do I sort a column that contains two divs with jQuery Tablesorter?

穿精又带淫゛_ 提交于 2019-11-30 15:47:37

You could use the textExtraction callback:

$(document).ready(function() { 

    // call the tablesorter plugin 
    $("table").tablesorter({ 
        // define a custom text extraction function 
        textExtraction: function(node) { 
          // check you're at the right column
          if ($(node).find('.green').length == 1) {
            // extract data from markup and return it  
            return $(node).find('div:not(.hidden)').find('span').text();;
          }
          else {
            return $(node).text();
          }
        } 
    }); 
}); 

I haven't tested that but it should work in theory

you'll want to look into creating a custom parser. its pretty simple, you just specify how to interpret the value in the cell by passing in a function to the format field.

A generic way to sort any type of column, no matter how the content is complex: add 'sorted' attribute to the , fill 'sorted' value in the server side according to the preffered order, (which is easier than in javascript).

e.g.

<td sorter='1'> 
    <div> 
        <span class="green">Yes</span> <a href="##">(unverify)</a> 
    </div> 
    <div class="hidden"> 
        <span class="red">No<>/a> <a href="##">(verify)</a> 
    </div> 
</td>
<td sorter='2'> 
    <div  class="hidden"> > 
        <span class="green">Yes</span> <a href="##">(unverify)</a> 
    </div> 
    <div>
        <span class="red">No<>/a> <a href="##">(verify)</a> 
    </div> 
</td>

then add the following code in $(function ()

$("table.tablesorter").tablesorter({
   textExtraction: function(node){                    
           return $(node).attr("sorter");                
   }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!