How to make one Javascript table popluate from multiple sets of data - javascript

I am creating a page that has 63 sets of data (states, provinces, territories) presented as an html table. The tables display 4 bits of information in columns and each table has as few as one entry or as many as 45 entries. I am currently marking this up on the page itself but with what will be hundreds of rows of data, it's making for an extremely long html page. How can I create one table in Javascript and have it populate the information from my 63 sets of data based, maybe on a unique ID or something?
Here are a sample of the tables I'm working with.
<div class="hidden" id="Alabama"> <!--ALABAMA-->
<table class="place-table">
<thead>
<tr>
<th class="location-column">Location</th>
<th class="name-column">Name</th>
<th class="type-column">Type</th>
<th class="rating-column">Rating</th>
</tr>
</thead>
<tbody>
<tr>
<td>Eight Mile</td>
<td><a class="place-name-link" href="https://www.campendium.com/chickasabogue-park" target="_blank" rel="noopener">Chickasabogue Park</a></td>
<td>County Park</td>
<td class="center">3</td>
</tr>
<tr>
<td>Fort Payne</td>
<td><a class="place-name-link" href="https://www.campendium.com/desoto-state-park" target="_blank" rel="noopener">DeSoto State Park</a></td>
<td>Fort Payne</td>
<td class="center">4</td>
</tr>
<tr>
<td>Helflin</td>
<td><a class="place-name-link" href="https://www.campendium.com/pine-glen-recreation-area" target="_blank" rel="noopener">Pine Glen Recreation Area</a></td>
<td>National Forest</td>
<td class="center">3</td>
</tr>
<tr>
<td>Tuskaloosa</td>
<td><a class="place-name-link" href="https://www.allstays.com/c/Walmart/AL-Tuscaloosa-Wal-Mart-715.htm" target="_blank" rel="noopener">Walmart/Sam's Club</a></td>
<td>Big Box Parking Lot</td>
<td class="center">2</td>
</tr>
<!--<tr>
<td>Location</td>
<td>Camping place name</td>
<td>Camping place type</td>
<td class="center">Rating</td>
</tr>-->
<tbody>
</table>
</div>
<div class="hidden" id="Alberta"> <!--ALBERTA-->
<table class="place-table">
<thead>
<tr>
<th class="location-column">Location</th>
<th class="name-column">Name</th>
<th class="type-column">Type</th>
<th class="rating-column">Rating</th>
</tr>
</thead>
<tbody>
<tr>
<td>Steveville</td>
<td><a class="place-name-link" href="https://www.campendium.com/steveville-bridge" target="_blank" rel="noopener">Steveville Bridge Campground</td>
<td>Provincial Park</td>
<td class="center">4</td>
</tr>
<!--<tr>
<td>Location</td>
<td><a class="place-name-link" href="" target="_blank" rel="noopener">Place Name</td>
<td>Place Type</td>
<td class="center">Rating</td>
</tr>-->
<tbody>
</table>
</div>
<div class="hidden" id="Arizona"> <!--ARIZONA-->
<table class="place-table">
<thead>
<tr>
<th class="location-column">Location</th>
<th class="name-column">Name</th>
<th class="type-column">Type</th>
<th class="rating-column">Rating</th>
</tr>
</thead>
<tbody>
<tr>
<tr>
<td>Camp Verde</td>
<td><a class="place-name-link" href="https://www.campendium.com/distant-drums-rv-resort" target="_blank" rel="noopener">Distant Drums RV Resort</td>
<td>RV Park</td>
<td class="center">3+</td>
</tr>
<tr>
<td>Cottonwood</td>
<td><a class="place-name-link" href="https://www.campendium.com/dead-horse-ranch-state-park" target="_blank" rel="noopener">Dead Horse Ranch State Park</td>
<td>State Park</td>
<td class="center">3</td>
</tr>
<tr>
<td>Dragoon</td>
<td><a class="place-name-link" href="https://www.arizonarestareas.com/az-i10-rest-area-eastbound-mile-marker-320/" target="_blank" rel="noopener">I-10 Texas Canyon</td>
<td> Rest Area</td>
<td class="center">2</td>
</tr>
<tr>
<td>Flagstaff</td>
<td><a class="place-name-link" href="https://www.campendium.com/j-h-rv-park" target="_blank" rel="noopener">J & H RV Park</td>
<td>RV Park</td>
<td class="center">3</td>
</tr>
<tr>
<td>Kingman</td>
<td><a class="place-name-link" href="https://www.campendium.com/blake-ranch-rv-park" target="_blank" rel="noopener">Blake Ranch RV Park and Horse Motel</td>
<td>RV Park</td>
<td class="center">2</td>
</tr>
<tr>
<td>Mayer</td>
<td><a class="place-name-link" href="https://www.campendium.com/bloody-basin-road-east-dispersed-camping" target="_blank" rel="noopener">Bloody Basin Road East Dispersed Camping</td>
<td>BLM</td>
<td class="center">4</td>
</tr>
<td>Mesa</td>
<td><a class="place-name-link" href="https://www.tripadvisor.com/ShowUserReviews-g31281-d4739035-r574036384-Bass_Pro_Shop_Outdoor_World-Mesa_Arizona.html" target="_blank" rel="noopener">Bass Pro Shop</td>
<td>Parking Lot</td>
<td class="center">2</td>
</tr>
<tr>
<tr>
<td>Parker</td>
<td><a class="place-name-link" href="https://www.campendium.com/shea-road" target="_blank" rel="noopener">Shea Road Dispersed Camping</td>
<td>BLM</td>
<td class="center">5</td>
</tr>
<tr>
<td>Prescott</td>
<td><a class="place-name-link" href="https://www.campendium.com/thumb-butte-loop-designated-dispersed-campsite-1" target="_blank" rel="noopener">Thumb Butte Loop Designated Dispersed Campsite</td>
<td>National Forest</td>
<td class="center">4</td>
</tr>
<td>South Happy Camp Canyon, Bowie</td>
<td><a class="place-name-link" href="https://www.campendium.com/indian-bread-rocks" target="_blank" rel="noopener">Indian Bread Rocks</td>
<td>BLM</td>
<td class="center">5+</td>
</tr>
<tr>
<td>Topock</td>
<td><a class="place-name-link" href="https://www.campendium.com/needle-mountain-road" target="_blank" rel="noopener">Needle Mountain Road Dispersed Camping</td>
<td>BLM</td>
<td class="center">3</td>
</tr>
<tr>
<td>Tonopah</td>
<td><a class="place-name-link" href="https://www.campendium.com/saddle-mountain" target="_blank" rel="noopener">Saddle Mountain Dispersed Camping</td>
<td>BLM</td>
<td class="center">4</td>
</tr>
<tr>
<td>Tucson</td>
<td><a class="place-name-link" href="https://www.campendium.com/diamond-j-rv-park" target="_blank" rel="noopener">Justin’s Diamond J RV Park</td>
<td>RV Park</td>
<td class="center">4</td>
</tr>
<tr>
<td>White Hills</td>
<td><a class="place-name-link" href="https://www.campendium.com/kingman-wash" target="_blank" rel="noopener">Kingman Wash Dispersed Camping</td>
<td>National Recreation Area</td>
<td class="center">2</td>
</tr>
<tr>
<td>Wickenburg</td>
<td><a class="place-name-link" href="https://www.campendium.com/vulture-peak" target="_blank" rel="noopener">Vulture Peak</td>
<td>State Trust Land</td>
<td class="center">4</td>
</tr>
<!--<tr>
<td>Location</td>
<td><a class="place-name-link" href="" target="_blank" rel="noopener">Place Name</td>
<td>Place Type</td>
<td class="center">Rating</td>
</tr>-->
<tbody>
</table>
</div>
I understand only very rudimentary Javascript, so an actual example of code will be much more useful than an explanation of the logic behind it. Thanks in advance for any help.
I've seen examples of how to populate one table with multiple arrays, i.e., multiple rows, but I can't find an example of coding one table that will dynamically populate different arrays. If I just replicate the code to create one Javascript table 63 times, I'm not saving myself any work or space.

Related

.closest() not working on jquery when applied to on change event

I have a table like this
<table style="width:100%">
<tr>
<th>Downpayment (%)</th>
<th>Downpayment (₱)</th>
<th>Balance</th>
<th>Payment Period (Months)</th>
<th>MA 1 - XX</th>
<th>MA 1 - XX (VAT)</th>
</tr>
<tr>
<td class="dp-perce" data-perce="0">0%</td>
<td class="dp-peso"></td>
</tr>
<tr>
<td class="dp-perce" data-perce="15">15%</td>
<td class="dp-peso"></td>
</tr>
<tr>
<td class="dp-perce" data-perce="30">30%</td>
<td class="dp-peso"></td>
</tr>
<tr>
<td class="dp-perce" data-perce="50">50%</td>
<td class="dp-peso"></td>
</tr>
<tr>
<td class="dp-perce" data-perce="95">95%</td>
<td class="dp-peso"></td>
</tr>
</table>
whenever I use closest(), it is always returning null. what seems to be the problem?
here is my jquery:
<script>
$('#unit-type').on('change', function() {
$('.dp-peso').each(function(){
var percentage = $(this).closest('td').find('.dp-perce').data('perce');
$(this).text(percentage);
});
});
</script>
what this script is doing is to get the value of data-attribute of the closest dp-perce then display to class name dp-peso.
please let me know the bug. I've been stuck here for a long time/

Get specific HTML table element with google script

I've been looking for this same question but none of them seems to have an accurate answer.
I think this should be simpler, I want to get a specific cell from an HTML table in a website using google script.
It needs to work inside google script so pls dont suggest =importhtml, although that's exactly the function I'm looking for.
This is a website example https://prestadores.pami.org.ar/result.php?c=6-2-1-1&beneficio=110313900302&parent=00&vm=2
I need to get the date next to the FECHA DE NACIMIENTO cell, but I dont want to do messy things like indexOf since I have to do it with a few more values.
<table width="480" border="0" cellpadding="3" style="margin-left: 40px;">
<tbody><tr>
<td class="gris"><p>APELLIDO Y NOMBRE:</p></td>
<td class="grisClaro"><p>PEREZ JUANA ANTONIA </p></td>
</tr>
<tr>
<td class="gris"><p>TIPO BENEFICIARIO:</p></td>
<td class="crema"><p>JUBILACION</p></td>
</tr>
<tr>
<td class="gris"><p>N? BENEFICIO:</p></td>
<td class="grisClaro"><p>110313900302</p></td>
</tr>
<tr>
<td class="gris"><p>FECHA DE NACIMIENTO:</p></td>
<td class="crema"><p>08/03/1922</p></td>
</tr>
<tr>
<td class="gris"><p>NACIONALIDAD:</p></td>
<td class="grisClaro"><p>ARGENTINA</p></td>
</tr>
<tr>
<td class="gris"><p>PAIS:</p></td>
<td class="crema"><p>ARGENTINA</p></td>
</tr>
<tr>
<td class="gris"><p>UGL:</p></td>
<td class="crema"><p>LANUS</p></td>
</tr>
<tr>
<td class="gris"><p>DOCUMENTO:</p></td>
<td class="grisClaro"><p>DNI123456</p></td>
</tr>
<tr>
<td class="gris"><p>SEXO:</p></td>
<td class="crema"><p>FEMENINO</p></td>
</tr>
<tr>
<td class="gris"><p>ESTADO CIVIL:</p></td>
<td class="grisClaro"><p>SEPARADO/A LEGAL</p></td>
</tr>
<tr>
<td class="gris"><p>VENCIMIENTO AFILIACION:</p></td>
<td class="crema"><p></p></td>
</tr>
<tr>
<td class="gris"><p>UNIDAD OPERATIVA:</p></td>
<td class="grisClaro"><p>NO ASIGNADA</p></td>
</tr>
<tr>
<td class="gris"><p>ALTA:</p></td>
<td class="crema"><p>01/09/1982</p></td>
</tr>
<tr>
<td class="gris"><p>BAJA:</p> </td>
<td class="grisClaro"><p>10/10/2013</p></td>
</tr>
<tr>
<td class="gris"><p>OTRA OBRA SOCIAL:</p></td>
<td class="crema"><p>NO</p></td>
</tr>
</tbody></table>
Any suggestions?
Using jQuery's contains selector, it can be done like the following easily.
let td = $('table td.gris:contains("FECHA DE NACIMIENTO")');
console.log(td);
let theDate = td.next('td').text();
console.log(theDate);

jQuery DataTable multi rows header

I used jQuery DataTable to show some data.
Here is my code.
I tried to add collapse/expand using bootstrap collapse functionality.
So I made two rows.
But if you see console it throws an javascript error so code below it not running.
How can I fix this?
Here is html code:
<table id="taskTable">
<thead class="bg-light text-capitalize">
<tr>
<th>Subject</th>
<th>Name</th>
<th>Duration</th>
<th>User</th>
</tr>
<tr style="display:none;">
<th></th>
</tr>
</thead>
<tbody>
<tr data-toggle="collapse" data-target="#demo1">
<td>Subject1</td>
<td>Name1</td>
<td>Duration1</td>
<td>User1</td>
</tr>
<tr id="demo1">
<td>aaa</td>
</tr>
<tr data-toggle="collapse" data-target="#demo2">
<td>Subject2</td>
<td>Name2</td>
<td>Duration2</td>
<td>User2</td>
</tr>
<tr id="demo2">
<td>aaa</td>
</tr>
</tbody>
</table>
Not feeling good using this approach:
Because of search, the sort, etc will break the concept of <tr> as header and it's next <tr> as content.
Maybe you can achieve the same thing using a different approach, like you can have child rows like here. But you can also fix the above problem as below:
JS Fiddle
HTML
<table id="taskTable">
<thead class="bg-light text-capitalize">
<tr>
<th>Subject</th>
<th>Name</th>
<th>Duration</th>
<th>User</th>
</tr>
<tr style="display:none;">
<th colspan="4"></th>
</tr>
</thead>
<tbody>
<tr data-toggle="collapse" data-target="#demo1">
<td>Subject1</td>
<td>Name1</td>
<td>Duration1</td>
<td>User1</td>
</tr>
<tr id="demo1">
<td colspan="4">aaa</td>
<td style="display: none;"></td>
<td style="display: none;"></td>
<td style="display: none;"></td>
</tr>
<tr data-toggle="collapse" data-target="#demo2">
<td>Subject2</td>
<td>Name2</td>
<td>Duration2</td>
<td>User2</td>
</tr>
<tr id="demo2">
<td colspan="4">aa2</td>
<td style="display: none;"></td>
<td style="display: none;"></td>
<td style="display: none;"></td>
</tr>
</tbody>
</table>
JS
$("#taskTable").dataTable({
"ordering": false
});

Deleting duplicate adjacent td elements using jquery

I've the html code as shown below.
<div class="serverSet">
<h2>JH Storefront servers</h2>
<table border="1" class="CSSTableGenerator" class="myTable">
<tr>
<th>Component</th>
<th>Properties</th>
<th class="servers"> lqwasc10 </th>
<th class="servers"> lqwasc11 </th>
</tr>
<tr>
<td class="comps">DeliveryMethodsRepository</td>
<td class="props">externalCacheBatchInfoSize</td>
<tr/>
<tr>
<td class="comps">InventoryManager</td>
<td class="comps">InventoryManager</td>
<td class="props">itemType</td>
<tr/>
<tr>
<td class="comps">InventoryManager</td>
<td class="props">maxConcurrentUpdateRetries</td>
<tr/>
<tr>
<td class="comps">CatalogTools</td>
<td class="comps">CatalogTools</td>
<td class="props">queryASAFFabrics</td>
<tr/>
<tr>
<td class="comps">CatalogTools</td>
<td class="props">loggingDebug</td>
<tr/>
<tr>
<td class="comps">CatalogTools</td>
<td class="props">outOfStockCode</td>
</tr>
</table>
</div>
In the above html code, there are few duplicate components present in adjacent properties column. Is there a way we can identify those duplicate components from properties column and delete them?
In the above example, two components CatalogTools and InventoryManager are present in properties column. Because of this, their respective properties have moved to an adjacent column on the right side.
The above html code might look faulty as it is generated from server, so I want to tidy up with jquery.
Eventually, I'm looking for an html as shown in this screenshot.
If you need some more details, please do let me know.
Thanks in advance.
var dups = $('.comps + .comps')
dups.remove()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="serverSet">
<h2>JH Storefront servers</h2>
<table border="1" class="CSSTableGenerator" class="myTable">
<tr>
<th>Component</th>
<th>Properties</th>
<th class="servers"> lqwasc10 </th>
<th class="servers"> lqwasc11 </th>
</tr>
<tr>
<td class="comps">DeliveryMethodsRepository</td>
<td class="props">externalCacheBatchInfoSize</td>
<tr/>
<tr/>
<td class="comps">InventoryManager</td>
<td class="comps">InventoryManager</td>
<td class="props">itemType</td>
<tr/>
<td class="comps">InventoryManager</td>
<td class="props">maxConcurrentUpdateRetries</td>
<tr/>
<tr/>
<td class="comps">CatalogTools</td>
<td class="comps">CatalogTools</td>
<td class="props">queryASAFFabrics</td>
<tr/>
<td class="comps">CatalogTools</td>
<td class="props">loggingDebug</td>
<tr/>
<td class="comps">CatalogTools</td>
<td class="props">outOfStockCode</td>
<tr/>
<tr/>
</tr>
</tr>
</table>
</div>

jQuery Text Replace

Have some text that needs to be replaced, searched around this website for all results with similar titles and no luck.
Currently the text is Handling Fee: and I need it to say Shipping Insurance: - Please Help!
Here's the html output of the page;
<div class="c3 right">
<h2>Order Summary</h2>
<table class="styledtable">
<thead>
<tr>
<th>Quantity</th>
<th>Product</th>
<th>Price</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">1</td>
<td><strong>ACT Clutch Kit - Heavy Duty (HD) (DC1-HDSS)</strong>
<div class="order_item_notes">HD Clutch Kit<br/> Performance Street Disc (SS)</div>
<div class="order_item_specs">
Components : D018, 3001532, ATCP23<br/>
</div>
</td>
<td style="text-align:right">$314.25</td>
<td style="text-align:right">$314.25</td>
</tr>
<tr>
<td colspan="3" style="text-align:right">Items Total</td>
<td style="text-align:right">$<span id="itemstotal">314.25</span></td>
</tr>
<tr>
<td colspan="3" style="text-align:right">Shipping:</td>
<td style="text-align:right">$<span id="shippingtotal">TBD</span></td>
</tr>
<tr>
<td colspan="3" style="text-align:right">Handling Fee:</td>
<td style="text-align:right">$<span id="handlingfee">0.00</span></td>
</tr>
<tr>
<td colspan="3" style="text-align:right">Tax:</td>
<td style="text-align:right">$<span id="taxtotal">0.00</span></td>
</tr>
<tr>
<td colspan="3" style="text-align:right">Order Total:</td>
<td style="text-align:right">$<span id="total">0.00</span></td>
</tr>
</tbody>
</table>
<p>Upon checkout, you <strong>must enter</strong> your cars <strong>year, make and model</strong> into the comments section at the bottom of this page. <strong> We will not complete your order if we do not have this information!</strong></p>
<p> </p>
</div>
</div>
You can use a jQuery :contains selector:
$("td:contains('Handling Fee:')").text("Shipping Insurance:");
You can see it in action here: http://jsfiddle.net/cnhYj/
Update
in order to get it to work after the document is ready you can write it like that:
$(function() {
$("td:contains('Handling Fee:')").text("Shipping Insurance:");
});
$("td:contains('Handling Fee:').text('Shipping Insurance');
I don't see any javascript there just html
you want to turn this..
<td colspan="3" style="text-align:right">Handling Fee:</td>
into this...
<td colspan="3" style="text-align:right">Shipping Insurance:</td>

Categories