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
2.4k views
in Technique[技术] by (71.8m points)

jquery - How to avoid pagination reset while sorting datatables

I am having a simple jquery-datatable which allows basic sorting and pagination functionality. Consider below data-tables snippet

$(document).ready(function() {
  $('#example').dataTable({
    "sPaginationType": "full_numbers"
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/jquery.dataTables.min.js"></script>

<link href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/css/jquery.dataTables.css" rel="stylesheet" />

<table cellpadding="0" cellspacing="0" border="0" class="dataTable" id="example">
  <thead>
    <tr>
      <th>Rendering engine</th>
      <th>Browser</th>
      <th>Platform(s)</th>
      <th>Engine version</th>
      <th>CSS grade</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Trident</td>
      <td>Internet Explorer 4.0</td>
      <td>Win 95+</td>
      <td> 4</td>
      <td>X</td>
    </tr>
    <tr>
      <td>Trident</td>
      <td>Internet Explorer 5.0</td>
      <td>Win 95+</td>
      <td>5</td>
      <td>C</td>
    </tr>
    <tr>
      <td>Trident</td>
      <td>Internet Explorer 5.5</td>
      <td>Win 95+</td>
      <td>5.5</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Trident</td>
      <td>Internet Explorer 6</td>
      <td>Win 98+</td>
      <td>6</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Trident</td>
      <td>Internet Explorer 7</td>
      <td>Win XP SP2+</td>
      <td>7</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Trident</td>
      <td>AOL browser (AOL desktop)</td>
      <td>Win XP</td>
      <td>6</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Firefox 1.0</td>
      <td>Win 98+ / OSX.2+</td>
      <td>1.7</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Firefox 1.5</td>
      <td>Win 98+ / OSX.2+</td>
      <td>1.8</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Firefox 2.0</td>
      <td>Win 98+ / OSX.2+</td>
      <td>1.8</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Firefox 3.0</td>
      <td>Win 2k+ / OSX.3+</td>
      <td>1.9</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Camino 1.0</td>
      <td>OSX.2+</td>
      <td>1.8</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Camino 1.5</td>
      <td>OSX.3+</td>
      <td>1.8</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Netscape 7.2</td>
      <td>Win 95+ / Mac OS 8.6-9.2</td>
      <td>1.7</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Netscape Browser 8</td>
      <td>Win 98SE+</td>
      <td>1.7</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Netscape Navigator 9</td>
      <td>Win 98+ / OSX.2+</td>
      <td>1.8</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Mozilla 1.0</td>
      <td>Win 95+ / OSX.1+</td>
      <td>1</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Mozilla 1.1</td>
      <td>Win 95+ / OSX.1+</td>
      <td>1.1</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Mozilla 1.2</td>
      <td>Win 95+ / OSX.1+</td>
      <td>1.2</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Mozilla 1.3</td>
      <td>Win 95+ / OSX.1+</td>
      <td>1.3</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Mozilla 1.4</td>
      <td>Win 95+ / OSX.1+</td>
      <td>1.4</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Mozilla 1.5</td>
      <td>Win 95+ / OSX.1+</td>
      <td>1.5</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Mozilla 1.6</td>
      <td>Win 95+ / OSX.1+</td>
      <td>1.6</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Mozilla 1.7</td>
      <td>Win 98+ / OSX.1+</td>
      <td>1.7</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Mozilla 1.8</td>
      <td>Win 98+ / OSX.1+</td>
      <td>1.8</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Seamonkey 1.1</td>
      <td>Win 98+ / OSX.2+</td>
      <td>1.8</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Gecko</td>
      <td>Epiphany 2.20</td>
      <td>Gnome</td>
      <td>1.8</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Webkit</td>
      <td>Safari 1.2</td>
      <td>OSX.3</td>
      <td>125.5</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Webkit</td>
      <td>Safari 1.3</td>
      <td>OSX.3</td>
      <td>312.8</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Webkit</td>
      <td>Safari 2.0</td>
      <td>OSX.4+</td>
      <td>419.3</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Webkit</td>
      <td>Safari 3.0</td>
      <td>OSX.4+</td>
      <td>522.1</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Webkit</td>
      <td>OmniWeb 5.5</td>
      <td>OSX.4+</td>
      <td>420</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Webkit</td>
      <td>iPod Touch / iPhone</td>
      <td>iPod</td>
      <td>420.1</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Webkit</td>
      <td>S60</td>
      <td>S60</td>
      <td>413</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Presto</td>
      <td>Opera 7.0</td>
      <td>Win 95+ / OSX.1+</td>
      <td>-</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Presto</td>
      <td>Opera 7.5</td>
      <td>Win 95+ / OSX.2+</td>
      <td>-</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Presto</td>
      <td>Opera 8.0</td>
      <td>Win 95+ / OSX.2+</td>
      <td>-</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Presto</td>
      <td>Opera 8.5</td>
      <td>Win 95+ / OSX.2+</td>
      <td>-</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Presto</td>
      <td>Opera 9.0</td>
      <td>Win 95+ / OSX.3+</td>
      <td>-</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Presto</td>
      <td>Opera 9.2</td>
      <td>Win 88+ / OSX.3+</td>
      <td>-</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Presto</td>
      <td>Opera 9.5</td>
      <td>Win 88+ / OSX.3+</td>
      <td>-</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Presto</td>
      <td>Opera for Wii</td>
      <td>Wii</td>
      <td>-</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Presto</td>
      <td>Nokia N800</td>
      <td>N800</td>
      <td>-</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Presto</td>
      <td>Nintendo DS browser</td>
      <td>Nintendo DS</td>
      <td>8.5</td>
      <td>C/A<sup>1</sup></td>
    </tr>
    <tr>
      <td>KHTML</td>
      <td>Konqureror 3.1</td>
      <td>KDE 3.1</td>
      <td>3.1</td>
      <td>C</td>
    </tr>
    <tr>
      <td>KHTML</td>
      <td>Konqureror 3.3</td>
      <td>KDE 3.3</td>
      <td>3.3</td>
      <td>A</td>
    </tr>
    <tr>
      <td>KHTML</td>
      <td>Konqureror 3.5</td>
      <td>KDE 3.5</td>
      <td>3.5</td>
      <td>A</td>
    </tr>
    <tr>
      <td>Tasman</td>
      <td>Internet Explorer 4.5</td>
      <td>Mac OS 8-9</td>
      <td>-</td>
      <td>X</td>
    </tr>
    <tr>
      <td>Tasman</td>
      <td>Internet Explorer 5.1</td>
      <td>Mac OS 7.6-9</td>
      <td>1</td>
      <td>C</td>
    </tr>
    <tr>
      <td>Tasman</td>
      <td>Internet Explorer 5.2</td>
      <td>Mac OS 8-X</td>
      <td>1</td>
      <td>C</td>
    </tr>
    <tr>
      <td>Misc</td>
      <td>NetFront 3.1</td>
      <td>Embedded devices</td>
      <td>-</td>
      <td>C</td>
    </tr>
    <tr>
      <td>Misc</td>
      <td>NetFront 3.

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

1 Reply

0 votes
by (71.8m points)

Based on the communication on the thread:- https://datatables.net/forums/discussion/21001/how-to-keep-current-page-after-sorting

The findings are:-

If you mean the click on the header to trigger sort, then yes, that will always jump you back to the first page, there is no way around that without modifying DataTables, or applying your own sort / click listener to the header elements.

So basically it's not possible by-default(means datatables doesn't provide this feature).

You can raise a question to datatables Site and ask them to do this and added it as an update to there existing library.

May be they will do it.


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

...