jQuery: Count the Number of Checkboxes Checked on a Page

To return the number of checked or ticked checkboxes on a page using jQuery is in actual fact pretty simple.

By making use of the special :checked selector and combining it with the standard length property, you code snippet to retrieve the number of checked checkboxes on a page looks like this:

alert($(":checkbox:checked").length);

Similarly, if you wanted to locate all the checked checkboxes based on a specific selector grouping, you would make use of the .filter function and combine it with those we used above. So your code would now look as follows:

alert($('.createrightcheckbox').filter(":checked").length);

Note, if you wanted to run the same code above but only pick up on unchecked checkboxes, you could run:

alert($('.createrightcheckbox').filter(":not(:checked)").length);

Nifty.

Posted in Technology & Code, Tutorials | Tagged , , , , | View Comments

jQuery DataTables: Simple Row Highlight Example

I’ve mentioned the wonderful jQuery plugin DataTables a number of times before, the awesome little trick that instantly transforms any HTML table fed to it into a fully sortable, paginated, searchable and zebra-striped table, requiring the most minimum of coding to implement.

Today’s tip looks at how you can quickly and easily implement a visually helpful hover-based row highlight on any of your datatable objects.

First off, you will need to define a highlight rule to be applied to your table in your CSS declarations. In the simplest form, all you really want to do is define a unique background-color to the element wish you wish to highlight.

In CSS, this could look something like this:

.datatablerowhighlight {
background-color: #ECFFB3 !important;
}

The next step is then to add our new highlight class to the row when the mouse hovers over it and then to remove it again when the mouse moves away. To do this we need to add bindings for the mouseenter and mouseleave jQuery events, where we add and remove the highlight class accordingly.

To do this, we piggyback into the handy fnDrawCallback function that is defined when initializing your datatable and which gets executed each time the datatable gets drawn.

Looking at this in code, you should now have:

$('#activitylog').dataTable( {
"fnDrawCallback": function(){
      $('td').bind('mouseenter', function () { $(this).parent().children().each(function(){$(this).addClass('datatablerowhighlight');}); });
      $('td').bind('mouseleave', function () { $(this).parent().children().each(function(){$(this).removeClass('datatablerowhighlight');}); });
}
} );

Note how we grab the whole rows contents y first grabbing the parent element of the element we are currently hovering above, and then expanding to all that parent’s children elements – in other word the entire row. A simple addClass and removeClass call handles the actual attaching and detaching of the highlight CSS class.

Nifty, and pretty damn effective to boot! ;)

Related Link: http://datatables.net/

Posted in Technology & Code, Tutorials | Tagged , , , , , , , | View Comments

jQuery DataTables: How to Force Specific Column Widths

I’ve mentioned the wonderful jQuery plugin DataTables a number of times before, the awesome little trick that instantly transforms any HTML table fed to it into a fully sortable, paginated, searchable and zebra-striped table, requiring the most minimum of coding to implement.

Today’s tip looks at how you can specify what column widths to use in your DataTables implementation, instead of relying on the default auto column widths assigned by the plugin.

Firstly, you need to turn off DataTable’s built in auto column width calculator by feeding it a false value for the bAutoWidth switch when initialising your DataTable object. Next, you need to feed the column widths you wish to use directly into the initialization by means of the aoColumns definitions, making use of the sWidth switch to specify the width.

Looking at this in code, you should now have:

$('#activitylog').dataTable( {
"bAutoWidth": false,
"aoColumns": [{"sWidth":"60%"},{"sWidth":"20%"},{"sWidth":"20%"}]
} );

Nifty.

Related Link: http://datatables.net/

Posted in Technology & Code, Tutorials | Tagged , , , , , | View Comments

FPDF PHP PDF Generator: How to Wrap Long Lines of Text in Tables

I have mentioned the useful FPDF PHP PDF generating library before, but today I’m quickly going to point out how you can solve the problem of inserting extra long lines of text into a table and forcing the table to automatically wrap or linebreak all of your text without simply chopping it off at the end when it reaches the right hand border of the cell.

The solution to this is pretty simple and it involves dropping the use of the Cell() function and instead incorporating the MultiCell() call.

The MultiCell basically allows for the printing of text with line breaks. These line breaks can be automatic, in other words when the text hits the right border of the cell, or explicit, i.e. via the \n control character.

The function cleverly automatically lays down as many cells as possible, one below the other and all without any input or direction from you. The cell block can then be frames and even have its background painted if you want.

Parameters are pretty self-explanatory. You can set the width of the cells, the height of the cells, the text to print, whether or not you want a border, how you want the text aligned and whether or not you want the background filled.

Putting this in code, you would get a call that looks something like this:

$pdf->MultiCell(275,7,’Insert your long string here’,1,’L',false);

Nifty.

Related Link: http://www.fpdf.org/en/doc/multicell.htm

Posted in Technology & Code, Tutorials | Tagged , , , , , | View Comments

Accessing Values in Javascript JSON Objects with Keys containing Hyphens (Dashes)

In Javascript, JSON objects (well, JS objects in general) have a great short hand little way of accessing values using the fullstop object notation.

In other words, using a JSON dataset looking like:

{“status”:”1″,”message-data”:”Success!”}

we could access the “status” value by simply entering:

alert(myobject.status);

However, if we tried the same thing with the “message-data” key, you will note that this will fall over syntactically straight away – all because of that silly hyphen (dash).

So the way around this?

Well you basically have to revert to the long notation format to get at the data associated with these keys, meaning that you would now need to do something like this:

alert(myobject["message-data"]);

And now you know.

Nifty.

Posted in Technology & Code, Tutorials | Tagged , , , , , | View Comments