Error going through table html with jquery

Asked

Viewed 65 times

0

I have a table HTML and I need to take the values of value='1' data-chkIdProcessamentoDiario='1' data-chkIdRegistro='1' and assign in an array, but the problem is that apparently the each does not travel table html, although the browser log shows otherwise:

inserir a descrição da imagem aqui

Boon:

<input class="btn btn-primary downloadPDF" value="Download PDF" type="button" id="BbtnDownloadPDF" />

Button event:

$(document).on('click', '.downloadPDF', function (e) {
                    e.preventDefault();    
                    var dados = [];
                    var i = 0;
                    var table = $('.grid > tbody');

                    table.find('tr').each(function () {

                        var _idprocessamentodiario = $('.chkProcessamentoDiario').data('chkidprocessamentodiario');
                        var _idregistro = $('.chkProcessamentoDiario').data('chkidregistro');
                        var _idservico = $('.download').data('idservico');

                        dados[i] = new Object();
                        dados[i]['IdProcessamentoDiario'] = _idprocessamentodiario;
                        dados[i]['IdRegistro'] = _idregistro;
                        dados[i]['IdServico'] = _idservico;
                        console.log(i + ' - _idprocessamentodiario: ' + dados[i].IdProcessamentoDiario + '| _idregistro: ' + dados[i].IdRegistro + '| idservico: ' + dados[i].IdServico);

                        i++;
                    });

                });

This is the HTML table:

<table class="grid">
    <thead>
        <tr class="header">
            <th scope="col">
            </th>
            <th scope="col">
<a href="/Documento?sort=EMPRESA&amp;sortdir=ASC">EMPRESA</a>            </th>
            <th scope="col">
<a href="/Documento?sort=CNPJ&amp;sortdir=ASC">CNPJ</a>            </th>
            <th scope="col">
<a href="/Documento?sort=CERT+CONJ+RFB%0d%0a&amp;sortdir=ASC">CERT CONJ RFB
</a>            </th>
            <th scope="col">
<a href="/Documento?sort=CERTIF+FGTS%0d%0a&amp;sortdir=ASC">CERTIF FGTS
</a>            </th>
            <th scope="col">
<a href="/Documento?sort=CND+D%c3%8dVIDA+ATIVA+DA+UNI%c3%83O&amp;sortdir=ASC">CND D&#205;VIDA ATIVA DA UNI&#195;O</a>            </th>
            <th scope="col">
<a href="/Documento?sort=CND+FGTS&amp;sortdir=ASC">CND FGTS</a>            </th>
            <th scope="col">
<a href="/Documento?sort=RECEITA+FEDERAL+PESSOA+JUR%c3%8dDICA+-+CNPJ&amp;sortdir=ASC">RECEITA FEDERAL PESSOA JUR&#205;DICA - CNPJ</a>            </th>
            <th scope="col">
<a href="/Documento?sort=TRIBUTOS+MOBILI%c3%81RIOS%0d%0a&amp;sortdir=ASC">TRIBUTOS MOBILI&#193;RIOS
</a>            </th>
</tr>
</thead>
<tbody>
    <tr>
        <td><input class='.checkbox-primary .checkbox-info chkProcessamentoDiario'  id='chkProcessamentoDiario' name='chkProcessamentoDiario' type='checkbox' value='1' data-chkIdProcessamentoDiario='1' data-chkIdRegistro='1'/></td>
        <td><div style='width:100%; height:100%; float:left;white-space:nowrap;'>ENERGIAS RENOVAVEIS S.A.</div></td>
        <td><div style='width:100%; height:100%; float:left;white-space:nowrap;'>08.439.659/0001-50</div></td>
        <td></td>
        <td></td>
        <td><a href='#'  class='download'  name='downloaditem' id='downloaditem1' data-IdProcessamentoDiario=1 data-IdRegistro=1  data-IdServico=1><span style='cursor:pointer'>CERTIDÃO POSITIVA</span></a></td>
        <td><a href='#'  class='download'  name='downloaditem' id='downloaditem2' data-IdProcessamentoDiario=1 data-IdRegistro=1  data-IdServico=2><span style='cursor:pointer'>Regular</span></a></td>
        <td><a href='#'  class='download'  name='downloaditem' id='downloaditem3' data-IdProcessamentoDiario=1 data-IdRegistro=1  data-IdServico=3><span style='cursor:pointer'>ATIVA</span></a></td>
        <td></td>
    </tr>
    <tr class="alt">
        <td><input class='.checkbox-primary .checkbox-info chkProcessamentoDiario'  id='chkProcessamentoDiario' name='chkProcessamentoDiario' type='checkbox' value='1' data-chkIdProcessamentoDiario='1' data-chkIdRegistro='2'/></td>
        <td><div style='width:100%; height:100%; float:left;white-space:nowrap;'>ENERGIAS RENOVAVEIS S.A. 2</div></td>
        <td><div style='width:100%; height:100%; float:left;white-space:nowrap;'>08.439.659/0002-31</div></td>
        <td></td>
        <td></td>
        <td><a href='#' class='download' name='downloaditem' id='downloaditem1' data-IdProcessamentoDiario=1 data-IdRegistro=2  data-IdServico=1><span  style='cursor:pointer'><font color='red'><b>A certidão deve ser emitida para o CNPJ da matriz ? 08.439.659/000150</b></font></span></a></td>
        <td><a href='#'  class='download'  name='downloaditem' id='downloaditem2' data-IdProcessamentoDiario=1 data-IdRegistro=2  data-IdServico=2><span style='cursor:pointer'>Regular</span></a></td>
        <td><a href='#' class='download' name='downloaditem' id='downloaditem3' data-IdProcessamentoDiario=1 data-IdRegistro=2  data-IdServico=3><span  style='cursor:pointer'><font color='red'><b>BAIXADA</b></font></span></a></td>
        <td></td>
    </tr>
    <tr>
        <td><input class='.checkbox-primary .checkbox-info chkProcessamentoDiario'  id='chkProcessamentoDiario' name='chkProcessamentoDiario' type='checkbox' value='1' data-chkIdProcessamentoDiario='1' data-chkIdRegistro='3'/></td>
        <td><div style='width:100%; height:100%; float:left;white-space:nowrap;'>EOL HOLDING S.A.</div></td>
        <td><div style='width:100%; height:100%; float:left;white-space:nowrap;'>11.594.952/0001-05</div></td>
        <td></td>
        <td></td>
        <td><a href='#'  class='download'  name='downloaditem' id='downloaditem1' data-IdProcessamentoDiario=1 data-IdRegistro=3  data-IdServico=1><span style='cursor:pointer'>CERTIDÃO NEGATIVA</span></a></td>
        <td></td>
        <td><a href='#'  class='download'  name='downloaditem' id='downloaditem3' data-IdProcessamentoDiario=1 data-IdRegistro=3  data-IdServico=3><span style='cursor:pointer'>ATIVA</span></a></td>
        <td></td>
    </tr>
</tbody>
</table>
  • It became difficult to understand. You want to go through all the rows of the table and add the Infos: processingDiario, record and idservico in an array, that?

  • @Aline he wants to use the event on the button to get all the items that have checkbox marked. Well, at least that’s what I got after I set up his HTML here and looked at the browser. But the question is really hard to understand, especially since HTML is also confused, because the checkbox has value=1 for all items. Therefore, it is not possible to know exactly which checkbox will be checked or not. The only property that differs would be the date-chkIdRegister. Therefore, he must rephrase the question.

  • @Andrewpaes, is it really? When he wants to take idService by date attribute, everyone is a.download. I think it’s unclear what he wants and how he wants it. = T

  • @Aline is definitely not clear. I would still downvote to request closure because I am using real data that can compromise customers. Therefore, I will give the chance to repeat the question, following this model: https://answall.com/help/mcve

  • @Andrews Paes what led you to conclude that this data is real ? and if they were real would be public knowledge, because they are data that can be consulted by anyone... I really don’t know why downvote for that reason, since the doubt is very likely from many other people.

1 answer

1


Using in this way $('.chkProcessmentDiary') you will always catch the first checkbox, Voce have to search the checkbox inside the tr that is in the iteration. So:

table.find('tr').each(function (index, tr) {

                        var _idprocessamentodiario = $(tr).find('.chkProcessamentoDiario').data('chkidprocessamentodiario');
                        var _idregistro = $(tr).find('.chkProcessamentoDiario').data('chkidregistro');
                        var _idservico = $(tr).find('.download').data('idservico');

                        ...
});
  • Thanks @Euler01 ! Worked perfectly !!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.