¿Error no capturado al obtener valores de un datatable?

¿Error no capturado al obtener valores de un datatable?

DUMDUM Posts: 4Questions: 1Answers: 0

Tengo una consulta y es la siguiente:
Estoy trabajando con datatables y la cuestión es que yo cargo un excel y obtengo sus datos en json para con esos datos llenarlo en un datatable hasta aquí no hay ningún problema, el datatable tiene un checkbox para seleccionar los datos que quieres obtener.Cuando la primera vez que lleno el datatable desde un excel cargado y selecciono los checkbox todo funciona bien, pero cuando selecciono otro excel y se llena el datatable y selecciono el checkbox me bota el error de no capturado

Aquí dejo mi código

        $dt.on('change', 'tbody input', function () {
            var info = table.row($(this).closest('tr')).data();
            info.checked = this.checked;

            /**  NUEVO CÓDIGO */
            /**  Verificar si todos están seleccionados */
            var countSelected = 0
            var countTotal = 0
            table.data().each(function (info) {
                countTotal += 1;
                if (info.checked) {
                    countSelected += 1;
                }
            });

            /** Actualizamos UI según caso */
            if (countSelected === countTotal) {
                $('thead input').prop("checked", true);
                $('thead input').prop("indeterminate", false);
            } else if (countSelected === 0) {
                $('thead input').prop("checked", false);
                $('thead input').prop("indeterminate", false);
            } else {
                $('thead input').prop("checked", false);
                $('thead input').prop("indeterminate", true);
            }
            /** END NUEVO CÓDIGO */

        });

Answers

  • DUMDUM Posts: 4Questions: 1Answers: 0

    Me he percatado que cuando cargo el datatable por ejemplo por primera vez con 3 registros funciona bien los checkbox, pero cuando realizo una segunda carga al datatable con 8 registros los 3 primeros registros funcionan bien con el checkbox y a partir del cuarto registro me sale el error no capturado.

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • DUMDUM Posts: 4Questions: 1Answers: 0
  • DUMDUM Posts: 4Questions: 1Answers: 0

    Primero ingresa "json1" al input y click en "ejecutar json" y selecciona cualquier checkbox y das al botón "obtener" y mostrará en consola los datos seleccionados. Luego ingresa al input "json2" y selecciona el checkbox del 4to registro y da click al botón "obtener" y te mostrará el error.

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    It's because you're using the old event handlers - when you destroy and recreate the table, you're adding additional event handlers leaving the old handlers with the old table size still.

    Adding $dt.off('change'); does the trick, see here.

    Colin

This discussion has been closed.