Add a filter and set a background color on the rows in jqGrid

After analyzing the code and output in firebug, I can filter the td and assign a different background color based on certain conditions.

Here is the code snippet:

loadComplete: function() {
            var i, names=this.p.groupingView.sortnames[0], l = names.length;
            for (i=0;i<l;i++) {
                if (names[i]==='envVariable') {
                    $(this).jqGrid('groupingToggle',this.id+"ghead_"+i);
                } else {
                    // hide the grouping row
                    $('#'+this.id+"ghead_"+i).hide();
                }
            }
            var getColumnIndexByName = function(grid, columnName) {
                var cm = grid.jqGrid('getGridParam','colModel'),i=0,l=cm.length;
                for (; i<l; i++) {
                    if (cm[i].name===columnName) {
                        return i; // return the index
                    }
                }
                return -1;
            };

            var iCol = getColumnIndexByName($(this),'isEqual'),
            cRows = this.rows.length, iRow, row, className;
            for (iRow=0; iRow<cRows; iRow++) {
                row = this.rows[iRow];
                className = row.className;
                if ($.inArray('jqgrow', className.split(' ')) > 0) { 
                    console.info(row.cells[iCol]);
                                      
$(row.cells[iCol])
                    .filter("false")
                    .css("background", "#c8ebcc",
                            "background-color", "#DCFFFF",
                            "background-image", "none");

                }
            }

        }

**Updated**

@Oleg: To achieve the functionality of hiding rows where isEqual is true and changing background color for rows where isEqual is false, I modified your code. However, it does not hide the rows as expected. Can you help me figure out where I went wrong?

var i, l, data = this.p.data, rows = this.rows, item;

            l = data.length;
            for (i=0;i<l;i++) {
                item = data[i];
                if (!item.isEqual) {

                    $(rows.namedItem(item._id_))
                    .css({
                        "background-color": "#DCFFFF",
                        "background-image": "none"
                    });
                }
                else
                {
                    $(rows.namedItem(item._id_)).hide();
                }

            }

Answer №1

It appears that your current query is a continuation of your previous inquiry. When working with local data, accessing all grid data is quite straightforward. Begin by defining an additional hidden grid column:

{ name: 'isEqual', index: 'isEqual', width: 100, hidden:true }

Then include the following code snippet within the loadComplete:

var i, l, data = this.p.data, rows = this.rows, item;

l = data.length;
for (i=0;i<l;i++) {
    item = data[i];
    if (!item.isEqual) {
        $(rows.namedItem(item._id_))
            .css({
                "background-color": "#DCFFFF",
                "background-image": "none"
            });
    }
}

This will yield the desired results.

Check out the demonstration here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to retrieve XML data using jQuery AJAX

I am facing an issue with returning an xml file from the server to the client and parsing it using jQuery's ajax function. The code snippet is as follows: Client: $("#submit").click(function(){ $.ajax({ type: "POST", ...

Images on web pages are automatically resized to fit into a smaller preview window

Currently, I am facing an issue with the display of images in my grid windows on my website . The images are appearing as partial representations instead of rescaled versions where the whole picture is resized to fit the grid window. I have attempted mod ...

What is the best way to extract the data parameter from an Ajax request in a Restful web service?

When making an AJAX call to a Restful web service, I encounter a problem where the data parameter passed is null. Here is the code snippet: var b2bValues = "Some String"; var lookupUrl = "http://ip:{port}/ImageViewer/rest/ImageServlet/callrest"; var l ...

Adding a background image to a box in MUI is a simple task that can enhance

I've been attempting to include a background image in the Box component of mui, but I can't seem to get it to work. Here is the code I've been using: const Main = () => { return ( <Box sx={{backgroundImage:'images/cove ...

Submitting an intricate item to a WCF REST API using JQuery

Currently, I am attempting to send a complex object to my WCF REST service. Utilizing this method seems to be the most straightforward way to upload a Stream type object along with other parameters to an endpoint simultaneously. Service: [OperationContra ...

Is there a method to ensure that the window event always gets triggered before any other events?

Is there a way to make the window event trigger first when clicking on #myDiv? (function ($, w, d) { $(d).ready(function () { $(w).click(function() { alert('window has been clicked'); }); $('#myDiv').cl ...

jQuery for validating input fields with positive integers only using regular expressions

I currently have two input fields in my HTML that look like this: <input type="text" class="txtminFeedback" pattern="^\d+([\.\,][0]{2})?$" placeholder="Minimum Feedback"> <input type="text" class="txtmaxFeedback" pattern="^\d ...

Capturing the Following and Previous Actions

I am looking for a solution to reload the calendar when the next or previous buttons are clicked. Currently, the calendar loads correctly for one day but does not refresh when navigating to the next or previous days. EDIT: var Calendar = function () { ...

Tips for creating a script that compiles all SCSS files into CSS within a Vue 3 project

Within my project, there exists a file named index.scss located in the src/assets/styles directory. Adjacent to this file are multiple folders housing SCSS files that are ultimately imported into index.scss. My objective is to devise a script within the pa ...

Using Jquery to hide or show objects within a <div> when clicked

My goal is to create a webpage that displays three different contents based on which button is clicked. Below is the code for reference: I want the page to show three specific sections: A search bar only when the 'search' button is clicked, T ...

Validate if the JSON data array contains any elements

I'm currently working with an ajax code and I need to determine whether the JSON data contains an array or not. However, despite my attempts, I am still receiving incorrect output. $.ajax({ url: url, type: 'POST', da ...

Output based on conditional regex in Jquery

Can I use regex and jQuery to dynamically replace numbers within square brackets with images based on the number? For example: [1] would be replaced with <span><img src="1.png"></span> If we have [11], it should turn into <span>& ...

Braintree drop-in feature now allows for automatic disabling of the submit button while the transaction

I've been struggling with a seemingly simple task that I just can't seem to figure out. I'm using Braintree's dropin UI and I have a submit button that I need to disable while the processing is happening, but I can't seem to find t ...

Unable to set $_POST value when using $.ajax post request

I am a beginner in the world of PHP and JavaScript, and I have encountered an issue where I need to make changes to values in an XML document that has been read in. Specifically, I have an HTML Select element that was dynamically generated by PHP code. fu ...

Having trouble with replacing an entire div selector with a new div after using Jquery's .html method?

I've searched high and low, but can't find a reference. However, this should be obvious. Two select lists If the result of the first list is Australia, then keep the second list. If it's not Australia, replace the second list with an inpu ...

The attempt to run 'setProperty' on 'CSSStyleDeclaration' was unsuccessful as these styles are precalculated, rendering the 'opacity' property unchangeable

I am attempting to change the value of a property in my pseudo element CSS class using a JavaScript file. Unfortunately, I keep encountering the error mentioned in the title. Is there any other method that can be used to achieve this? CSS Code: .list { ...

The importance of scope in ng-style

I am attempting to dynamically change the font color in an input field based on the value entered into that field. However, I have encountered an issue with using ng-style as it is not recognizing the value of the scope and therefore the color does not upd ...

Transfer information from one input field to another and then proceed to submit the form

I need to move the content of an input field that is located higher up the page to a form field at the bottom of the page and then submit the form. Below is what I have attempted so far, but it is not working: $('#move_down').click(function() ...

The map fails to load on the HTML page

I am struggling to load a map into my HTML page from a file located in the app folder. Despite using the correct code for inserting the map, it still does not load properly. <!-- Contact section start --> <div id="contact" class="contact"> ...

Dropdown submenu display issue with multiple selections

I've been working on a multi-level dropdown menu. It seems like there's a dropdown menu inside another dropdown menu. However, when I click on the inner dropdown menu, it doesn't open to the right as expected; instead, it opens below the men ...