如何动态生成Datatable Jquery

时间:2017-10-30 22:17:07

标签: php jquery json ajax datatables

我需要使用 DataTable Jquery 动态创建表,该表必须是 服务器端处理 类型并且在同一请求中( Ajax)并回答了列,结果显然是结果。

DatosColumna=""//Json with Response of ajax

$('#table_query_info').dataTable({
    columnDefs: DatosColumna,//Here its the headers
    destroy: true,
    processing: true,
    serverSide: true,
    ajax: {
        url: myBaseUrl + "consults/find_docs",
        type: "post",
        data: function (d) {
            d.datos = make_info()//Send Json with info
        },
        statusCode: {
            200: function (resp) {
                alert("OK");//That Good
            }
        }
    }
});

我尝试使用此代码,但它没有工作

非常感谢

1 个答案:

答案 0 :(得分:0)

解决步骤:

  1. 制作一个html代码

<table class="table" id="tablaindex" style="width:100%"></table>

  1. 拉美布拉河沿岸的Obtengo las columnas

    $.ajax({ type: "POST", url: "AtencionTurno/ColumnasDatatable", data: TblOficina, success: function (response) { inicializarTabla(response); }, error: function (request, status, error) { console.log("Error"); } });

  2. 要求获得以下json

    [ { "title": "Column 1", "data": "Id_Turno", "orderable": false }, { "title": "Column 2", "data": "Turno", "orderable": false }, { "title": "Column 3", "data": "Clasificacion", "orderable": false }, { "title": "Column 4", "data": "Concepto", "orderable": false }, { "title": "Column 5", "data": "Hora", "orderable": false }]

  3. 现在我用获得的列初始化表

    inicializarTabla = function (columnas) { var dataTable = $('#tablaindex').DataTable({ "processing": true, "serverSide": true, stateSave: true, orderable: false, "order": [], columns: columnas, "lengthChange": false, searching: false, colReorder: false, "ajax": { url: "AtencionTurno/IndexTurnos", type: "post", data: TblOficina } }); };

  4. 来自服务器的响应:

    { "draw": "1", "recordsTotal": 4, "recordsFiltered": 4, "data": [ { "Id_Turno": "358", "Turno": "ALAT1", "Clasificacion": "euismod", "Concepto": "Convallis", "Hora": "11:00:12", "Tiempo_Espera": "2018/07/03 11:00:12", "Tipo_Cliente": "Calendario", "Transferencia": "", "null": "358" }, { "Id_Turno": "354", "Turno": "ALAK23", "Clasificacion": "euismod", "Concepto": "Pagos", "Hora": "16:36:14", "Tiempo_Espera": "2018/06/29 16:36:14", "Tipo_Cliente": "Utiles Escolares ", "Transferencia": "", "null": "354" } ] }