Thursday, January 22, 2009

Adding All Option CFSELECT

I am creating a web page that makes use of related cfselect's. This is a handy feature of coldfusion that allows the contents of one select box to be dependent on the contents of another. For me that means I have one select that lists areas that our company covers. Another box lists the counties we cover. Using the related selects the counties will only show for the area selected in the first box. Very nice. I also link a jurisdiction select to the counties select.

I wanted to add a default "All" option to these selects. Colfusion has an atribute for CFSELECT - queryposition which you can set to above or below. This works great if you are not using the bind attribute in CFSELECT. I was using bind to connect those cfselects so I had to find another way to add "All". I found a comment on Ben Forta's Blog by Matt Bennett that gave me sample code like the following which I adapted slightly for my own use:



sortArray = ArrayNew(1);
QueryAddColumn(qry, "sort_order", sortArray);
QueryAddRow(qry);
querySetCell(qry, "name", "All");
querySetCell(qry, "id", "0");
QuerySetCell(qry, "sort_order", "1");


SELECT *
FROM qry
ORDER BY sort_order desc, name asc



Works great, I just added that code after the qry I was already returning and boom, success.

No comments: