Home > Article > Web Front-end > How to Highlight Rows in a JqGrid Based on Checkbox Status?
Highlight Rows When Checkbox is True
In your jqGrid project, you aim to highlight rows based on the status of a checkbox.
Rowattr Callback
Version 4.3.2 of jqGrid introduces the rowattr callback, which allows you to add custom attributes to rows during the grid fill process. This is a suitable approach for row highlighting.
rowattr: function (rd) { if (rd.GroupHeader === "1") { // Verify this logic based on your data structure return {"class": "myAltRowClass"}; } }
CSS Class for Highlighting
Define a CSS class myAltRowClass that sets the background color for highlighted rows.
Multiselect and gridview
For row highlighting without row selection, omit multiselect: true. To enhance performance, enable gridview: true.
Usage of Column Templates
To streamline your code, you can utilize column templates:
cmTemplate
cmTemplate: {align: 'center', sortable: false, editable: true, width: 80}
Custom Templates
Create custom templates for commonly used properties:
var myCheckboxTemplate = { formatter: 'checkbox', edittype: 'checkbox', type: 'select', editoptions: {value: "1:0"} }; var myTextareaTemplate = { edittype: "textarea", editoptions: {size: "30", maxlength: "30"} };
Optimized colModel
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}
The above is the detailed content of How to Highlight Rows in a JqGrid Based on Checkbox Status?. For more information, please follow other related articles on the PHP Chinese website!