Monthly Archives: January 2015

3 8894

Today I got to write a jQuery plugin that styles a select with hierarchical data. The markup would normally look like this in HTML:

<select>
  <optgroup label="Swedish Cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
  </optgroup>
  <optgroup label="German Cars">
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </optgroup>
</select>

You can see the result of this over at w3schools

This looks cool, but what if you want to allow the user to select the optgroup telling the back end script to grab that level and all under it? I whipped up a quick plugin which works off of data attributes to describe the hierarchy. Here is what the structure of the html looks like:

<select id="hierarchySelect" class="form-control">
    <option data-isparent="true"  data-level="1">Main Dept 1</option>
    <option data-isparent="false" data-level="2">Sub 01</option>
    <option data-isparent="false" data-level="2">Sub 02</option>
    <option data-isparent="true"  data-level="2">Sub 03</option>
    <option data-isparent="false" data-level="3">Sub of Sub</option>
    <option data-isparent="true"  data-level="1">Main Dept 2</option>
    <option data-isparent="false" data-level="2">Sub 01</option>
    <option data-isparent="false" data-level="2">Sub 02</option>
    <option data-isparent="false" data-level="2">Sub 03</option>
</select>

The data-isparent should be flagged as true when there are child options under it. The plugin assumes that the options are already in the correct order. The data-level describes how many levels deep the option is. This assists in the indentation of the option. The default indentation is 25px, but can be overridden in the options of the plugin.

$("#hierarchySelect").selectHierarchy();

Here is a working fiddle that can be used to see the plugin in action.

Here is a link to Download the plugin. I want to build in some additional options and push it out to github when I have a chance. I will update this post when that happens. In the mean time, feel free to use it however you would like.

0 5357

I recently needed to determine if a library was loaded in CI form my Model. I have the CI Rest library using the same model for a query and wanted to change the query if the system had a user logged in. I simply wanted to check and see if the Tank Auth library was loaded and grab the user id from it if it was. I ended up using the PHP function class_exists.

    if(class_exists('Ion_auth')) {
        //Include user specific query stuff here
    }

0 1915

Thought I would share a function I wrote to convert property from an a array of objects to a one dimensional array. I ran into this problem because I wanted to select a single field from a DB query into a one dimensional array. I could not figure out how to get that from the DB. All that was available was a object array or array of arrays.

Here is a function to take care of this issue

function objects_to_one_dim($array, $property) {
            $oneDimArray = array();
            foreach($array as $o) {
                $oneDimArray[] = $o -> $property;
            }

            return $oneDimArray;
}

and can be utilized like this

 $newArray = objects_to_one_dim($myObjArray, 'a_property_in_the_object');

23 26225

I have been working on a project in PHP / CodeIgniter that I CANNOT wait to tell you guys about…

I have been developing some sweet CI libraries for this project and have decided to give some of them back to the community.   This is my first PHP open source contribution and my first solo Github project.

The CodeIgniter DataTable library handles all the DB operations for searching, sorting, paging, etc a jQuery DataTable.  The project can be found on GitHub CodeIgniter-DataTables.

This weekend I will be working on a CI example application that uses this library, and will update this post when it is available.  I have some other useful libraries that will be rolling out soon so stay tuned…
***Update: A full working example can be found here