Highlight Row When the Checkbox Is True

Highlight row when the checkbox is true

I described in the answer one good way how you can implement the highlighting.

Version 4.3.2 of jqGrid has new feature - rowattr callback (see my original suggestion), which was introduced especially for cases like yours. It allows you to highlight some rows of grid during filling of the grid. To have the best performance advantage you should use gridview: true additionally. By the way I recommend you to use gridview: true in all jqGrids.

The usage of rowattr callback is very easy:

gridview: true,
rowattr: function (rd) {
if (rd.GroupHeader === "1") { // verify that the testing is correct in your case
return {"class": "myAltRowClass"};
}
}

The CSS class myAltRowClass should define background color of highlighted rows.

The corresponding demo you can find here:

Sample Image

Because in your demo you need just highlight and not select the rows I didn't used multiselect: true in my demo. In case of multiselect: true it works exactly in the same way.

At the end of my answer I would like to recommend you to use column templates. The feature will make your code shorter, more readable and easy to maintain. What you need to do is the following:

  • you can include common or the most frequently used settings from column definitions in cmTemplete. In your case it could be
cmTemplate: {align: 'center', sortable: false, editable: true, width: 80}
  • then you can define some variables which describe common properties which you use frequently in some columns. For example:
var myCheckboxTemplate = {formatter: 'checkbox', edittype: 'checkbox', type: 'select',
editoptions: {value: "1:0"}},
myTextareaTemplate = {edittype: "textarea",
editoptions: {size: "30", maxlength: "30"}};
  • after that you can use myCheckboxTemplate and myTextareaTemplate inside of colModel which will reduced in your case to the following
colModel: [
{name: 'TypeID', index: 'TypeID', width: 350, hidden: true, edittype: "select",
editoptions: {value: getTypeID()}, editrules: { edithidden: true}},
{name: 'Order1', index: 'Order1', template: myTextareaTemplate},
{name: 'Order2', index: 'Order2', template: myTextareaTemplate},
{name: 'Order3', index: 'Order3', template: myTextareaTemplate},
{name: 'Description', index: 'Description', width: 140, template: myTextareaTemplate},
{name: 'Notes', index: 'Notes', width: 120, template: myTextareaTemplate},
{name: 'Measure', index: 'Measure', template: myTextareaTemplate},
{name: 'UnitPrice', index: 'UnitPrice', width: 100, editable: false, template: myTextareaTemplate},
{name: 'Remarks', index: 'Remarks', width: 140, template: myTextareaTemplate},
{name: 'UnitCost', index: 'UnitCost', width: 100, template: myTextareaTemplate},
{name: 'Service', index: 'Service', width: 120, template: myTextareaTemplate},
//If the GroupHeader is true the row background is yellow
{name: 'GroupHeader', index: 'GroupHeader', width: 100, template: myCheckboxTemplate},
{name: 'IsGroup', index: 'IsGroup', template: myCheckboxTemplate}
],
cmTemplate: {align: 'center', sortable: false, editable: true, width: 80},

Highlight only the first row of a table where checkbox is checked

You are using a wrong closest() selector. .row is for searching a class name. You have to search for a element. Adding color to the <tr> element will color the selected row.

More information about the closest() function can be found here.

Example

$(".my-check").each(function(e, elem) {
$(elem).on("change", function() {
var sel = $(this);
var num = sel.data("number");
var co = sel.data("code");
if (sel.eq(0).is(":checked")) {
if (!$(".highlight")[0]) { // IF CLASS IS NOT FOUND --> Add class
sel.closest("tr").addClass("highlight");
}
$(".my-check:not([data-number=" + num + "])").prop("disabled", true);
$(".my-check:not([data-code=" + co + "])").prop("disabled", true);
} else {
if (!$(".my-check[data-number=" + num + "]:checked").length) {
$('table tr').removeClass("highlight");
$(".my-check").not(sel).prop("disabled", false);
}
}
});
});
.highlight {
background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Status</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
<td><input type="checkbox" class="my-check" data-number="7867" data-code="CB45" /></td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
<td><input type="checkbox" class="my-check" data-number="3307" data-code="HUNT1" /></td>
</tr>
<tr>
<td>Harry</td>
<td>Green</td>
<td>50</td>
<td><input type="checkbox" class="my-check" data-number="7867" data-code="CB45" /></td>
</tr>
<tr>
<td>Mark</td>
<td>Twain</td>
<td>94</td>
<td><input type="checkbox" class="my-check" data-number="5645" data-code="KLY" /></td>
</tr>
</table>

How to highlight entire row when the checkbox is selected

Well my approach with jQuery is this :

            <p:column >
<h:selectBooleanCheckbox onclick="highlight(this)" />
</p:column>

Create .highlighted class in your CSS file:

.highlighted {
background-color: red;
}

And finally actual function:

          function highlight(param) {  
var row = jQuery(param).parent().parent(); //children() available as well
row.toggleClass('highlighted');
}

You just get the row of the clicked checkbox and handle assigning of the CSS class. Straight and simple.

EDIT: Of course number of .parent() depends on your html elements composition. Edited the function to fit on your case, I've tried it with different composition of elements.

onChange Checkbox Highlight Row

use jquery..

And don't forget to add jquery cdn......

$("#checkboxSelect").click(function(){
if ($(this).is(":checked")){
$(this).parent().parent().removeClass("highlight")}
else{
$(this).parent().parent().addClass("highlight")}
});

That will do it...

Google Sheets highlight a column if a checkbox is checked and also matches an input, applied to the whole row

Try this formula for OR condition

=sumproduct($C$3:$H$3,$C6:$H6=$C$2:$H$2)

try this one for AND condition

=sumproduct($C$3:$H$3,$C6:$H6=$C$2:$H$2)=sumproduct(N($C$3:$H$3))

jquery highlight table row if checkbox is checked

You can use .each() [docs] and the :checked [docs] pseudo selector:

$('tr .checkboxes:checked').each(function() {
$(this).closest('tr').addClass('pinkrow');
});

If you want to use pure CSS selectors only, you can test whether an element is checked with the DOM element's checked property:

$('tr .checkboxes').each(function() {
if(this.checked) {
$(this).closest('tr').addClass('pinkrow');
}
});

Also note that tr .checkboxes only selects those .checkboxes elements, which are in a table row (in case there are others).



Related Topics



Leave a reply



Submit