Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

javascript - Set a default sort column in Sortable

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

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

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...