Mostrar los valores de un datatable de las filas que están ocultas (con paginacion)

publicado por: Anonymous

He mirado ya todas las preguntas y respuestas sobre estos temas pero no hay ninguna que me solucione el problema. Lo que necesito es recorrer un datatable y mostrar los valores de una columna. He probado todas las funciones que hay por aquí y con la documentación del datatable y todas me hacen lo mismo, me muestran los valores pero de las filas que se ven, es decir, las de la página actual, no las que están ocultas (ya que el datatable tiene paginación).

Ahora estaba probando esta funcion pero hace exactamente lo mismo:

var arrayIds = new Array();
var contador = 0;
var ids;
$("#tablaDatos tr").each(function(index){
    $(this).children("td").each(function(index2){
        switch(index2){
            case 0:
                ids = $(this).text();
                alert(ids);
                break;
        }
    });
});

Con esta función solo me muestra los valores de las filas que se ven.

<table id="tablaDatos" class="table table-striped table-bordered dt-responsive" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th data-priority="1">Id.Pieza</th>
                    <th>*</th>
                    <th>Num.Serie</th>
                    <th>Cod. Art.</th>
                    <th>Descripcion</th>
                    <th>Almacén</th>
                    <th>Estado Pieza</th>
                    <th>Proyecto</th>
                    <th>Proveedor</th>
                    <th>Observaciones</th>
                    <th data-priority="2">Acción</th>
                </tr>
            </thead>
            <tbody id="tbody_grid"></tbody>
        </table>

Saludos.

solución

DataTable tiene unos métodos para capturar los rows de la tabla generada aun estando estos ocultos, y son rows() y every().

https://datatables.net/reference/api/rows().every()

Si vieramos el data, toma todos los elementos de esa fila de la tabla ya que siempre tendrán posición horizontal y no vertical, por tanto hay que añadir al array que queramos o el uso que le demos, la posición que ocupa en la columna.

Si queda alguna duda, comenta.

_x000D_

_x000D_

let table = $('#dataTable').DataTable();_x000D_
//Declaramos 2 arrays de ejemplo que tomarán los elementos que tenga la 1º y 3º columna_x000D_
    let arrayCol1 = [],_x000D_
        arrayCol3 = [];_x000D_
_x000D_
// Con row().every() capturamos todos los rows estén o no ocultos_x000D_
    table.rows().every(function (rowIdx, tableLoop, rowLoop) {_x000D_
        var data = this.data();_x000D_
        arrayCol1.push(data[0]);   _x000D_
        arrayCol3.push(data[2]); _x000D_
    });_x000D_
//Imprimimos el resultado_x000D_
    for (let i = 0; i < arrayCol1.length; i++) {_x000D_
        if (i === 0) {_x000D_
            document.getElementById('htmlPrint').innerHTML += '<strong>La columna Rendering Engine tiene:</strong> ';_x000D_
        }_x000D_
        document.getElementById('htmlPrint').innerHTML += arrayCol1[i] + ', ';_x000D_
    }_x000D_
_x000D_
    for (let i = 0; i < arrayCol3.length; i++) {_x000D_
        if (i === 0) {_x000D_
            document.getElementById('htmlPrint').innerHTML += '<br /> <br /><strong>La columna Platform tiene:</strong> ';_x000D_
        }_x000D_
        document.getElementById('htmlPrint').innerHTML += arrayCol3[i] + '|, |';_x000D_
    }

_x000D_

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>_x000D_
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/css/jquery.dataTables.css">_x000D_
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.0/css/jquery.dataTables_themeroller.css">_x000D_
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>_x000D_
<div class="container">_x000D_
_x000D_
        <div class="container">_x000D_
_x000D_
    <table cellpadding="0" cellspacing="0" border="0" class="dataTable" id="dataTable">_x000D_
        <thead>_x000D_
            <tr>_x000D_
                <th>Rendering engine</th>_x000D_
                <th>Browser</th>_x000D_
                <th>Platform(s)</th>_x000D_
                <th>Engine version</th>_x000D_
                <th>CSS grade</th>_x000D_
            </tr>_x000D_
        </thead>_x000D_
        <tbody>_x000D_
            <tr>_x000D_
                <td>Trident</td>_x000D_
                <td>_x000D_
                    Internet_x000D_
                    Explorer 4.0_x000D_
                </td>_x000D_
                <td>Win 95+</td>_x000D_
                <td> 4</td>_x000D_
                <td>X</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Trident</td>_x000D_
                <td>_x000D_
                    Internet_x000D_
                    Explorer 5.0_x000D_
                </td>_x000D_
                <td>Win 95+</td>_x000D_
                <td>5</td>_x000D_
                <td>C</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Trident</td>_x000D_
                <td>_x000D_
                    Internet_x000D_
                    Explorer 5.5_x000D_
                </td>_x000D_
                <td>Win 95+</td>_x000D_
                <td>5.5</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Trident</td>_x000D_
                <td>_x000D_
                    Internet_x000D_
                    Explorer 6_x000D_
                </td>_x000D_
                <td>Win 98+</td>_x000D_
                <td>6</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Trident</td>_x000D_
                <td>Internet Explorer 7</td>_x000D_
                <td>Win XP SP2+</td>_x000D_
                <td>7</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Trident</td>_x000D_
                <td>AOL browser (AOL desktop)</td>_x000D_
                <td>Win XP</td>_x000D_
                <td>6</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Firefox 1.0</td>_x000D_
                <td>Win 98+ / OSX.2+</td>_x000D_
                <td>1.7</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Firefox 1.5</td>_x000D_
                <td>Win 98+ / OSX.2+</td>_x000D_
                <td>1.8</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Firefox 2.0</td>_x000D_
                <td>Win 98+ / OSX.2+</td>_x000D_
                <td>1.8</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Firefox 3.0</td>_x000D_
                <td>Win 2k+ / OSX.3+</td>_x000D_
                <td>1.9</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Camino 1.0</td>_x000D_
                <td>OSX.2+</td>_x000D_
                <td>1.8</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Camino 1.5</td>_x000D_
                <td>OSX.3+</td>_x000D_
                <td>1.8</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Netscape 7.2</td>_x000D_
                <td>Win 95+ / Mac OS 8.6-9.2</td>_x000D_
                <td>1.7</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Netscape Browser 8</td>_x000D_
                <td>Win 98SE+</td>_x000D_
                <td>1.7</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Netscape Navigator 9</td>_x000D_
                <td>Win 98+ / OSX.2+</td>_x000D_
                <td>1.8</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Mozilla 1.0</td>_x000D_
                <td>Win 95+ / OSX.1+</td>_x000D_
                <td>1</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Mozilla 1.1</td>_x000D_
                <td>Win 95+ / OSX.1+</td>_x000D_
                <td>1.1</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Mozilla 1.2</td>_x000D_
                <td>Win 95+ / OSX.1+</td>_x000D_
                <td>1.2</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Mozilla 1.3</td>_x000D_
                <td>Win 95+ / OSX.1+</td>_x000D_
                <td>1.3</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Mozilla 1.4</td>_x000D_
                <td>Win 95+ / OSX.1+</td>_x000D_
                <td>1.4</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Mozilla 1.5</td>_x000D_
                <td>Win 95+ / OSX.1+</td>_x000D_
                <td>1.5</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Mozilla 1.6</td>_x000D_
                <td>Win 95+ / OSX.1+</td>_x000D_
                <td>1.6</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Mozilla 1.7</td>_x000D_
                <td>Win 98+ / OSX.1+</td>_x000D_
                <td>1.7</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Mozilla 1.8</td>_x000D_
                <td>Win 98+ / OSX.1+</td>_x000D_
                <td>1.8</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Seamonkey 1.1</td>_x000D_
                <td>Win 98+ / OSX.2+</td>_x000D_
                <td>1.8</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Gecko</td>_x000D_
                <td>Epiphany 2.20</td>_x000D_
                <td>Gnome</td>_x000D_
                <td>1.8</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Webkit</td>_x000D_
                <td>Safari 1.2</td>_x000D_
                <td>OSX.3</td>_x000D_
                <td>125.5</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Webkit</td>_x000D_
                <td>Safari 1.3</td>_x000D_
                <td>OSX.3</td>_x000D_
                <td>312.8</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Webkit</td>_x000D_
                <td>Safari 2.0</td>_x000D_
                <td>OSX.4+</td>_x000D_
                <td>419.3</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Webkit</td>_x000D_
                <td>Safari 3.0</td>_x000D_
                <td>OSX.4+</td>_x000D_
                <td>522.1</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
            <tr>_x000D_
                <td>Webkit</td>_x000D_
                <td>OmniWeb 5.5</td>_x000D_
                <td>OSX.4+</td>_x000D_
                <td>420</td>_x000D_
                <td>A</td>_x000D_
            </tr>_x000D_
        </tbody>_x000D_
    </table>_x000D_
_x000D_
</div>_x000D_
<div id="htmlPrint" style="width: 100%; height: auto; margin-top: 36px;"></div>

_x000D_

_x000D_

_x000D_

Respondido por: Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *