PINSystems Source Samples:
HTMLGrid
  SourceSamples Home

Launch Sample   Source Code

The concept: Create a cross-browser grid control based on html/javascript. We've used, and like, SlickGrid but the dependencies are a little more than we want all of the time. Plus, it's easy to layout workable html table components for both headers and data. The trick in this task was to get the header and data columns synchronized in both size and position. In this project we've gone under the assumption that the header should always be sized to match the data column width.
Is it perfect, no but it's pretty close. Chrome appears to work a little differently than the others and is occassionally 1 pixel off. I haven't been able to find the cause, but if/when I do this will be updated.

Problems encountered...
  • Getting sizing and positioning to work properly on all browsers while being easy to implement, performant and facilitate formatting any implementations we might need. We test on IE, Chrome, FireFox and WhiteHat Aviator.


  • Resolutions...
    Sizing
    This took much longer than anticipated. After a few iterations using different header control sets, we ended up back to using a table for the headers. The trick was to...
    1) Determine the css attributes/values that were required for proper processing. After a successful combination was determined, these values were hardcoded into component inititalization logic.
    2) Realize that when setting a TDs width to attain a specific dimension, the padding and border-width settings of the TD being set need to be taken into account (removed from the desired size).
    3) Determine the component structure that would faciliate the required functionality... self contained logic (as much as possible) and resizing the outer container resizing the control properly.

    Source Code...
    The following HTMLGrid implementation is from HtmlGridSamnple.html / .js / .css

    HTML definition/requirements
    Notes:
    * The component tree outlined below is required and number of header TDs should match the visible data columns/TDs
    * Class settings aren't required, but surely you'll want to format it


    /* Add the htmlGrid object library */
    <script type="text/javascript" src="psi.ui.htmlGrid.js"></script>
    ...
    <div id="divGridContainer" class="divGridContainer">
      <div id="divHeaders" class="divHeaders">
        <table id="tblHeaders" class="tblHeaders">
          <tr id="trHeaders" class="trHeaders">
            <td id="hdr1" class="hdrAll hdr1">Header 1</td>
            <td id="hdr2" class="hdrAll hdr2">Header 2 And More And More And More</td>
            <td id="hdr3" class="hdrAll hdr3">Header 3</td>
          </tr>
        </table>
      </div>

      <div id="divTableData" class="divTableData">
        <table id="tblData" class="tblData">
          <tr>
            <td class="tdAll td1">Cell Content 1</td>
            <td class="tdAll td2">Cell Content 2</td>
            <td class="tdAll td3">Cell Content 3</td>
          </tr>
          <tr>
            <td class="tdAll td1">More Cell Content 1</td>
            <td class="tdAll td2">More Cell Content 2</td>
            <td class="tdAll td3">More Cell Content 3</td>
          </tr>
        </table>
      </div>
    </div>

    JavaScript
    * Define a variable for each grid implemented on the page
    * Initialize the grid(s) in the page onload handler
    * Add a formatGrid call for each grid object in the page onresize handler.
    * The sample page contains a demonstration containing multiple grid controls on one page.

    var _psiHtmlGrid = null;
    function pageLoaded(event) {
      /* Prep the psi.ui.htmlGrid object */
      /* If multiple grids on one page, Prep a psi.ui.htmlGrid object for each */
      var divGridContainer = document.getElementById("divGridContainer");
      var divHeaders = document.getElementById("divHeaders");
      var divTableData = document.getElementById("divTableData");
      _psiHtmlGrid = new psi.ui.htmlGrid(divGridContainer, divHeaders, divTableData);
      /* If you have a debug message logger with a func(msg) signature, it can be assigned to the object */   //_psiHtmlGrid.setDebugMsgFunc(addDebugMsg);

      /* Run the initial sync on the grid */
      _psiHtmlGrid.formatGrid();
    }
    window.onload = pageLoaded;

    function pageResized(event) {
      _psiHtmlGrid.formatGrid();
    }
    window.onresize = pageResized;


    CSS
    Take a look at HTMLGridSample.css, it's commented fairly well.
    You should be able to do anything you want to the header and data TD elements.
    Library owned settings are identified.
    Interesting... This css contains eve/odd row selectors. Good for alternating data row colors.



    12/21/2014 PINSystems.com