使用 jQuery DataTables 恢复原始排序顺序

2024-02-27

我从已经排序的数据库中检索数据,有时我需要重置回初始加载顺序。我已经尝试过这个fnSortNeutral它不适用于新的 API DataTables 1.10。

然后我尝试过这个

https://datatables.net/forums/discussion/26430/fnsortneutral-fails-with-new-api https://datatables.net/forums/discussion/26430/fnsortneutral-fails-with-new-api

不确定这会做什么,因为它会重置某些内容,但绝对不会回到原始加载顺序,而是它自己的排序顺序

$('#removesort').click( function() {
    table.order.neutral().draw();
});

我无法使用它,因为数据表上不存在排序列,无法使用它进行排序,排序应用于数据库查询

table
    .order( [[ 1, 'asc' ], [ 2, 'asc' ]] )
    .draw();

如何获取数据表以显示单击时从数据库检索到的原始加载顺序?

根据要求,这基本上是有效的基本正常代码,我可以找到很长的路来添加另一列进行排序,但感觉如果可以在数据表中轻松重置顺序,则添加纯粹用于排序的另一个字段过于复杂。这不可能吗?

                      var table = $('#example').DataTable( {    
                            order: [],            
                            "columnDefs": [ { "targets": 0, "orderable": false } ],                                    
                            "sAjaxSource": "external/load",
                            "oLanguage": {
                               "sLoadingRecords": "",
                               "sEmptyTable": "No data found"
                            },                                                     
                            "fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
                                  if($('#order'))
                                  {
                                    var order = $('#order').val();
                                    aoData.push( { "name": "order", "value": order } );
                                  }
                                  setTimeout(function() {
                                              oSettings.jqXHR = $.ajax( {
                                                       "dataType": 'json',
                                                       "type": "POST",
                                                       "url": sSource,
                                                       "data": aoData,
                                              "timeout": 15000,                         
                                              "success": fnCallback,
                                              "complete" : function(){

                                              },
                                              "error": function (e) {
                                                  console.log(e.message);
                                              }                  
                                        });
                                  },1000);

                            }
                      });

您是否尝试过在“抽奖”之后下“订单”?喜欢。

.draw();
.order( [[ 1, 'asc' ], [ 2, 'asc' ]] )

就我而言,我只是使用

.draw().order( [[ 0, 'desc' ]] );

它有效

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 jQuery DataTables 恢复原始排序顺序 的相关文章

随机推荐