简体   繁体   中英

Datatable Uncaught TypeError: Cannot read property 'add' of undefined (asp.net / js)

I'm getting

Uncaught TypeError: Cannot read property 'add' of undefined.

with data tables.

This is my HTML and JS code:

 function agregarFila(data) { let tabla = $('#tablaPedidos').DataTable(); for (var i = 0; i < data.length; i++) { tabla.rows.add([ data[i].productoID, data[i].producto, data[i].cliente, data[i].usuario, data[i].fechaCreacion, data[i].estado ]).draw(); } }
 <div class="box-body table-responsive"> <table id="tablaPedidos" class="table table-bordered table-hover"> <thead> <tr> <th>ID</th> <th>Producto</th> <th>Cliente</th> <th>Usuario</th> <th>Fecha de creacion</th> <th>Estado</th> </tr> </thead> <tbody id="tablaPedidosBody"> </tbody> </table> </div> <script src="js/jquery.min.js"></script> <script src="js/plugins/datatables/jquery.dataTables.js"></script> <script type="text/javascript" src="js/listaPedidos.js"></script>

I have no idea that may be causing this, can anyone help me figure this out?

Judging by https://datatables.net/examples/api/add_row.html you need tabla.row.add(...) instead of tabla.rows.add(...) (notice .row vs .rows ). Also, you should add debugger; statement before the line with add and run this code with browser dev tools open (F12 usually) - you will be able to inspect your variables and check, which methods are available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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