У меня есть несколько таблиц, которые сгенерированы из цикла в wp и написали jquery для удаления столбцов с пустыми значениями td, который работает для первой таблицы, а затем добавляет те же столбцы для всех других таблиц. Я пытался использовать «каждый» с jquery для запуска для каждой таблицы, но это не так.
Пример:
Каждая таблица имеет 1 строку и имеет одинаковое количество столбцов (7). Пустые столбцы скрыты, это работает с одной таблицей. Допустим, в таблице 1 есть 2 видимых столбца, а в таблице 2 - 3 видимых столбца. 3-й столбец (из таблицы 2) добавляется в таблицу 1, даже если он пуст.
Вот мой jquery
$(document).ready(function() {
$('.man-table').each(function (table) {
//count # of columns
var numCols = $("th", table).length;
for ( var i=1; i<=numCols; i++ ) {
var empty = true;
//grab all the <td>'s of the column at i
$("td:nth-child(" + i + ")", table).each(function(index, el) {
//check if the <span> of this <td> is empty
if ( $("span", el).text() != "" ) {
empty = false;
return false; //break out of each() early
}
});
if ( empty ) {
$("td:nth-child(" + i + ")", table).hide(); //hide <td>'s
$("th:nth-child(" + i + ")", table).hide(); //hide header <th>
}
}
})
});
Вот моя таблица, сгенерированная для каждого цикла
<table class='man-table'>
<thead>
<tr>
<th class='numeric'><span>inductance (r)</span></th>
<th class='numeric'><span>mount</span></th>
<th class='numeric'><span>dim (mm)</span></th>
<th class='numeric'><span>r current</span></th>
<th class='numeric'><span>impedance</span></th>
<th class='numeric'><span>capacitance</span></th>
<th class='numeric'><span>resistance</span></th>
<th class='numeric'><span>spec</span></th>
</tr>
</thead>
<tr>
<td data-title='inductance (r)' class='numeric'><span>{$ind}</span></td>
<td data-title='mount type' class='numeric'><span>{$mnt}</span></td>
<td data-title='dimensions' class='numeric'><span>{$dim}</span></td>
<td data-title='rated current' class='numeric'><span>{$rat}</span></td>
<td data-title='impedance' class='numeric'><span>{$imp}</span></td>
<td data-title='capacitance' class='numeric'><span>{$cap}</span></td>
<td data-title='resistance' class='numeric'><span>{$res}</span></td>
<td data-title='spec sheet' class='numeric'><span><a href='{$site_url}/product-spec/{$prod_pdf}' target='_blank;'><div id='spec-btn'>DOWNLOAD</div></a></span></td>
</tr>
</table>