Tips for managing embedded webgrid formatting

I have a sub Webgrid nested inside another webgrid that displays users per record...

<div id="grid">
        @grid.GetHtml(
            tableStyle: "webgrid",
            headerStyle: "webgrid-header",
            footerStyle: "webgrid-footer",
            alternatingRowStyle: "webgrid-alternating-row",
            selectedRowStyle: "webgrid-selected-row",
            columns: grid.Columns( grid.Column("BlogBlogId", "BlogId ",style: "BlogBlogId"),
                                    grid.Column("Domain", "Domain",style: "Domain"),
                                    grid.Column("Path", "Path",style: "Path"),
                                    grid.Column("BlogUsers", "Blog Users", style: "BlogUsers", format : (item) =>
                                    {
                                        string temp = "<div style='text-align:left;height:100%;" + "width: 100%; overflow: auto; overflow-x:hidden;'>";
                                        var sub = new WebGrid(item.BlogUsers,
                                           canPage: false,
                                           ajaxUpdateContainerId: "sub",
                                           ajaxUpdateCallback: "jQueryTableStyling",
                                           defaultSort: "BlogBlogId", 
                                           rowsPerPage: 100);
                                           temp += sub.GetHtml(htmlAttributes: new { id = "tblsubWebGrid" },
                                           columns:sub.Columns(
                                                    sub.Column("UserLogon", "Logon"),
                                                    sub.Column("Adstatus", "AdStatus")
                                                   )
                                            );
                                        temp += "</div>";
                                        return new HtmlString(temp);

                                    }),
                                    grid.Column("Archived", "Archived",style: "Archived"),
                                    grid.Column("Deleted", "Deleted",style: "Deleted"),
                                    grid.Column("Allusersvalid", "All Users Valid?",style: "User IP")


                )
             )
    </div>

A change in text color is applied based on the status of the user...

<script>
    function styleGrid() {
        $("#tblsubWebGrid tr:not(:first)").each(function () {
            var aptStatus = $(this).find("td:nth-child(2)").html();
            if (aptStatus == 'User Exists') {
                $(this).find("td:nth-child(2)").addClass("clsGreen");
            } else {
                $(this).find("td:nth-child(2)").addClass("clsRed");
            }

        });
    }

    $(document).ready(function() {
        styleGrid();
    });
</script>

ISSUE: The classes are only being applied to the first grid record with sub webgrid....

Query... 1. Is my issue due to multiple instances of table id="tblsubWebGrid"? 2. Does my jQuery script need to be modified so that it applies to all instances of these sub webgrids?

Answer №1

UPDATE: I made a slight adjustment to my Jquery code by removing the ":not(:first)" from "tblsubWebGrid tr", which resulted in successfully appending the CSS class references.

<script>
    function customStyle() {
        $("#tblsubWebGrid tr").each(function () {
            var status = $(this).find("td:nth-child(2)").html();
            if (status == 'User Exists') {
                $(this).find("td:nth-child(2)").addClass("clsGreen");
            } else {
                $(this).find("td:nth-child(2)").addClass("clsRed");
            }

        });
    }

    $(document).ready(function() {
        customStyle();
    });
</script> 

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

Having trouble with PHP upload to server file function

This code seems to be error-free, but unfortunately it is not inserting the file into the database and destination folder upon clicking the upload button. HTML Form <html> <body> <form action="includes/parts-cat/zip ...

When the parent container is given a specific height, the Flexbox item on iOS 9 appears to be shrunk down

Encountering a bug on iOS version 9 while using the latest Bootstrap 4 Beta. The issue involves a parent div and ul element with multiple list items. The parent div is a flexbox with set dimensions and overflow enabled for child elements. The list item sho ...

Adjustable Panel Width

Is there a way to have the width of the bottom panel expand to col-md-12 when the top panel is collapsed, and then return back to col-md-8 when the user expands the top panel again? I'm still learning, but here's what I have managed to code so f ...

Is there an alternative function that can be used in place of live() in jQuery version 1.26?

$('#data').find('div:eq(0)').append('<span class="show"> | show more</span><span class="hide"> | hide content</span>') $('div:gt(0)').hide(); $('.show, .hide').on('click', ...

I'm trying to understand why the combination of boostrap 5 mx-auto d-block is not effectively centering a fluid image. Can anyone

My code seems to be very simple, yet I am puzzled as to why it is not working. I have removed any external CSS from the page that could potentially cause interference, but the image still refuses to center inside my column. I have searched extensively and ...

How to eliminate excess white space on the right side in Bootstrap 4

Could someone please clarify why I am experiencing a white space on the right side when using Bootstrap 4? This is the primary code block for the page. <main id="intro" role="intro" class="inner text-center"> <h2>Lorem Ipsum</h2> ...

Combining CSS and JavaScript to handle two events with a single onClick

Here is the code I've created: <a href="#" id="home" class="showLink" onclick="showHide('example');return false;"> <li class="buttons">home</li> </a> <a href="#" id="user" class="showLink" onclick="showHide(&a ...

Personalized jQuery mask customization

After experimenting with a masking function that conceals numbers based on a specific pattern, I am now looking to make it more flexible for users. For instance, if a user types 123456789, the field will display as XXX-XX-6789. Currently, the pattern is ...

Dealing with timeouts in getJSON requests

When using the jQuery getJSON() function, data is retrieved smoothly most of the time. However, there are instances where the loading process seems to be stuck, displaying "loading loading loadin" at the center of the page. While the ajax() function has ...

Showing a custom PHP field only when it contains a value

Exploring the realm of php, I am currently diving into the Advanced Custom Fields plugin for WordPress. Below is the snippet of php code I am working with: <?php $group_ID = 110; $fields = array(); $fields = apply_filters('acf/field_group/get_fi ...

Tips on choosing the initial h4 element within the same parent element but at different levels of depth

Consider the following code snippet: <div class="main-element"> <h4>Title 1</h4> <ul> <li></li> <li></li> <li> <h4>Title2</h4> & ...

The min-height attribute of the tooltip functions, but the min-width attribute does

I found a helpful tooltip on this site: I customized it to work with an input element: <div class="tooltip"> <input type="text" placeholder="Name" name="dp_name"> <span>Full name asd as dasd as dasd</span> </div> ...

I prefer to avoid using the "#" sign in URLs

<a href="#" onClick="load_page()">intro</a> I am trying to avoid displaying the # sign in the URL, and I would like it to appear like this instead: www.mydomain.com/ However, it currently displays as follows: www.mydomain.com/# Is there a ...

When attempting to add an option to the fieldset in jtable and Java servlet, the entire column fails to display

Just started using jQuery jTable. I needed to include a dropdown menu in one of my jTable fieldset. However, after adding the dropdown feature which retrieves data from the database through my servlet, the entire column disappeared. I've attached a s ...

Utilize CountUp.js to generate a dynamic timer for tracking days and hours

I am looking to create a unique counter similar to the one featured on this website https://inorganik.github.io/countUp.js/ that counts up to a specific number representing hours. My goal is to display it in a format such as 3d13h, indicating days and hour ...

Utilizing jQuery for cloning elements

I need to add functionality for adding and deleting rows in multiple tables on my website. Currently, I am doing this by directly inserting HTML code using jQuery, but it has become inefficient due to the number of tables I have. I am considering creating ...

When jQuery adds new elements, their CSS may not be applied accurately

One issue I encountered is using jQuery to dynamically add new elements, only to find that the CSS isn't being applied correctly to the newly added element. To illustrate this problem, I created a jsFiddle. In the example, there are differences in sp ...

How to select specific elements with jQuery nth-child selector without skipping any

As a newcomer to jquery, I may not be able to grasp more advanced concepts right away. However, here's my issue: when attempting to select the nth-child(1), it correctly chooses the first item (I realize I could use first child, but I prefer not to). ...

"Utilize Javascript to upload a picture and showcase it on your website

<!DOCTYPE html> <html> <head> <title>Unique Webpage!</title> <meta charset=utf-8 />                       <link rel="stylesheet" href="styles/customcss.css" /> <script src="j ...

An issue with the blog tag occurs immediately following the if statement

I am working on rendering three columns per row in my HTML file with the help of Django2.1 and Bootstrap4. Here is a snippet of the HTML code I am using: <main class="container" role="main"> {% for treasure in treasures %} {% block row ...