Button inside clickable row is activating the row highlight - javascript

I have a Bootstrap table that is just a clickable row. I put a button in that row which is going to be a href to another location. When I click on the button, the row is activated and gets highlighted.
I am reading that I need to stop pagination use jQuery however I am not sure how to do that. This is my current jQuery that is used to toggle the clickable row.
$(document).ready(function() {
$('.myTable').on('click', '.clickable-row', function(event) {
$(this).toggleClass('table-warning');
});
});
I am not sure what needs to be added so that my row does not get highlighted when click on the button
EDIT:
<table class="table table-hover" id="myTable">
<thead>
<tr class="clickable-row">
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr class="clickable-row">
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>#mdo</td>
</tr>
<tr class="clickable-row">
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>#fat</td>
</tr>
<tr class="clickable-row">
<th scope="row">3</th>
<td colspan="2">Larry the Bird</td>
<td><a class="btn btn-primary" href="#" target="_blank" role="button">Link</a></td>
</tr>
</tbody>

You may have read that you need to stop propagation, not pagination. To stop propagation means to prevent further propagation of the current event in the capturing and bubbling phases. Applied to your example, it means that a click on the button in the table won't be also counted as click on the row. More info on stopPropagation(). Note that I've also added event.preventDefault(); in the example as it would result in opening a broken link. Omit this in your real code when you add a link address to the button.
$(document).ready(function() {
$('#myTable').on('click', '.clickable-row', function(event) {
$(this).toggleClass('table-warning');
});
$('#myTable').on('click', '.btn', function(event) {
event.preventDefault();
event.stopPropagation();
$(this).parent("td").toggleClass("clicked");
});
});
.table-warning {
background-color: yellow;
}
.clicked {
background-color:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-hover" id="myTable">
<thead>
<tr class="clickable-row">
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr class="clickable-row">
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>#mdo</td>
</tr>
<tr class="clickable-row">
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>#fat</td>
</tr>
<tr class="clickable-row">
<th scope="row">3</th>
<td colspan="2">Larry the Bird</td>
<td><a class="btn btn-primary" href="#" target="_blank" role="button">Link</a></td>
</tr>
</tbody>
</table>

Related

I need to check if any <td> in a html table is empty/null as in after the page loads it only returns <td></td> and change it to <td><p>$0</p></td>

example of the table
<table class="tg">
<thead>
<tr>
<th class="tg-0lax" id="blank-spaces"></th>
<th class="titles" id="this">????</th>
<th class="titles">???<br></th>
<th class="titles">???</th>
<th class="titles">???</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>not empty do nothing</td>
<td></td>
</tr>
</tbody>
<table>
Now the way this is really written, data will be pushed into each td from an API, some times that API is down, and I would like to use jquery to check if a td has anything displaying in it and if it doesnt I want there to be a string with an error message in the td. This is the jquery im trying currently
var empty = $("td").trim().filter(function () { return this.value.trim() === null })
empty.addClass("errorDefault");
if ($("td").hasClass("errorDefault")) {
this.val("$0");
this.text("$0");
this.html("<p>There was an error getting data</p>");
}
There is no .trim() in jQuery
string trim() is not going to return null.
table cells do not have value
$("td").hasClass("errorDefault") only looks at first element
$("tbody td")
.filter((_, td) => !td.textContent.trim().length)
.addClass("errorDefault")
.text("$0");
.errorDefault {
background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="tg">
<thead>
<tr>
<th class="tg-0lax" id="blank-spaces"></th>
<th class="titles" id="this">????</th>
<th class="titles">???<br></th>
<th class="titles">???</th>
<th class="titles">???</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>not empty do nothing</td>
<td></td>
</tr>
</tbody>
<table>
If it is truly empty, CSS can do it.
tbody td:empty{
background: red;
}
tbody td:empty:after {
content: "$0";
}
<table class="tg">
<thead>
<tr>
<th class="tg-0lax" id="blank-spaces"></th>
<th class="titles" id="this">????</th>
<th class="titles">???<br></th>
<th class="titles">???</th>
<th class="titles">???</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>not empty do nothing</td>
<td></td>
</tr>
</tbody>
<table>

Delete row from table using button - Bootstrap Table

I'm trying the row to be deleted when I click the delete icon in the table using bootstrap table.
My table:
<table id="checkDataTable">
<thead style="color: #31708f; background-color: #d9edf7 ">
<tr>
<th data-field="AssetReference">Referans</th>
<th data-field="Customer">Müsteri</th>
<th data-field="ReceiverCompanyName">Alici Firma</th>
<th data-field="Tools" data-formatter="StatuFormatter">Islem</th>
</tr>
</thead>
</table>
Javascript:
function StatuFormatter(value, row, index) {
return "<a onclick='removeAssetEconomyCheckBillOfLadingRow()'><i class='fa fa-trash fa-2x' aria-hidden='true'></i></a>";
}
function removeAssetEconomyCheckBillOfLadingRow(id) {
alert(id);
}
It's fine until the removeAssetEconomyCheckBillOfLadingRow function.
How can I delete inside this function?
Please try below:
$(document).on('click', 'button.deleteRow', function(event) {
$(this).closest("tr").remove();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="checkDataTable" class="table">
<thead style="color: #31708f; background-color: #d9edf7 ">
<tr>
<th data-field="AssetReference">Referans</th>
<th data-field="Customer">Müşteri</th>
<th data-field="ReceiverCompanyName">Alıcı Firma</th>
<th data-field="Tools" data-formatter="StatuFormatter">İşlem</th>
</tr>
</thead>
<tobdy>
<tr>
<td>Data1</td>
<td>Data1</td>
<td>Data1</td>
<td><button type="button" class="deleteRow">
Delete
</button></td>
</tr>
<tr>
<td>Data2</td>
<td>Data2</td>
<td>Data2</td>
<td><button type="button" class="deleteRow">
Delete
</button></td>
</tr>
<tr>
<td>Data3</td>
<td>Data3</td>
<td>Data3</td>
<td><button type="button" class="deleteRow">
Delete
</button></td>
</tr>
</tobdy>
</table>
First of all, you need to set id to to identify the deleting row.
<tr id="1"></tr>
And get tr and remove
function removeAssetEconomyCheckBillOfLadingRow(id) {
$('#checkDataTable').row($('#{{id}}'))).remove().draw();
}

How can I identify an element with no class/ID and won't always be the same nth child?

I've got some text displaying in a table (oldschool, I know) and I'm trying to identify that specific <td> element so I can use jQuery to wrap() <a href> tags around it and convert it to a link.
The problem is, none of the <td>'s in the table have unique classes or ID's, and there will always be an unknown amount of <td>'s before the one I want to access, so I don't think I can use nth of child.
The ONLY unique way that <td> is identifiable is the <td> DIRECTLY before it, which will contain some text that will always be the same. Can I use jQuery to find that <td> based on the text inside it, then target the <td> directly after that? Or is there a better way to do this?
You can use jQuery to fetch element that contains specific text and access the next td as required with a single line of jQuery code. This won't thrown an exception in case when there is no next td.
$(document).ready(function() {
var yourVal = $('td:contains("2.2")').next('td').text();
console.log(yourVal);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table class="table">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>1.1</td>
<td>1.2</td>
<td>1.3</td>
<td>1.4</td>
</tr>
<tr>
<td>2.1</td>
<td>2.2</td>
<td>2.3</td>
<td>2.4</td>
</tr>
<tr>
<td>3.1</td>
<td>3.2</td>
<td>3.3</td>
<td>3.4</td>
</tr>
</tbody>
</table>
You are looking for the nextElementSibling of the <td> with unique textContent. In order to find it, loop over all the <td>s and then get the nextElementSibling of the <td> with unique textContent. And when you find it, break.
const tds = document.querySelectorAll("td")
for (let td of tds) {
if (td.innerText.includes("Larry")) {
const element = td.nextElementSibling
console.log(element.innerText)
break;
}
}
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>#mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>#fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>#twitter</td>
</tr>
</tbody>
</table>
If you like jQuery, use this.
const td = jQuery("td:contains('Larry')").next("td").text()
console.log(td)
<script src="https://cdn.jsdelivr.net/npm/jquery#3.5.1/dist/jquery.min.js"></script>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>#mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>#fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>#twitter</td>
</tr>
</tbody>
</table>

Disable button if a specific column has a certain value

So what I'm currently trying to do know is if a column has a value of "TO BE CHECK" the "Print" button will be disabled.
Can someone help me with this?
Code is attached below.
<form action="" method="POST">
<table id="MyTable" class="table">
<thead>
<tr>
<th scope="col">Food Lane Sticker Control Number</th>
<th scope="col">OR/CR #</th>
<th scope="col">Business Permit #</th>
<th scope="col" id="test">Status</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody name="MyTable" id="tbody">
<tr name="MyTable">
<th name="MyTable"></th>
<th name="MyTable"></th>
<th name="MyTable"></th>
<th name="MyTable">TO BE CHECK</th>
<td name="MyTable">
<button type="submit" id="print" name="print" class="btn btn-success">Print</button>
</td>
</tr>
</tbody>
</table>
</form>
With JQuery, you could do
$('#MyTable th[name="MyTable"], #MyTable td[name="MyTable"]').each( function(i,e){
if (e.innerHTML === "TO BE CHECK") {
$('#Print').prop('disabled', true);
}
} );
disabled onclick="addtocart()" />

Button with link inside Bootstrap accordion table

I have a Bootstrap accordion table that works perfectly. Basically, it shows more content if someone clicks a row. Now, the problem is that one of the rows contains a button link and when someone clicks that button, it goes to that link correctly, but it also opens the hidden row content.
How do I make it so that when someone clicks on the button, it doesn't open the hidden row as well and goes to the link only?
<table class="table">
<thead>
<th width="20%" class="">Row 1</th>
<th width="20%" class="">Row 2</th>
<th width="20%" class="">Row 3</th>
<th width="20%" class="">Row 4</th>
<th width="20%" class="">Row 5</th>
</thead>
<tbody>
<tr data-toggle="collapse" data-target="#123" class="accordion-toggle">
<td class="">Content 1</td>
<td class="">Content 2</td>
<td class="">Content 3</td>
<td class="">Content 4</td>
<td class="">Link</td>
</tr><tr>
<td colspan="5" class="hiddenRow"><div class="accordian-body collapse" id="123">More Content</div> </td>
</tr>
</tbody>
</table>
Try something like this:-
Link
<script type="text/javascript">
function stopEventPropogation(e){
e.stopPropogation()
}
</script>
OR as suggested by Bart this can also be used:-
$('.accordion-toggole').on('click', '.btn', function(e) { e.stopPropagation(); })

Categories

Resources