Datatables jquery plugin – sorting date with JSON data

Datatables is powerful but the documentation can be confusing in places. If you want a column to sort on a date one option (of many) is to have a formatted date to display to the user and an integer timestamp to use for the sorting.

Datatables column sorting with date

Here is some JSON that could be used to create this

var data = [
 {
 "name": "Airi Satou (33)",
 "position": "Accountant",
 "office": "Tokyo",
 "displayStartDate": "23\/01\/03",
 "startDate": "1043362800",
 "salary": "162700",
 },
 {
 "name": "Angelica Ramos (47)",
 "position": "Chief Executive Officer (CEO)",
 "office": "London",
 "displayStartDate": "17\/01\/03",
 "startDate": "1042761600",
 "salary": "1200000",
 }
];

The JavaScript used to get proper sorting of the date column involves adding an extra object to the date column which allows another data item to be used for the sort.

$('#example').DataTable( {
 data: data,
 columns: [
 { data: 'name' },
 { data: 'position' },
 { data: 'office' },
 { data: {
   _: 'displayStartDate',
   sort: 'startDate'
 }},
 { data: 'salary'}
 ]
 } );

One advantage of this approach is that date formatting is done up on the server which may be useful if you are sending tens of thousands of rows of data to the client.

A great WordPress theme for Bootstrap 3

If you are looking for a clean WordPress theme that uses Bootstrap 3, I can recommend Bootstrap Basic.

bootstrap-basic

I had an issue when I first installed it so I raised it on the support forum and the author immediately made some changes and released a new version.

Adding a custom hover event to a morris.js donut chart

Morris.js is a lightweight and relatively simple JavaScript charting solution compared when compared with some of the others in the sector which have lot of options.

morrisjs-charts

However with that simplicity comes a lack of documentation.

Adding a custom hover event to donut chart

Here’s some HTML which adds a visible label element (like you get with a line chart) next to the donut chart:

2010 Q4
this is a test

I won’t cover how to create the donut chart in JavaScript as you can just lift that from the morris.js examples, but you must assign the chart to a variable:

var donut = new Morris.Donut({...});

Finally the code to add hover events should look like this:

for(i = 0; i < donut.segments.length; i++) {
  donut.segments[i].handlers['hover'].push(function(i){
    $('#morrisdetails-item .morris-hover-row-label').text(donut.data[i].label);
    $('#morrisdetails-item .morris-hover-point').text(donut.data[i].value);
  });
}

Here is a working example on JSFiddle.

CodeIgniter 3.1 Blog example

I was asked to do a coding test to create a basic blog. I decided to to use CodeIgniter 3.1 (yes – it’s not dead as was widely reported a few years ago) because I’ve been working with it recently. CodeIgniter may not be as fashionable as Laravel but it is considerable smaller and will run fast on a small/cheap server, it is also quick and easy to learn how to use it.

My source code is available on BitBucket if anyone is interested to take a look: https://bitbucket.org/richardwo/coding-test-blog-ci. There are 2 controllers for users and posts, 3 models for categories, posts and users, 5 admin view files and 3 front-end view files. Overall it took me about 4.5 hours.

Lessons learnt:

Some other PHP Frameworks like CakePHP or Laravel can include registration and authentication by running a few commands. This would have saved me a lot of time because I had to write my own login system and it took a while.

I also ran into some issues with redirects because I was running using the PHP built-in webserver trying to make the application self contained – after some investigation it turns out I needed $config['base_url'] = 'http://localhost:8000/'; in application/config/config.php for the redirects to work properly.

More time was wasted writing the usual CRUD methods and creating Bootstrap forms (even if this is only a cut/paste job). CakePHP scaffolding / Code Generation with Bake would have saved more time (although out of the box it doesn’t use Bootstrap).

Blog home screen

On a more positive side, I did like the validators where using 'is_unique[posts.slug]' will automatically check that table/field for you and return an error if it’s already been used.

Next time I’ve got to do this I’ll use Laravel as it seems more popular than CakePHP and I’ll benefit from the nice extras this has to offer over CodeIgniter. However I could just reuse my authentication code and save time.

CodeIgniter set_select default value

The CodeIgniter documentation is vague on how to set the default value in a select tag when you are using the form helper. There’s a third parameter to the set_select method that’s a boolean.

I recommend using something like the following code snippet:


So you have to check the value of the $option variable with each iteration of the loop and if it matches another variable (which you should set in your controller) it sets the TRUE that the set_select method requires.