Shaun Mccran

My digital playground

15
M
A
Y
2011

Combining JQuery Datatable with drag and drop functions

The datatables JQuery plugin ( http://www.datatables.net/ ) is an incredibly powerful way of displaying tabular data and allowing the user to manipulate it without leaving the current view.

The plugin already features paging and sorting, but what if you want to extend the plugin with some other non default behaviours like drag and drop? Using a previous example ( http://www.mccran.co.uk/index.cfm/2010/4/29/JQuery-Datatables-plugin-example-using-a-server-side-data-request-coldfusion ) as a starting point I thought I'd try and integrate drag and drop with the datatable object.

[ More ]

01
M
A
Y
2011

Adding custom validation rules to the JQuery Validation Plugin

The http://bassistance.de/jquery-plugins/jquery-plugin-validation/ JQuery Validation plugin is a fantastic option for adding client side JavaScript validation to your forms.

It comes with quite a few pre defined validation methods (you can see them here: http://docs.jquery.com/Plugins/Validation#List_of_built-in_Validation_methods), but what if one of your fields requires a bespoke validation type, for example matching the first N characters of a string.

Well its pretty easy to do, you just have to plan your new rule, and programatically add it to the plugin. In the example below I am going to match the string '123456' and check it as a rule, alongside the existing rules.

I'm assuming you have already imported the JQuery library and Validation plugin. Next we use the addMethod() function of the Validation plugin to add a new custom method. This accepts a few different arguments, as detailed below:

Name (String)
The name of the method, used to identify and referencing it, must be a valid javascript identifier

Method (Callback)
The actual method implementation, returning true if an element is valid. First argument: Current value. Second argument: Validated element. Third argument: Parameters.

Message (string / function)
The default message to display for this method. Can be a function created by jQuery.validator.format(value). When undefined, an already existing message is used (handy for localization), otherwise the field-specific messages have to be defined.

As an example I have created a new rule called 'pattern-match', this rule will check if the passed in element is required 'this.optional(element)' OR if the first six characters match the designated pattern, in this case '123456'.

view plain print about
1<s/cript language="javascript">
2$(document).ready(function(){
3
4$.validator.addMethod("pattern-match", function(value, element) {
5
6return this.optional(element) || (value.substr(0,6) == '123456');
7
8}, "* match the string");
9
10});
11</script>

This code creates the new validation method, now we can reference it in our validate() method just like all the pre existing methods.

view plain print about
1<s/cript language="javascript">
2$(document).ready(function(){
3
4$("#form").validate({
5
6errorContainer: "#error",
7errorLabelContainer: "#error ul",
8wrapper: "li",
9
10rules: {
11numberField: {
12
13required: true,
14pattern-match: true,
15minlength: 8,
16maxlength: 10 }
17    },
18
19messages: {
20
21numberField: {
22
23required: "Please enter a number",
24pattern-match: "Number does not match 123456",
25minlength: "Number must be at least 8 characters long",
26maxlength: "Number cannot be more than 10 characters long"
27}
28
29    }
30});
31
32});
33</script>

Just add it in with the other methods, and specify the the error message that goes with it.

The addMethod() function will take any form of custom validation that you can write in standard JavaScript, so it should be possible to accommodate any bizarre rules you might want to come up with.

13
A
P
R
2011

Triggering functions when closing a fancybox pop up

The FancyBox JQuery plugin is a popular lightbox style plugin that allows you a variety of options to 'pop' different types of content up into a users view.

I needed to have the parent page change when the user had performed an action within the pop up. But how to do this?

FancyBox (http://fancybox.net/) has a 'onClosed' option to allow you to embed a function call whenever the user closes the lightbox.

The format of this is a little tricky, so here is an example:

view plain print about
1// pop up handler
2$.fancybox({
3    'autoDimensions':    false,
4    'speedIn'            : 600,
5    'speedOut'            : 200,
6    'overlayShow'        : true,
7    'width'            : 440,
8    'height'            : 340,
9    'type'            : 'iframe',
10    'scrolling'            : 'no',
11    'transitionIn'        : 'elastic',
12    'transitionOut'        : 'fade',
13    'enableEscapeButton'    : 'true',
14    'overlayOpacity'        : 0.5,
15    'title'            : '',
16    'href'             : 'yourpage.htm',
17    'onClosed'            : function() { alert('closed'); }

You can build any function you want in this call, it is normal JavaScript after all, if you just wanted to reload the page you could have a refresh function:

view plain print about
1function() {
2
3parent.location.reload(true);
4
5}

This seems to work for any method of closing the Fancybox as well, so it will always fire.

24
M
A
R
2011

JQuery Drag and Drop to multiple div elements

My only previous experience with Drag and Drop web development was with Flex 2, which I have to say was a bit of a nightmare. There was an astronomical amount of setup to do in terms of event listeners and mouse watching services, so I was reluctant to get back into 'Drag and Drop' with any other technology.

This article explains how straight forward the JQuery Drag and Drop interfaces are to use, and how I constructed a Drag and Drop example using multiple divs.

There is an example of a JQuery Drag and Drop demo here. http://www.mccran.co.uk/examples/jquery-drag-drop

[ More ]

_UNKNOWNTRANSLATION_ /