简体   繁体   English

复杂的排序html表

[英]Complex sort html table

I have table: 我有桌子:

<table id="myTable" class="tablesorter">
  <thead>
  <tr>
    <th>name</th>
    <th>secondname</th>
    <th>E-mail</th>
    <th>Salary</th>
    <th>web site</th> 
  </tr>
  </thead>
  <tbody>
  <tr>
    <td>Smit</td>
    <td>Jonson</td>
    <td></td>
    <td>$50.00</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>Smit2</td>
    <td>Jonson2</td>
    <td></td>
    <td>$20.00</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>Bah</td>
    <td>Frank</td>
    <td>fbach@yahoo.com</td>
    <td>$40.00</td>
    <td>no</td>
  </tr>
  <tr>
    <td>Dou</td>
    <td>jenson</td>
    <td></td>
    <td>$100.00</td>
    <td></td>
  </tr>
  <tr>
    <td>Kortni</td>
    <td>Love</td>
    <td></td>
    <td>$50.00</td>
    <td></td>
  </tr>
  </tbody>
</table>

How sort my table that would simultaneously three conditions: 1 condition: E-mail = empty and web site = yes and Salary = ascending; 如何同时对我的表进行排序,这将同时满足以下三个条件:1个条件:电子邮件=空,网站=是,工资=升序; Then go string with 2 condition: E-mail - not empty and web site = no and Salary = ascending; 然后转到2个条件的字符串:电子邮件 - 不为空,网站=否和薪水=升序; Then strings - other. 然后是字符串 - 其他。 Sort with JavaScript(use jQuery). 用JavaScript排序(使用jQuery)。 All data for table come from Spring Controller. 表的所​​有数据都来自Spring Controller。 Rresult: Rresult:

 name   secondname  E-mail       Salary           web site
   Smit2    Jonson2                  $20.00                  yes   
   Smit     Jonson                   $50.00                  yes
    Bah     Frank      fbach@yahoo.com   $40.00                   no
    Bah     Frank      fbach@yahoo.com   $330.00                  no
    Kortni  Love                     $50.00     
    Dou     jenson                   $100.00    

Use data tables - http://datatables.net/ 使用数据表 - http://datatables.net/

Its a great plugin for jQuery and here is a great example of how to do multi-column sorting - http://datatables.net/release-datatables/examples/basic_init/multi_col_sort.html 它是jQuery的一个很棒的插件,这是一个如何进行多列排序的很好的例子 - http://datatables.net/release-datatables/examples/basic_init/multi_col_sort.html

/* Define two custom functions (asc and desc) for string sorting */
jQuery.fn.dataTableExt.oSort['string-case-asc']  = function(x,y) {
    return ((x < y) ? -1 : ((x > y) ?  1 : 0));
};

jQuery.fn.dataTableExt.oSort['string-case-desc'] = function(x,y) {
    return ((x < y) ?  1 : ((x > y) ? -1 : 0));
};

$(document).ready(function() {
    /* Build the DataTable with third column using our custom sort functions */
    $('#example').dataTable( {
        "aaSorting": [ [0,'asc'], [1,'asc'] ],
        "aoColumns": [
            null,
            null,
            { "sType": 'string-case' },
            null,
            null
        ]
    } );
} );

UPDATE UPDATE

Based on your question, you would look for something like this: 根据您的问题,您将寻找这样的东西:

...
    "aaSorting": [ [2,'asc'], [4,'asc'], [3, 'asc' ],
...

Which is Email, Web Site, Salary (zero-based column 2, 4, and 3). 这是电子邮件,网站,薪水(从零开始的第2、4和3列)。 If that doesn't do it perfectly, switch out 'asc' for 'desc' where needed. 如果这样做不完美,请在需要的地方为'desc'切换'asc'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM