【问题标题】:Server-side processing default order服务器端处理默认顺序
【发布时间】:2019-02-06 01:42:13
【问题描述】:

我想将我的数据表的默认顺序设置为 desc。

我试图查看 $request["order"][0]['dir'] 的值,它总是按升序排列。有没有办法将排序设置为降序?我在下面添加了我的 JS。

 $sqlRecord .= " ORDER BY ". $columns[$request['order'][0]['column']] ." " . $request["order"][0]['dir'] . " LIMIT " . $request["start"] . " ," . $request["length"]. " ";

$.ajax({
    url: "coordinator-activities-table.php",
    method: "POST",
    success: function(data){
        $("#retailer-activities-container").html(data);

        table = [
            { "width": "120px", "orderable": false, "targets": 0 },
            { "width": "80px", "targets": 1 },
            { "width": "150px", "targets": 2 },
            { "width": "120px", "targets": 3 },
            { "width": "150px", "targets": 4 },
            { "width": "150px", "targets": 5 },
            { "width": "150px", "targets": 6 },
            { "width": "150px", "targets": 7 },   
            { "width": "150px", "targets": 8 },
            { "width": "150px", "targets": 9 },
            { "width": "120px", "targets": 10 },
            { "width": "100px", "targets": 11 },
            { "width": "110px", "targets": 12 },
            { "width": "110px", "targets": 13 },
            { "width": "150px", "targets": 14 },
            { "width": "150px", "targets": 15 },
            { "width": "120px", "targets": 16 },
            { "width": "150px", "orderable": false, "targets": 17 }
        ];

        var table = $('#activities-table').DataTable({
            "searching": { "regex": true },
            "paging": true,
            "autoWidth": false,
            "processing": true,
            "serverSide": true,
            "ajax": {
                url: "coordinator-activities-data.php",
                type: "POST",
                "dataType": "json",
                data: {coordinator:coordinator, startdate:startdate, enddate:enddate, regional:regional},
                "complete": function(response) {
                }
            },
            "columnDefs": table,
            "language": {
                "emptyTable": "No data available in table",
                "zeroRecords": "No data available in table",
                "info": "Showing <b>_START_</b> to <b>_END_ of _TOTAL_</b> entries",
                "paginate": {
                    "first":      "First",
                    "last":       "Last",
                    "next":       "Next",
                    "previous":   "Previous"
                },
                search: "_INPUT_",
                searchPlaceholder: "Search..."
            },
            dom: 'Bfrtip',
            buttons: [
                'csv', 'excel', 'pdf'
            ]
        });
    },
    error: function(data){
        console.log("error");
    }
});

我想将默认排序设置为降序

【问题讨论】:

  • ORDER BY ... DESC,至少在 MySql 中。
  • @Quasimodo'sclone 加载数据表时顺序应该是降序但我仍然可以点击排序
  • @Quasimodo'sclone 我正在使用数据表服务器端处理
  • 发送请求的代码是什么?
  • @Quasimodo'sclone 我想在获得 $request["order" 的值时立即将 $request["order"][0]['dir'] 的值更改为 Descending ][0]['dir'] 值为“ASC”

标签: php datatable datatables


【解决方案1】:

当数据表向服务器发出请求时,您可以更改排序数据:

        var firstsort='DESC';

                var table = $('#activities-table').DataTable({
                    "searching": { "regex": true },
                    "paging": true,
                    "autoWidth": false,
                    "processing": true,
                    "serverSide": true,
                    "ajax": {
                        url: "coordinator-activities-data.php",
                        type: "POST",
                        "dataType": "json",
                        data: function (data) {

                            var sort = data.order[0].column;
                            //check condition or remove condition if you want every request with sort DESC
                            if (firstsort != '') {
                                data.order[0].dir = "DESC";
                                firstsort = '';
                            }
                            data['coordinator'] = coordinator, data['regional'] = regional,
                                data['startdate'] = startdate, data['enddate'] = enddate;

                            return data;
                        },
                        "complete": function (response) {
                        }
                    },
                    "columnDefs": table,
                    "language": {
                        "emptyTable": "No data available in table",
                        "zeroRecords": "No data available in table",
                        "info": "Showing <b>_START_</b> to <b>_END_ of _TOTAL_</b> entries",
                        "paginate": {
                            "first": "First",
                            "last": "Last",
                            "next": "Next",
                            "previous": "Previous"
                        },
                        search: "_INPUT_",
                        searchPlaceholder: "Search..."
                    },
                    dom: 'Bfrtip',
                    buttons: [
                        'csv', 'excel', 'pdf'
                    ]
                });

【讨论】:

  • 它对我不起作用。有没有办法在 PHP 中编辑它?
  • 我加了请看
  • @LawrenceAgulto 试试这个,我只更改了你的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多