【问题标题】:put datatables to two tables in the same page将数据表放在同一页面中的两个表中
【发布时间】:2019-07-25 16:24:00
【问题描述】:

我有两个用 javascrips AJAX 填充的表,问题是我想要两个数据表,它们在同一个页面上。

表格

<table id="Table01" class="table table-hover display">
    <thead>
        <tr id="tr_tablita">
            <th>#</th>
            <th>Código</th>
            <th>Producto</th>
            <th>Precio</th>
            <th>Cantidad</th>
            <th>Total</th>
            <th>Movimientos</th>
        </tr>
    </thead>
    <tbody id="tabla_get_articulos_venta">

    </tbody>
</table>

<table id="table02" class="table table-hover display">
    <thead>
        <tr>
            <th>#</th>
            <th>Código</th>
            <th>Producto</th>
            <th>Precio</th>
            <th>Proveedor</th>
            <th>Movimientos</th>
        </tr>
    </thead>
    <tbody id="tabla_get_articulos">

    </tbody>
</table>

我填表

var id_venta = <?php echo $id_venta;?>; // traigo el id de la venta para poder editar la venta
var contador = 1;

$(document).ready(function(){        
    get_articulos_venta(id_venta);
    get_articulos();  

    // $('table.display').DataTable();
    var allTables = $('table.display').DataTable();
});

function get_articulos() {

    $.ajax({
        url : "<?php echo base_url('Ventas_controller/get_articulos')?>",
        type: "POST",
        dataType: "JSON",
        success: function(data) {

            var html = '';
            var contador_art = 1;

            var filas = data.length;
            for (  i = 0 ; i < filas; i++){ //cuenta la cantidad de registros
                html += "<tr><td>" +
                contador_art + "</td><td>" +
                data[i].codigo + "</td><td>" +
                data[i].descripcion + "</td><td>" +
                "$ "+data[i].precioCompra + "</td><td>" +
                data[i].proveedor + "</td><td>"+
                "<a type='button' onclick='add_articulo_venta("+data[i].ideArticulo+")' class='btn btn-success'>" +
                    "<i class='fa fa-plus' style='color:#fff;'></i>" +
                "</a>" +"</td><tr>" ;

                contador_art++;
            }
            $('#tabla_get_articulos').html(html);  

        },
        error: function (jqXHR, textStatus, errorThrown) {
            swal({
              title: "Error!",
              text: "No trajeron los artículos del sistema!",
              icon: "error",
              button: true,
              dangerMode: false,
            })
            .then((willDelete) => {
              if (willDelete) {

              }
            });
        }
    });
}

function get_articulos_venta(id_venta) {

    $.ajax({
        url : "<?php echo base_url('Ventas_controller/get_articulos_venta')?>/"+id_venta,
        type: "POST",
        dataType: "JSON",
        success: function(data) {

            var html = '';
            var filas = data.length;

            for (  i = 0 ; i < filas; i++){ //cuenta la cantidad de registros
                html += "<tr><td>" +
                contador + "</td><td>" +
                data[i].codigo + "</td><td>" +
                data[i].descripcion + "</td><td>" +
                "$ "+data[i].precioCompra + "</td><td>" +
                data[i].cantidad + "</td><td>"+
                data[i].preciototal + "</td><td>"+
                "<a type='button' onclick='delete_articulo_venta("+data[i].ideDetalleVenta+")' class='btn btn-danger'>" +
                    "<i class='fa fa-times' style='color:#fff;'></i>" +
                "</a> " +
                "<a type='button' onclick='get_info_articulo_venta("+data[i].ideDetalleVenta+")'  class='btn btn-warning'>" +
                    "<i class='fa fa-pencil' style='color:#fff;'></i>" +
                "</a>" +"</td></tr>" ;

                contador++;
            }
            $('#tabla_get_articulos_venta').html(html); 
        },
        error: function (jqXHR, textStatus, errorThrown) {
            swal({
              title: "Error!",
              text: "No trajeron los artículos de la venta!",
              icon: "error",
              button: true,
              dangerMode: false,
            })
            .then((willDelete) => {
              if (willDelete) {

              }
            });
        }
    });
}

get_articulos_venta(parameter); 填写第一张表

get_articulos(); 填满第二张表

我需要将数据表应用到两个表

试试这个选项

https://datatables.net/examples/basic_init/multiple_tables.html

这就是结果

应用设计数据表,但它们看起来好像是空的表。

【问题讨论】:

  • 初始化每个DataTable之后你插入所有的数据html。当你在插件初始化后添加所有的 html ......插件对此一无所知
  • 你能举个例子吗?
  • @Javierfr 从您的屏幕截图看来,您的两个表都不是空的。我只看到了您的顶部导航布局,涵盖了第一个表格标题和搜索。你说的是哪个区域“清空桌子”?

标签: javascript ajax codeigniter datatables


【解决方案1】:

在你的 ajax 调用的成功函数中,在 for 循环构建表之后,初始化数据表。

function get_articulos_venta(id_venta) {

$.ajax({
    url : "<?php echo base_url('Ventas_controller/get_articulos_venta')?>/"+id_venta,
    type: "POST",
    dataType: "JSON",
    success: function(data) {

        var html = '';
        var filas = data.length;

        for (  i = 0 ; i < filas; i++){ //cuenta la cantidad de registros
            html += "<tr><td>" +
            contador + "</td><td>" +
            data[i].codigo + "</td><td>" +
            data[i].descripcion + "</td><td>" +
            "$ "+data[i].precioCompra + "</td><td>" +
            data[i].cantidad + "</td><td>"+
            data[i].preciototal + "</td><td>"+
            "<a type='button' onclick='delete_articulo_venta("+data[i].ideDetalleVenta+")' class='btn btn-danger'>" +
                "<i class='fa fa-times' style='color:#fff;'></i>" +
            "</a> " +
            "<a type='button' onclick='get_info_articulo_venta("+data[i].ideDetalleVenta+")'  class='btn btn-warning'>" +
                "<i class='fa fa-pencil' style='color:#fff;'></i>" +
            "</a>" +"</td></tr>" ;

            contador++;
        }
        $('#tabla_get_articulos_venta').html(html);
        $('#tabla_get_articulos_venta').DataTable({
        select: true,
        searching: true
    });


    },
    error: function (jqXHR, textStatus, errorThrown) {
        swal({
          title: "Error!",
          text: "No trajeron los artículos de la venta!",
          icon: "error",
          button: true,
          dangerMode: false,
        })
        .then((willDelete) => {
          if (willDelete) {

          }
        });
    }
});

}

【讨论】:

  • 是的。每个数据表都应该在构建好html结构后进行初始化。
  • 也许仔细检查以确保 1. 表具有唯一的 ID 和 2. 代码相应地引用每个表。也尝试在每个数据表初始化后在行上添加debugger。按 F12 并刷新页面。每次遇到断点时,使用 f8 继续。这可能更容易评估问题。
猜你喜欢
  • 1970-01-01
  • 2012-12-13
  • 2018-03-11
  • 1970-01-01
  • 2014-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-29
相关资源
最近更新 更多