首页 > 解决方案 > 如何在响应ajax jquery后创建数据表

问题描述

我有一个问题,我想在用 ajax jquery 发送数据后在这里调用数据表,在这里我很难,因为我将创建的数据表是服务器端的,因为服务器端调用脚本是成功的并且如果 console.log (response) 它将根据数据表的格式输出数据,如下所示

draw: null
recordsFiltered: 9
recordsTotal: 9
data:(9) [{..}] (and other)

这是我发送数据的代码

var urlajax = "<?= $this->url->get($this->router->getControllerName() . '/ajaxbynip')?>";
    $("#btn-filter").click(function(){
        var data = $('#form-filter').serialize();
        $.ajax({
            url:urlajax,
            type:"get",
            data:data,
            success:function(response){
                console.log(response);
                //how can i create datatables server-side in here?
            }
        });
    });

我尝试使用此代码,但它不起作用

tabel = $('#ats-dt-basic-datatbles').DataTable({
                stateSave: true,
                "processing": true,
                "serverSide": true,
                "ordering": true,
                "order": [[ 0, 'asc' ]],
                "iDisplayLength": 50,
                "ajax":
                {
                    "url": urlajax,
                    "type": "POST"
                },
                "deferRender": true,
                "aLengthMenu": [[5, 10, 50],[ 5, 10, 50]],
                "columns": [
                    { "data": "no" },
                    { "data": "name" },
                    { "data": "PERAN" },
                    { "data": "action" },
                ],
            });

标签: javascriptjquerydatatabledatatablesjquery-plugins

解决方案


    $("#btn-filter").click(function(){
    var data = $('#form-filter').serialize();
    $.ajax({
        url:urlajax,
        type:"get",
        data:data,
        success:function(response){
            console.log(response);
            //you can try this one --> call a function or url with ajax 
            // ==>url can be a php file like createDB.php or any function url
            // write code in specific url or php file that call from ajax url for create a database according to your passing data
            
            $.ajax({
              url: url,
              type:"POST",
              data:response,
              success:function(response){
                 console.log(response);
          
              }
            });
         }
    });
});

推荐阅读