Monday, March 23, 2015

DataTables 1.10.x and ColdFusion serverside parsing

The 1.10 update to datatables is a great improvement, adding things like html5 support and responsive output.  There is even a new, easy to implement piece to support Bootstrap (the css layout framework).

One of the side effects of the update, though, is that all of the server side variables submitted to your ajax processing back end have changed.  Unfortunately, the new variables look like cf array notation and is difficult to put into a shape that you can easily work with.

Here is a bit of code that you might find useful if you need to do this sort of thing:

<cfscript>
req = {columns = [], search = {}, order = {}};
var keys = listToArray(listSort(structKeyList(url), 'textnocase', 'asc'));

for (key in keys)

{
attr1 = listFirst(key,'[]');
switch (attr1) 
{
case "columns":
{
attr2 = listGetAt(key,2, '[]');
attr3 = listGetAt(key,3, '[]');
switch(attr3)
{
case "data":
{
arrayInsertAt(req.columns, attr2+1, {data=url[key]});
break;
}
case "name":
{
req.columns[attr2+1]["NAME"] = url[key];
break;
}
case "orderable":
{
req.columns[attr2+1]["ORDERABLE"] = url[key];
break;
}
case "search":
{
attr4 = listGetAt(key, 4, '[]');
switch(attr4)
{
case "regex":
{
req.columns[attr2+1]["SEARCH"]["REGEX"] = url[key];
break;
}
case "value":
{
req.columns[attr2+1]["SEARCH"]["VALUE"] = url[key];
break;
}
}
}
case "searchable":
{
req.columns[attr2+1]["SEARCHABLE"] = url[key]
}
}
break;
}
case "search":
{
attr2 = listGetAt(key, 2, '[]');
switch(attr2)
{
case "regex":
{
req.search["REGEX"] = url[key];
break;
}
case "value":
{
req.search["VALUE"] = url[key];
break;
}
}
break;
}
case "order":
{
attr2 = listGetAt(key, 2, '[]');
attr3 = listGetAt(key, 3, '[]');
switch(attr3)
{
case "column":
{
req.order["COLUMN"] = url[key];
break;
}
case "dir":
{
req.order["DIR"] = url[key];
break;
}
}

break;

}
default:
{
req[ucase(key)] = url[key];
}
}
}
</cfscript>
<cfdump var="#req#"/>

<cfdump var="#url#"/>