Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Using Sortable, a jquery plugin, I would like to choose a default column for sorting on each load.

For example, sort by the column which has the class name ".column5"

How can I do this using the sortable jquery plugin?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
308 views
Welcome To Ask or Share your Answers For Others

1 Answer

According to the documentation, you can do this on page load.

$(document).ready(function(){
  $("th.sort").each(function(){
    sorttable.innerSortFunction.apply(this, []);
  })
})
th, td {
  padding: 3px
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://www.kryogenix.org/code/browser/sorttable/sorttable.js"></script>


<table class="sortable">
  <thead>
    <tr>
      <th>Name</th>
      <th class="sort">Salary</th>
      <th>Extension</th>
      <th>Start date</th>
      <th>Start date (American)</th>
    </tr>
  </thead>
  <tbody>
    <tr>
        <td>Bloggs, Fred</td>
        <td>$12000.00</td>
        <td>1353</td>
        <td>18/08/2003</td>
        <td>08/18/2003</td>
    </tr>
    <tr>
        <td>Turvey, Kevin</td>
        <td>$191200.00</td>
        <td>2342</td>
        <td>02/05/1979</td>
        <td>05/02/1979</td>
    </tr>
    <tr>
        <td>Mbogo, Arnold</td>
        <td>$32010.12</td>
        <td>2755</td>
        <td>09/08/1998</td>
        <td>08/09/1998</td>
    </tr>
    <tr>
        <td>Shakespeare, Bill</td>
        <td>$122000.00</td>
        <td>3211</td>
        <td>12/11/1961</td>
        <td>11/12/1961</td>
    </tr>
    <tr>
        <td>Shakespeare, Hamnet</td>
        <td>$9000</td>
        <td>9005</td>
        <td>01/01/2002</td>
        <td>01/01/2002</td>
    </tr>
    <tr>
        <td>Fitz, Marvin</td>
        <td>$3300</td>
        <td>5554</td>
        <td>22/05/1995</td>
        <td>05/22/1995</td>
    </tr>
  </tbody>
</table>

<pre><code>
$(document).ready(function(){
  $("th.sort").each(function(){
    sorttable.innerSortFunction.apply(this, []);
  })
})
</code></pre>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...