Custom sorting on values in a data-sort attribute with Jquery Datatables

后端 未结 2 1029
北恋
北恋 2020-12-14 01:44

I have to do some custom sorts with Jquery Datatables. I do not want to write custom sort functions for every custom sort.

I want to define a value to sort on and ha

相关标签:
2条回答
  • 2020-12-14 02:09

    I found an easy solution that works for me and does not require too much code or changes to datatables.js.

    It is very similar to the requirements of the question, but not exactly the same.

    Instead of data-sort you can use a HTML comment tag.

    <td data-sort="111123">E 1.111,23</td>

    becomes

    <td><!-- 000000111123 -->E 1.111,23</td>

    By zero-padding an int they will be sorted as a string. The zero-padding makes the sort behave as you'd expect: sorting the integers from high to low.

    Solution works for dates, ints and strings. For dates and ints you can use a scripting language to output them in the way you want (eg: zero-padded, formatted as yyyy-mm-dd).

    0 讨论(0)
  • 2020-12-14 02:22

    You can use data-order attr, for example

    <table class="table table-bordered table-hover">
    <thead>
        <tr>
            <th>Date</th>
            <th>Count</th>
        </tr>
    </thead>
    <tbody>
    <?php
       $count = 0;
       foreach($users as $user) {?>
          <tr>
             <td data-order="<?php echo $count ?>">
                <?php echo $user['createdDate']; ?>
             </td>
             <td>
                <?php echo $user['count']; ?>
             </td>
             </tr>
       <?php
          $count++;
       }?>
       <tr>
          <td data-order="999999999999999999999999999"> <!--always last-->
              Total
          </td>
          <td>
             <?php echo count($users); ?>
          </td>
      </tr>
    

    more information HTML5 data-* attributes

    0 讨论(0)
提交回复
热议问题