The CSS styles within the GridView are not appearing on the page after an AJAX update

In the context of a gridview, I have implemented a CSS-based inline graph. Initially, everything functions as expected until a filter is applied and the gridview undergoes an update. At that point, the CSS styling ceases to apply within the grid. Has anyone encountered this issue before and found a solution? To be honest, delving into CSS troubleshooting is not my forte.


This is the element prior to the AJAX update:


This is after the AJAX update:

.stat-block .stat-graph {
background: none repeat scroll 0 0 #FFFFFF;
border: 1px solid #D7D7D7;
border-radius: 3px;
margin-right: 10px;
padding: 10px 10px 8px;
text-align: center;
width: auto;

}

Upon initial generation of the grid, it seems that the CSS generates a canvas tag like so:

<canvas style="display: inline-block; width: 29px; height: 20px; vertical-align: top;" width="29" height="20"></canvas>

However, following the AJAX update and refresh of the gridview, that canvas tag no longer appears.

I attempted to place the graph data inside the canvas tag with no success.

Below is the gridview code snippet:

this->widget('zii.widgets.grid.CGridView', array(
'id' => 'cartuse-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'afterAjaxUpdate' => 'reinstallDatePicker',
'columns' => array(
    array(
        'id' => 'autoId',
        'class' => 'CCheckBoxColumn',
        'selectableRows' => '50',
    ),
    //  'id',
    array(
        'name'=>'client',
        'type'=>'raw',
        'value'=>'client($data->client)',
        'htmlOptions' => array(
            'align'=>'center',
            //'width'=>'35%'
        )

Here is the client function utilized:

       function client($client)   {     

...

return '<div class="stat-block" id="graph">
            <ul>
                <li class="stat-graph inlinebar" id="activitate-lunara">
                '.$data.'
                </li>
                <li class="stat-count">
                    <span>'.$data['0'].'
                    </span>       
                </li> 
                <li class="stat-percent">
                    <span class="text-info stat-percent">'.$target.'</span>
                </li>               
            </ul>
        </div>';
}

Edit 1: Following the recommendation from answer 1, I attempted to use the removeClass() and addClass() functions in order to refresh the CSS post-AJAX update. Unfortunately, this did not yield any changes. The canvas tag continues to remain absent.

Another approach was to utilize replaceWith() to directly insert the canvas tag, but this caused issues with filtering functionality.

Here's the reinstallDatePicker function:

<?php Yii::app()->clientScript->registerScript('re-install-date-picker', "
function reinstallDatePicker(id, data) {
   $('#datepicker_min').datepicker({ dateFormat: 'yy-mm-dd',
                                      showOtherMonths: true,
                                      selectOtherMonths: true,
                                      changeYear: true,
                                      changeMonth: true,
                                      });
   $('#datepicker_max').datepicker({ dateFormat: 'yy-mm-dd',
                                      showOtherMonths: true,
                                      selectOtherMonths: true,
                                      changeYear: true,
                                      changeMonth: true,
                                      });
   $( \"#activitate-lunara\" ).removeClass( \"stat-graph inlinebar\" );
   $( \"#graph\" ).removeClass( \"stat-block\" );
   $( \"#graph\" ).addClass( \"stat-block\" );
   $( \"#activitate-lunara\" ).addClass( \"stat-graph inlinebar\" );

}"); ?>

Edit 2:

Prior to this, I had not been utilizing renderPartial for column content, instead relying on a function to return the desired content. After exhausting various options, I transitioned to renderpartial. By employing renderpartial along with registering scripts/CSS in the partial view and using removeClass/addClass methods, I was able to resolve the issue successfully.

Answer №1

There are times when I encounter this issue as well.

One thing to keep in mind is that if you're using partial views, make sure to include the CSS within the partial view itself.

If not, it may be necessary to reapply the styling after each ajax update.

I noticed that you have included

"afterAjaxUpdate"=>"reinstallDatePicker"
, so one solution could be to add the CSS to .stat-block .stat-graph within that function. You can utilize jquery functions such as css(), addClass(), and others.

The key point here is to ensure that you style your elements following every ajax update. As the function reinstallDatePicker is triggered after each ajax update, you can simply insert some code within this function to achieve the desired styling (i.e., restyling the elements).

Answer №2

I have exhausted all my options without success. The only method that has worked for me is storing the filter data in my session and triggering a reload after ajax update. However, this approach is not optimal as it may slow down the page if queries are not optimized.

I am sharing this here in case someone else encounters the same issue and wants to find a workaround before finding a proper solution.

In my controller, I implemented the following:

public function actionAdmin()
{
    $model=new Cartuse('search');
    $model->unsetAttributes();  // clear any default values
    if(isset($_GET['Cartuse']))
                $_SESSION['filterData'][$this->id][$this->action->id] = $_GET['Cartuse'];
                $model->attributes=$_SESSION['filterData'][$this->id][$this->action->id];
    if (isset($_GET['pageSize'])) {
        Yii::app()->user->setState('pageSize',(int)$_GET['pageSize']);
        unset($_GET['pageSize']);
    }
    $this->render('admin',array(
        'model'=>$model,
    ));
}

In my gridview setup:

$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'cartuse-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'afterAjaxUpdate' => 'reinstallDatePicker',
    'columns' => array(/*
            .....
        )
));

For the datepicker functionality, I had to create a separate function to ensure proper registration:

<?php Yii::app()->clientScript->registerScript('re-install-date-picker', "
    function reinstallDatePicker(id, data) {
            location.reload();
       $('#datepicker_min').datepicker({ dateFormat: 'yy-mm-dd',
                                          showOtherMonths: true,
                                          selectOtherMonths: true,
                                          changeYear: true,
                                          changeMonth: true,
                                          });
       $('#datepicker_max').datepicker({ dateFormat: 'yy-mm-dd',
                                          showOtherMonths: true,
                                          selectOtherMonths: true,
                                          changeYear: true,
                                          changeMonth: true,
                                          });
       $.datepicker.setDefaults($.datepicker.regional['ro']);
     }
");

This workaround is not a definitive solution, just a temporary fix. I am open to suggestions for a more permanent resolution.

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

Looking to ensure that a div with no content adjusts to the height of the neighboring div?

Currently working on a Reactjs project. I'm trying to set the height of the colorTab div to match that of the content div, while making it responsive. The height of content should adapt dynamically based on the text content in title and description, w ...

The script functions perfectly on one page, but encounters issues on another page

I'm currently working on developing an iOS7 WebApp using a template that I came across on this website: After writing some JavaScript/jQuery to create a fading effect for a picture and a toolbar, and testing it successfully on a blank page, I encount ...

Tips on customizing the appearance of React rendering components

<div> <h3>{this.props.product.name}</h3> <h3>{this.props.product.code}</h3> {this.renderColors()} <article> <div da ...

Alter the background color of a table cell in Angular HTML depending on a boolean value

Attempting to use Angular ng-class to set the background color of a table cell. I'm referencing code snippets from these resources: Angular: How to change the color of cell table if condition is true Change HTML table cell background color using ...

Using Laravel to extract JSON data from a table column

Within my Laravel application, there is a table called 'doors' which contains a column named 'darha'. I have stored JSON data using the serialize() method in this column. Each row in the table has a 'darha' column cell with da ...

Stretch a component outside of the bootstrap container

Currently, I am working on a mockup using Bootstrap 4 and have encountered an unusual element inside the container where the background extends beyond the container. I'm not sure how to address this issue effectively. I attempted to use position: abso ...

refreshing treeview without reloading the page

In my implementation, I've included an UpdatePanel that contains a TabContainer, which in turn houses a Panel. Within this panel, there is a TreeView that I must maintain the order of. Now, going back to the contents of the UpdatePanel, there's a ...

What is the best way to determine if a user is logged in on one tab but not on another tab within the same browser session?

GitHub has recently introduced a new functionality where if you are browsing a page as a guest in one tab and then log in from another tab, the tab where you are still a guest will show a specific message. This feature also functions in incognito or privat ...

Exploring the world of Single Page Applications and their impact on Open

As I develop a Single Page Application using underscore templating, the focus is on searching for and rating music albums through an AJAX call. However, a challenge arises when Facebook open graph metatags cannot be modified dynamically and the URL remains ...

Is there a way to create a gradual fading effect on my images?

Check out the images currently on my portfolio page: .collection-type-gallery #slideshow .slide img:hover { opacity: 1; -webkit-transition: opacity 1s ease-in-out; -moz-transition: opacity 1s ease-in-out; -o-transition: opacity 1s ease ...

How can I refresh the container with a new version of the file using PDFObject?

I'm having trouble with a button that generates a PDF and then reloads the PDF in the browser by replacing the container it's in, but with a different link. Despite my efforts, the new PDF does not show up and the old one remains even after refre ...

Is there a way to adjust paragraph sizes in CSS flexbox so they scale with the font size?

After spending hours trying to figure this out on my own, I finally caved and registered on StackOverflow. Despite Google providing plenty of related questions and examples, I still can't find a solution to my specific problem. The issue at hand is c ...

Tips for referencing a string in JavaScript

I am trying to use the showmodal method, but I keep getting an error when passing a string. It works fine with integers, but how can I pass a string in JavaScript? <script> var table = ' <table id="example" class="table table-striped " w ...

Achieve a consistently displayed shopping cart icon on the right side of the menu with Bootstrap

I have attempted to ensure that the shopping cart icon always appears on the right side of the screen for mobile, tablet, and desktop users using Bootstrap. However, I am facing issues with responsiveness. How can I ensure that the shopping cart icon alway ...

Is it advisable to turn off CSRF verification?

I'm currently working on an online quiz application using Laravel. One of the features I have implemented is the ability for users to resume their sessions during the quiz. This means that if a user reloads the page while taking the quiz, their sessi ...

Disabled text selection in Internet Explorer

I am in need of making one HTML element (Label) unselectable for IE. I have tried using the following methods: Unselectable=on onselectreturn=false; Unfortunately, none of these solutions are working for me. For Firefox and Chrome, I've successful ...

Guidelines on managing two sets of JSON data sent via AJAX to a PHP script

Embarking on my journey into the realm of JSON/AJAX for the first time. Essentially, I am tasked with sending 2 sets of data to a PHP file. One set will be saved in a MySQL table, while the other is utilized by the PHP script. My Javascript generates 2 s ...

How can you efficiently send JSON data from Ajax to a Servlet in a Java environment?

I am encountering a perplexing issue with sending data from my jQuery script to a servlet using AJAX. The strange part is that when I set the contentType to application/json, all values on the server side turn out to be null. However, if I remove it, the s ...

Why does it seem like only one div is being added?

I am facing an issue with dynamically appending multiple div elements. Despite my efforts, only one div element is showing up on the browser when I try to test the code. I have searched for similar problems but could not find any solutions. Any assistanc ...

Troubleshooting Challenges with JavaScript DOM Manipulation

I am facing an issue with a table where I need the first column to remain fixed when scrolling horizontally. The code snippet below works perfectly for the first column of td's, but fails to work for the tr's. Despite checking the code thoroughly ...