Varias cosas:
- La variable
row
del for
no existe, tu variable de condición del bucle es i
- En dicho bucle la condición de parada tiene que ser
tab.rows.length
row.cells
te da fallo porque row
es undefined
- En cuanto al problema de que te obtiene todas las filas. Lo mejor es que, por un lado, saques el botón de tu tabla y por otro, para la cabecera pon el
tag <th>
en vez de <td>
y en el bucle pon una condición con indexOf()
para que no te obtenga aquellas filas con el tag <th>
Deberías tener algo de este estilo:
let tab=document.getElementById('ordenT');
let jsonArr=[];
for(let i=0; i<tab.rows.length; i++){
if(tab.rows[i].innerHTML.indexOf("<th")<0){
let col=tab.rows[i].cells;
let jsonObj= { folio: col[0].innerHTML }
jsonArr.push(jsonObj);
}
}
console.log(jsonArr);
//CODIGO HTML
<table class="form_table" id="ordenT" width="100%">
<thead>
<th style="text-align: center; background-color: #fff0b3 !important">
<b>Órdenes de Trabajo.</b>
</th>
</tr>
<tr>
<td>
Dato 1
</td>
</tr>
<tr>
<td>
Dato 2
</td>
</tr>
</table>
<input type="button" class="button" id="txtIdTramite" name="txtIdTramite" value="ASIGNAR ORDENES DE TRABAJO" autofocus="autofocus" onclick="enviar();"></input>
PD: Observarás que en vez de var
estoy utilizando let
. Si tu navegador no soporta let
(Internet Explorer < 11) utiliza var
. Aquí tienes más info.
Edito (con el botón dentro de la tabla)
let tab=document.getElementById('ordenT');
let jsonArr=[];
for(let i=0; i<tab.rows.length; i++){
if(tab.rows[i].innerHTML.indexOf("<th")<0 && tab.rows[i].innerHTML.indexOf("button") < 0){
let col=tab.rows[i].cells;
let jsonObj= { folio: col[0].innerHTML }
jsonArr.push(jsonObj);
}
}
console.log(jsonArr);
<table class="form_table" id="ordenT" width="100%">
<thead>
<th style="text-align: center; background-color: #fff0b3 !important">
<b>Órdenes de Trabajo.</b>
</th>
</tr>
<tr>
<td>
Dato 1
</td>
</tr>
<tr>
<td>
Dato 2
</td>
</tr>
<tr>
<td style="text-align: center">
<input type="button" class="button" id="txtIdTramite" name="txtIdTramite" value="ASIGNAR ORDENES DE TRABAJO" autofocus="autofocus" onclick="enviar();"></input>
</td>
</tr>
</table>