У меня есть следующая таблица в HTML и хотелось бы программно извлечь атрибут call_id из каждой строки в таблице.
Я вставляю как код, так и упрощенный вид из своего редактора, поэтому вы можете видеть, что меня интересует:
Упрощенный вид:
Я делаю следующий вызов, чтобы получить всю таблицу, и хотел бы использовать этот объект для извлечения данных из:
var recordingTable = parent.recordingsFrame.document.getElementById("recordingTable");
Для этого решения я нацелен на чистый javascript, а не на использование внешней библиотеки.
<table cellspacing="1" id="recordingTable"><input id="" name="" type="hidden" value="recordingTable">
<thead class="callView">
<tr>
<th>
<translate>STATE</translate>
</th>
<th>
<translate>Call ID</translate>
</th>
<th>
<translate>Text Type</translate>
</th>
<th>
<translate>Start time</translate>
</th>
<th>
<translate>Call duration</translate>
</th>
<th>
<translate>Radio/Unit ID</translate>
</th>
<th>
<translate>Participant name</translate>
</th>
<th>
<translate>Tag</translate>
</th>
<th>
<translate>Note</translate>
</th>
<th>
<translate></translate>
</th>
</tr>
</thead>
<tbody class="callView">
<tr active="0" archivecount="0" callprovidertype="16" call_id="5" callsempty="false" deleted="false" flags="1" haslink="false" onlinecount="1" screenreccount="0" screensempty="true" issms="false" istext="true" style="background-color: rgb(208, 230, 248);">
<td class="spannedCell" style="color: rgb(18, 159, 221);">
<div id="pics_scrollPanel">
<table cellspacing="0" id="pics"><input id="" name="" type="hidden" value="pics">
<thead></thead>
<tbody>
<tr>
<td class="spannedCell"><i class="fa fa-envelope-o iTags" id="smsCallIcon" title="message"></i></td>
<td class="tableColumn"></td>
<td class="tableColumn"></td>
<td class="spannedCell"><span class="icon-dvd-archive-restored spanTags" id="restoredCallIcon" title="restored"></span></td>
<td class="tableColumn"></td>
</tr>
</tbody>
</table>
</div>
</td>
<td class="spannedCell" style="color: rgb(18, 159, 221);"><span>5</span></td>
<td class="spannedCell" style="color: rgb(18, 159, 221);"><span></span></td>
<td class="spannedCell" style="color: rgb(18, 159, 221);"><span>2013-08-06 19:11:24</span></td>
<td class="spannedCell" style="color: rgb(18, 159, 221);"><span>00:02:00</span></td>
<td class="spannedCell" style="color: rgb(18, 159, 221);">
<div>
<div>5010</div>
<div>5010 (Caller)</div>
</div>
</td>
<td class="spannedCell" style="color: rgb(18, 159, 221);">
<div>
<div>Samsung</div>
<div>Samsung</div>
</div>
</td>
<td class="spannedCell" style="color: rgb(18, 159, 221);"><span></span></td>
<td class="spannedCell" style="color: rgb(18, 159, 221);">
<div title="00 D4 14 86 7E CA 3B 7D 61 20">00 D4 14 8...</div>
</td>
<td class="tableColumn" style="color: rgb(18, 159, 221);"></td>
</tr>
<tr active="0" archivecount="0" callprovidertype="16" call_id="4" callsempty="false" deleted="false" flags="1" haslink="false" onlinecount="2" screenreccount="0" screensempty="true" issms="false" istext="true">
<td class="spannedCell">
<div id="pics_scrollPanel">
<table cellspacing="0" id="pics"><input id="" name="" type="hidden" value="pics">
<thead></thead>
<tbody>
<tr>
<td class="spannedCell"><i class="fa fa-envelope-o iTags" id="smsCallIcon" title="message"></i></td>
<td class="tableColumn"></td>
<td class="tableColumn"></td>
<td class="spannedCell"><span class="icon-dvd-archive-restored spanTags" id="restoredCallIcon" title="restored"></span></td>
<td class="tableColumn"></td>
</tr>
</tbody>
</table>
</div>
</td>
<td class="spannedCell"><span>4</span></td>
<td class="spannedCell"><span></span></td>
<td class="spannedCell"><span>2013-08-06 19:11:17</span></td>
<td class="spannedCell"><span>00:02:00</span></td>
<td class="spannedCell">
<div>
<div>8882103</div>
<div>5006 (Caller)</div>
</div>
</td>
<td class="spannedCell">
<div>
<div>Samsung</div>
<div>Samsung</div>
</div>
</td>
<td class="spannedCell"><span></span></td>
<td class="spannedCell">
<div title="00 D4 0E 96 7E DF 9E FC 80 F0">00 D4 0E 9...</div>
</td>
<td class="tableColumn"></td>
</tr>
<tr active="0" archivecount="0" callprovidertype="16" call_id="3" callsempty="false" deleted="false" flags="1" haslink="false" onlinecount="1" screenreccount="0" screensempty="true" issms="false" istext="true">
<td class="spannedCell">
<div id="pics_scrollPanel">
<table cellspacing="0" id="pics"><input id="" name="" type="hidden" value="pics">
<thead></thead>
<tbody>
<tr>
<td class="spannedCell"><i class="fa fa-envelope-o iTags" id="smsCallIcon" title="message"></i></td>
<td class="tableColumn"></td>
<td class="tableColumn"></td>
<td class="spannedCell"><span class="icon-dvd-archive-restored spanTags" id="restoredCallIcon" title="restored"></span></td>
<td class="tableColumn"></td>
</tr>
</tbody>
</table>
</div>
</td>
<td class="spannedCell"><span>3</span></td>
<td class="spannedCell"><span></span></td>
<td class="spannedCell"><span>2013-08-06 19:11:11</span></td>
<td class="spannedCell"><span>00:02:00</span></td>
<td class="spannedCell">
<div>
<div>5010</div>
<div>5010 (Caller)</div>
</div>
</td>
<td class="spannedCell">
<div>
<div>Samsung</div>
<div>Samsung</div>
</div>
</td>
<td class="spannedCell"><span></span></td>
<td class="spannedCell">
<div title="00 D3 F1 16 7E 3D D5 87 80 60">00 D3 F1 1...</div>
</td>
<td class="tableColumn"></td>
</tr>
<tr active="0" archivecount="0" callprovidertype="16" call_id="2" callsempty="false" deleted="false" flags="1" haslink="false" onlinecount="1" screenreccount="0" screensempty="true" issms="false" istext="true">
<td class="spannedCell">
<div id="pics_scrollPanel">
<table cellspacing="0" id="pics"><input id="" name="" type="hidden" value="pics">
<thead></thead>
<tbody>
<tr>
<td class="spannedCell"><i class="fa fa-envelope-o iTags" id="smsCallIcon" title="message"></i></td>
<td class="tableColumn"></td>
<td class="tableColumn"></td>
<td class="spannedCell"><span class="icon-dvd-archive-restored spanTags" id="restoredCallIcon" title="restored"></span></td>
<td class="tableColumn"></td>
</tr>
</tbody>
</table>
</div>
</td>
<td class="spannedCell"><span>2</span></td>
<td class="spannedCell"><span></span></td>
<td class="spannedCell"><span>2013-08-06 19:08:47</span></td>
<td class="spannedCell"><span>00:02:00</span></td>
<td class="spannedCell">
<div>
<div>8882107</div>
<div>5014 (Caller)</div>
</div>
</td>
<td class="spannedCell">
<div>
<div>Samsung</div>
<div>Samsung</div>
</div>
</td>
<td class="spannedCell"><span></span></td>
<td class="spannedCell">
<div title="20 D4 12 BE 7E 93 E9 4C E1 20">20 D4 12 B...</div>
</td>
<td class="tableColumn"></td>
</tr>
<tr active="0" archivecount="0" callprovidertype="16" call_id="1" callsempty="false" deleted="false" flags="1" haslink="false" onlinecount="1" screenreccount="0" screensempty="true" issms="false" istext="true">
<td class="spannedCell">
<div id="pics_scrollPanel">
<table cellspacing="0" id="pics"><input id="" name="" type="hidden" value="pics">
<thead></thead>
<tbody>
<tr>
<td class="spannedCell"><i class="fa fa-envelope-o iTags" id="smsCallIcon" title="message"></i></td>
<td class="tableColumn"></td>
<td class="tableColumn"></td>
<td class="spannedCell"><span class="icon-dvd-archive-restored spanTags" id="restoredCallIcon" title="restored"></span></td>
<td class="tableColumn"></td>
</tr>
</tbody>
</table>
</div>
</td>
<td class="spannedCell"><span>1</span></td>
<td class="spannedCell"><span></span></td>
<td class="spannedCell"><span>2013-08-06 19:08:47</span></td>
<td class="spannedCell"><span>00:02:00</span></td>
<td class="spannedCell">
<div>
<div>8882105</div>
<div>5010 (Caller)</div>
</div>
</td>
<td class="spannedCell">
<div>
<div>Samsung</div>
<div>Samsung</div>
</div>
</td>
<td class="spannedCell"><span></span></td>
<td class="spannedCell">
<div title="20 D4 08 66 7E 82 A9 4C E1 20">20 D4 08 6...</div>
</td>
<td class="tableColumn"></td>
</tr>
</tbody>
</table>
Вот быстрый фрагмент, который получил все значения call_id в вашей таблице:
var rows = document.querySelectorAll('[call_id]')
var callIds = Array.prototype.map.call(rows, function(tr){
return tr.getAttribute('call_id')
})
console.log(callIds)
Объяснение кода:
document.querySelectorAll()
запрашивает DOM для всех элементов, соответствующих селектору[call_id]
, который получает любой элемент с атрибутом, соответствующим call_id="<any>"
document.querySelectorAll()
не является нормальным массивом с методом map()
, я использую прототип Array для вызова карты метода класса, передавая результат в качестве this
значенияmap()
возвращает новый массив с возвращаемым обратным вызовом, в этом случае значения call_id