使用jQuery DataTables恢复原始排序顺序 [英] Restore original sort order with jQuery DataTables

查看:127
本文介绍了使用jQuery DataTables恢复原始排序顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I retrieve data from database already sorted, at times I need to reset back to the initial load order. I have tried this fnSortNeutral which doesn't work with the new API DataTables 1.10.

然后我尝试了这个

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

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

Not sure what this does, as it resets something but definitely not back to original load order, instead a sort order of its own

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

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

I can't use this as the sort column does not exsist on Datatable to be able to sort using it, sort is applied on database query

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

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

How does one get data table to show the original load order retrieved from database on click?

根据要求,这基本上是可以正常工作的基本普通代码,我可以找到很长的路要添加另一列进行排序,但是如果可以轻松地在数据表中重置顺序,我觉得添加纯粹用于排序的其他字段变得过于复杂.这不可能吗?

As requested this is basically the basic normal code that works, I could find a long way round to add another column for sorting but feel I am overcomplicating adding another field purely for sorting if order can be easily reset in datatables. Is this not possible?

                      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);

                            }
                      });

推荐答案

您是否尝试过将订单"放置在绘制"之后?喜欢.

Have you tried placing the 'order' after the 'draw'? like.

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

就我而言,我只是使用

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

它有效

这篇关于使用jQuery DataTables恢复原始排序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆