Here’s a simple coldfusion function to make a string title case(every word of the string starting with capital letters). You can pass in exceptions in a list.
it uses some underlying JAVA functionality to manipulate the string.
Enjoy!
Here’s a simple coldfusion function to make a string title case(every word of the string starting with capital letters). You can pass in exceptions in a list.
it uses some underlying JAVA functionality to manipulate the string.
Enjoy!
If you have to handle a list of data in coldfusion, you could end up using a list or an array. Well choose and array working with the data in the array is much faster compared to lists. lets do a little test for that theory:
lets define our list and array
<cfset list = “”>
<cfset array = arraynew(1)>
now lets do a loop and insert data in the list and the array
so we loop from 1 to 5000 and insert the data in the list and the array surrounding with cftimer tags to see the execution times in our debug info. the results are not surprising at all. Arrays being the clear winner. Here are the results for three executions
CFTimer Times
[172ms] myTimerString
[0ms] myTimerArray
CFTimer Times
[187ms] myTimerString
[0ms] myTimerArray
CFTimer Times
[172ms] myTimerString
[0ms] myTimerArray
So when handling list data’s use arrays for your data storing and sorting. If u need to have a list at the end you can always use the arrayToList() function to get it in a list.
Interestingly in JAVA there is very little difference doing the same test. Thats a bit weird considering that coldfusion itself runs on JAVA.
peace out!
Coldfusion makes it really easy to add autosuggest to a textbox (CF 8 and above only). the cfinput tag has a new attribute (autosuggest) that takes a list as an input and matches the entered text with this list.
so we have our list
<cfset myList = “jack,corey,david,sam”>
making the autosuggest is as easy as just adding the list to the cfinput tag
<cfform>
<cfinput name=”myInput” type=”text” autoSuggest=”#myList#”/>
</cfform>
You can also use data from a query to populate the textbox.
<cfquery name=”myQuery” datasource=”myDSN”>
SELECT firstName FROM Customer
</cfquery>
the input then changes to
<cfform>
<cfinput name=”myInput” type=”text” autoSuggest=”#valueList(myQuery.firstName)#”/>
</cfform>
One thing to keep in mind is that this way you append the whole list to the inline HTML of the page, so if you have a small list then its not a problem but if you have a massive list to check with, the page size could increase heaps. In those cases it is wiser to bind the input field to a CFC else use jQuery to make the autosuggest getting data via ajax from another page(i will post a jQuery alternative soon).
so here’s the final cfinput bound to a CFC
<cfform>
<cfinput name=”myInput” type=”text” autoSuggest=”cfc:myCFC.myFunction({value})”/>
</cfform>
Of course the function will have access set to remote and watch out for the onRequest function in the Application.cfc in case you use it.
We all know SQL Server has a plan cache for queries being executed. The query plans get cached and reused as the same query gets executed again. We can look after the plan cache by making sure we follow a few basics when writing queries.
The demo is for coldfusion using the cfquery tag and the same applies for any development environment passing queries to SQL server.
Here’s our first query
<cfquery datasource=”myDB”>
SELECT * FROM myTable WHERE foo=1;
</cfquery>
Here’s our second query
<cfquery datasource=”myDB”>
SELECT * FROM myTable WHERE foo=1 ;
</cfquery>
Although they appear to be the same, SQL server will store them as two separate query plans because of the extra space between the foo=1 and the query end symbol ‘;’. You can check the plans are stored separately in the cache using the DMV sys.dm_exec_cached_plans (2005 and upward only).
Just something to keep in mind while writing queries!
jquery is super cool! Here’s how easy it is to add a max length indicator using jQuery.
Firstly we have our textare inside a div layer
<div id=”commentDiv”>
<label for=”comment-text”>Comment:</label>
<textarea rows=”7″ cols=”30″ name=”comment-text” id=”comment-text”></textarea>
</div>
Then we have our jQuery code that displays and updates the indicator.
$(document).ready(function(){
var maxLength = 100;
$(‘#commentDiv’).append(‘<span id=”length”>’+maxLength+’</span>’);
$(‘.maxlength’).keyup(function(e) {
$(‘#length’).text(maxLength-$(this).val().length);
if ($(this).val().length >= maxLength) {
e.preventDefault();
}
});
});
The append function adds the span where we display the maxlength value to start with. You can add it in the HTML code on the page if you like. The keyup function then binds to the textarea. Then we change the value in the span tags using the .text function. The if condition stops the textarea from going over the maximum length set by us.
There you go , done and dusted in about 10 lines of code.
When dealing with coldfusion functions, i noticed my function which returns a string always came back with a leading space for some reason.
<cffunction name=”myFunction” access=”public” returntype=”string”>
<cfreturn “myString”/>
</cffunction>
When the function is called and you output the string – (i wont bother with creating the component and creating the object, you get the idea!)
<cfoutput>’#myFunction()#’</cfoutput>
outputs:-
‘ myString’
To remove this leading space i added the output attribute to the function and voila, it’s gone.
<cffunction name=”myFunction” access=”public” returntype=”string” output=”false”>
<cfreturn “myString”/>
</cffunction>
<cfoutput>’#myFunction()#’</cfoutput>
outputs:-
‘myString’
I have no idea why this happens. If someone knows why this happens and is expected behaviour, please post your comments.