简体   繁体   English

REST API(查询请求)数据表的分页

[英]Pagination for REST API (Query request) Datatables

I have a REST Api command that get an MYSQL query inside and perform it so now with this I request a select and put it on table and the table will convert to Datatables. 我有一个REST Api命令,可以在其中获取一个MYSQL查询并执行它,因此现在我请求一个选择并将其放在表上,然后该表将转换为数据表。 The problem start becuase I don't want to get the whole data in once so I should perform the MYSQL query with limit for example 10 record each time, and I need a pagination that work for it and another thing that I need is a search function which search from db instead of dom. 问题开始是因为我不想一次获取全部数据,所以我应该执行MYSQL查询,例如每次限制10条记录,并且我需要进行分页,而我需要做的另一件事是搜索从db而不是dom搜索的函数。

function getnames() {
    var Group = $("#traderGroup").val();
    var Sql = "select NAME,LOGIN,EMAIL from users";
    getNames = {"Sql":Sql};
    $.ajax({
        type: "POST",
        url: "https://test.com/api/db/select",
        data: JSON.stringify(getNames),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        processData: true,
        success: function (data, status, jqXHR) {
            for (var i = 0; i < data.Data.length; i++) {
                $("<tr><td align='center'>" + data.Data[i].LOGIN + "</td><td>" + data.Data[i].NAME + "</td><td>" + data.Data[i].EMAIL + "</td></tr>").appendTo("#ajaxResponse3").hide().fadeIn(500);
            }
        },
        error: function (data, status, jqXHR) {
                $.ajax({
                    type: 'POST',
                    url: apiurl+'Token',
                    data: loginData
                }).done(function(data) {                                        
            });
            $("<div class='alert alert-danger alert-dismissable'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>Something Went Wrong!! Please contact the IT Department.</div>").prependTo("#alert").hide().fadeIn(500);
        },
        beforeSend : function(xhr){
            $('#processing-modal').modal('show');
        },
        complete : function(){
            $('#processing-modal').modal('hide');
            table = $('#tablename').dataTable({
                responsive: true,
                "search": {"smart": true,"regex": true}
            });
            var tableTools = new $.fn.dataTable.TableTools(table, {
                "sButtons": ["copy","csv","xls","pdf",{ "type": "print", "buttonText": "Print me!" } ],
                "sSwfPath": "https://datatables.net/release-datatables/extensions/TableTools/swf/copy_csv_xls_pdf.swf" 
            });
            $(tableTools.fnContainer()).insertAfter('div.tabletools h3');
        }
    });
}

seems as though you're using ajax to populate a table and then calling the datatable rather than getting the table to get its own data via ajax ? 似乎您正在使用ajax填充表,然后调用数据表,而不是通过ajax获取表以获取其自身的数据?

Datatables allow you to use serverside and processing to get all the stuff you, and your table, need... I recently wrote my own serverSide script and it's surprisingly easy! 数据表允许您使用服务器端处理来获取您和表所需的所有内容。我最近编写了自己的serverSide脚本 ,这非常简单! There's another example on the site as well and it's a brilliant starting point. 该站点上还有另一个示例 ,这是一个绝佳的起点。

Not really an answer to your question I'm afraid excepting to say that if you want to get there you should perhaps look at setting off from somewhere else... 恐怕这并不是一个真正的答案,除了说如果您想到达那里,您也许应该考虑从其他地方出发...

Dom 多姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM