Theory of Relativity

Relativity-formula

Recently whilst putting a .NET web project to bed we had yet more reports from the field of some rendering issues with Internet Explorer 7.  Of course we had tested IE 7 using the compatibility modes present within later versions of IE but seemingly this does not throw up many issues that arise in the ‘real’ browser

I know, I know…., in an ideal world we’d all have the latest and greatest browsers but in the real world companies are not so ‘lithe’ when it comes IT infrastructure.

The issue being reported was that one form displaying an accordion control, which in turn had embedded within it a scrollable div which in turn contained three or four divs as well as a JQGrid was having ‘issues of parentage’… When the main scrollable div was being scrolled all underlying divs were being rendered correctly and scrolling with the content whilst the JQGrid was not moving at all, just merely floating in space and generally getting in the way. After a lot of digging I found the following solution that cured my issues:- The main HTML was defined thus

<div id=”QuestionnaireGridContainer”>
<table id=’QuestionnaireGrid’></table>
</div>

 with the ‘QuestionnaireGrid’ being the location that the javascript JQGrid is rendered into, The issue however seems to lie with the parent div and not the JQGrid itself, what I needed to do was attach a style via the css to the ‘QuestionnaireGridContainer’ div like thus:

#QuestionnaireGridContainer{
    position:relative;
}

This ‘position:relative’ style  in this instance seems to explicitly force all children objects to behave correctly with regard to their location within time and space and now when i scroll my div content all child objects are scrolled exactly as I would expect.

Advertisements

Float Like a Butterfly….Errr

floatlabRecently whilst putting a .NET web project to bed we had some reports from the field of some rendering issues with Internet Explorer 7.  Of course we had tested IE 7 using the compatibility modes present within later versions of IE but seemingly this does not throw up many issues that arise in the ‘real’ browser

I know, I know…., in an ideal world we’d all have the latest and greatest browsers but in the real world companies are not so ‘lithe’ when it comes IT infrastructure.

One such issue being reported was along the following lines:-

Our application was divided into a standard navigation-detail model with the navigation part of the equation being a host of controls displayed within an accordion located to the left hand side of the screen and the detail being displayed to the right hand side of the screen in line horizontally with the accordion. This was achieved through the div hosting the ‘Detail’ section having a ‘float:right’ instruction within the css like so:-

.mainPageContainer{
    display: inline-block;
    float: right;
    height: 75%;
    width: 74%;
}
 
This seemed to work without issue throughout the entire development cycle… We targeted the following browsers:-
 
Internet Explorer 8/9/10 (and 7 in compatibility mode!)
Chrome
Firefox
Opera
Safari
 
All seemed to display without issue, it was left to ‘the real’ Internet Explorer 7 to lower the tone…. Instead of rendering the detail directly to the right of the accordion IE 7 rendered the details BELOW and to the right of the accordion. The fix turned out to be very simple though NOT obvious to me although with hindsight I do kind of understand, the issue was css related  but not with my mainPageContainer, rather the issue was with my accordionContainer which was defined thus:-
 
.accordionContainer{
    height:650px;
    min-width:20%;
    width:25%;
    display:inline-block;
}
 
It is not obvious, to me at least, that in order for any items placed to the right of the accordion to float right ‘properly’ the accordion container itself should be explicitly be made to float left (which it seems to do very well itself without instruction!). Thus I made the following bold changes:-
 
.accordionContainer{
    height:650px;
    min-width:20%;
    width:25%;
    display:inline-block;
    float:left;
}

 Voila, now even Internet Explorer 7 can now correctly render our application,

 

 
 

Displaying Alternative Coloured Images in JQ Grid

RedAmberGreen

Whilst coding up a recent .NET MVC application built heavily using JQGrid I came across a requirement which to be honest I struggled with at first . In short I wanted to, on a column by column basis, colorise the icons that were to be displayed. Now JQGrid supports a icon colorset which seemingly cannot be changed easily so in the end I had to resort to changing the styles. Here is how I achieved this, warts and all….

Firstly of course JQGrid uses the standard set of JQuery Ui Icons to provide its user interface. I say ‘Icons’ but this is of course provided as one image which is broken up into sprites of 16 x 16 pixels, more of that later. So the first step is to download the set of colour icons that you require…. JQueryUI.com provides a standard set of these icons rolled in every colour you can dream so its in fact very easy. The hardest part is arriving at your chosen colours!

I arrived at the following soft representation of the Red, Amber and Green colours :-

  • Red Hex: FF99A3
  • Amber Hex: FFB380
  • Green Hex: 99FFB8

So using these hex values i was able to download my chosen colour schemes using the following url:-

In my instance i downloaded the following files:-

As you can see from the screen grab below the resultant images are in fact compound images containing many 16 x 16 sprites:

FF99A3

In my case I need to use the following icons Bullets and Ticks, as you can see if you examine the image the tick sprite is the fifth sprite across on the tenth line down whilst the bullet is the sixth character on the same line. Given that each character is 16 pixels wide and tall finding the location is relatively easy. The basic formula is (n-1) x16  expressed as a negative where n is the line number or character across.  So in the case of our tick as it is the fifth character across its horizontal location starts at -64 pixels, similarly its vertical location is -144 pixels (9 x 16 expressed as a negative)

We thus need to create some css classes to represent these icons so in my instance I created the following classes:-

.ui-green-tick{
width:16px;
height:16px;
background-image:url(../css/redmond/images/ui-icons_99FFB8_256x240.png)!important;
background-position:-64px -144px
}
.ui-red-tick{
width:16px;
height:16px;
background-image:url(../css/redmond/images/ui-icons_FF99A3_256x240.png)!important;
background-position:-64px -144px
}
.ui-amber-tick{
width:16px;
height:16px;
background-image:url(../css/redmond/images/ui-icons_FFB380_256x240.png)!important;
background-position:-64px -144px
}

.ui-green-bullet{
width:16px;
height:16px;
background-image:url(../css/redmond/images/ui-icons_99FFB8_256x240.png)!important;
background-position:-80px -144px
}
.ui-red-bullet{
width:16px;
height:16px;
background-image:url(../css/redmond/images/ui-icons_FF99A3_256x240.png)!important;
background-position:-80px -144px
}
.ui-amber-bullet{
width:16px;
height:16px;
background-image:url(../css/redmond/images/ui-icons_FFB380_256x240.png)!important;
background-position:-80px -144px
}

Note that each class points to the relevant colour image using the background-image:url property, in order to ensure that our image takes precendence over other images referred to in the JQGrid style sheets I habd to also use the !Important declaration, The sprite location is specified using the background-position: properties using the algorithm discussed above. As you can see we also specify the size of our images. in this instance 16 x 16 pixels.

OK, so believe it or not thats the worst part over with, all that is left now is for you to determine on a row by row and column by column basis which icons are to be displayed and change the relevant classes using javascript I defined the following javascript helper functions, some code has been removed for brevity so they may not be compilable (apologies in advance).

function OnLoadPrimeGridExclusive(grid, actionColumn, theTitle, dataColumn) {

    var iCol = getColumnIndexByName(grid, actionColumn);
    grid.children(“tbody”)
            .children(“tr.jqgrow”)
            .children(“td:nth-child(” + (iCol + 1) + “)”)
            .each(function () {
                $(“<div>”,
                    {
                        title: theTitle,
                        mouseover: function () {
                            $(this).addClass(‘ui-state-hover’);
                        },
                        mouseout: function () {
                            $(this).removeClass(‘ui-state-hover’);
                        },
                        click: function (e) {

                            /* Get the Contexts that we are using here. We need to know the Row
                                and the span in order to accomplish this task……*/

                            var row = $(e.target).closest(“tr.jqgrow”);
                            var span = $(e.target).closest(“span.ui-icon”)

                            /* And now call the Toggle Routine that will toggle the selection */

                            ToggleSelectedSelection(row, span, grid, dataColumn, true);

                            grid.saveRow($(row).attr(“id”), {
                                aftersavefunc: function (id, response, options) {
                                    var someRetValue = response; // set someRetValue to any value
                                }
                            });
                        }
                    }
                  ).css({ “margin-left”: “5px”, float: “left” })
                   .addClass(“ui-pg-div ui-inline-custom”)
                   .append(‘<span id=””></span>’)
                   .appendTo($(this).children(“div”));

                var row = $(this).closest(“tr.jqgrow”)
                var span = $(this).find(“span.ui-icon”);
                ToggleSelectedSelection(row, span, grid, dataColumn, false);

            });
    }
}
function ToggleSelectedSelection(row, span, grid, fieldName, toggle) {

    /* Get the Row id for the current row */

    var rowID = $(row).attr(“id”);
    var unselected =  bulletClass(fieldName);
    var selectedClass = tickClass(fieldName);

     /* And get the current selected value */

    var selected = grid.jqGrid(‘getCell’, rowID, fieldName);
    var removeClass = null;
    var addClass = null;
    var selectionState = null;

    /* Now work out what we need to show */

    if (toggle) {
        removeClass = (selected == ‘false’ || selected == false) ? unselected : selectedClass;
        addClass = (selected == ‘false’ || selected == false) ? selectedClass : unselected;
        selectionState = (selected == ‘false’ || selected == false) ? “true” : “false”;
    } else {
        addClass = (selected == ‘false’ || selected == false) ? unselected : selectedClass;
        removeClass = (selected == ‘false’ || selected == false) ? selectedClass : unselected;
    }

    /* And do that thing! */

    $(span).removeClass(removeClass);
    if (addClass != “”) {
        $(span).addClass(addClass);
    }
    if (selectionState != null) {
        grid.editRow(rowID);
        grid.jqGrid(‘setCell’, rowID, fieldName, selectionState, “”);
        grid.saveRow(rowID, {
           aftersavefunc: function (id, response, options) {
              }

         }
        );
    }
}

function bulletClass(fieldName) {
    switch (fieldName) {
        case “Red”: {
            return “ui-red-bullet”;
        }
        case “Amber”: {
            return “ui-amber-bullet”;
        }
        case “Green”: {
            return “ui-green-bullet”;
        }
        default: {
            return “ui-icon-bullet”;
        }
    }
}

function tickClass(fieldName) {
    switch (fieldName) {
        case “Red”: {
            return “ui-red-tick”;
        }
        case “Amber”: {
            return “ui-amber-tick”;
        }
        case “Green”: {
            return “ui-green-tick”;
        }
        default: {
            return “ui-icon-check”;
        }
    }
}
function  getColumnIndexByName (grid, columnName) {
    var cm = grid.jqGrid(‘getGridParam’, ‘colModel’), i = 0, l = cm.length;
    for (; i < l; i += 1) {
        if (cm[i].name === columnName) {
            return i; // return the index
        }
    }
    return -1;
}

And in the loadComplete event of the grid the following javascript starts the whole process….. If you are only colourising one column you need only call this routine once

OnLoadPrimeGridExclusive(grid, ‘RedAction’, ‘De/Select Red Rating’, ‘Red’, true);
OnLoadPrimeGridExclusive(grid, ‘AmberAction’, ‘De/Select Amber Rating’, ‘Amber’,true);
OnLoadPrimeGridExclusive(grid, ‘GreenAction’, ‘De/Select Green Rating’, ‘Green’, true);

A few notes that need to be made here, for each display column where the actual image is displayed ticked or not in this instance the columns RedAction/GreenAction/AmberAction there is also a hidden checkbox type data column which physically stores the boolean value indicating whether or not the grid is ticked or not (entitled Red/Green/Amber). This column is manipulated whenever the user clicks on an image to toggle the selection and the icon is set according to the underlying value of that column.

My time with JQGrid on this particular project has not been a happy one, I started off using the .NET MVC implementation but have overtime reverted to using the plain old javascript version as it offers a great deal more control and configurability. I think we may have turned a corner now and so thought I’d like to share some of that positivity with the community.

I hope this helps.