Entries for month: March 2010

Check All Checkboxes with JQuery Plugins

Posted by Bharat Patel on March 30, 2010
  0 comments

Hello Friends,

A jQuery plugin is simply a way to put your code into a package. It makes it easier to maintain and use in different projects.

I have created sample jquery plugin for checking all checkboxes at one click. Also you can select odd or even row using this plugin. You need to give class of child checkboxes. Remember, Class name must be same for all.

This plugins have three attributes.

1. child - It contains name of class which is assinged to child checkboxes.
2. odd - It contains boolean value either true or false. default value is false;
3. even - It also contains boolean value either true or false. defaule value is false;

Demo  Download

jQuery(function(){
    jQuery('#one').checkall({
          child : 'child',
          odd : true              
   });  
});

jQuery.checkAll.js contains

(function($){
    $.fn.extend({ 
        // Check and uncheck all check boxes
        checkall: function(options){
            var defaults = { 
                child : '',
                odd : false,
                even : false
            };
            var opts = $.extend({},defaults, options);
            return this.each(function(){
                obj = $(this);
                obj.click(function(){
                    var checkStatus = $(obj).attr('checked');
                    if(opts.odd == true){
                        $('.'+opts.child+'').attr('checked',false);
                        $('.'+opts.child+':even').attr('checked',checkStatus);            
                    }else if(opts.even == true){
                        $('.'+opts.child+'').attr('checked',false);
                        $('.'+opts.child+':odd').attr('checked',checkStatus);            
                    }else{
                        $('.'+opts.child+'').attr('checked',checkStatus);
                    }
                });
            });
        }
    });
})(jQuery);




cffinally, finally

Posted by Bharat Patel on March 17, 2010
  0 comments

Hello Friends,

ColdFusion 9 introduced cffinally tag. Cffinally or finally blocks are used within cftry or completion of try block. It is generally used for cleanup i.e. close database connections, close some objects or excecute code whether it is exception or not.

Category

Data output tag and error handling.

CFML Syntax

<cftry>
    ......
    <cfcatch type="any">
        ......
    </cfcatch>
    <cffinally>
        ......
    </cffinally>
</cftry>

CFScript Syntax

<cfscript>
    try{
        ......    
    }catch(Any e){
        ......
    }finally{
        ......
    }
</cfscript>

Generally we use try and catch block to catch exception like application error, database error, custom error etc. But sometimes we need to run some code whether it is exception or not so generally we need to write both of it in catch block as well as after try block but now in ColdFusion 9 has introduced cffinally and many more useful tags. cffinally tag always use in try block. You can nest cftry/cfcatch/cffinally blocks. Cffinally tag executes code whether it is exception or not. You can also use finally in cfscript tag.  These tags are meant for error handling. I hope it may be useful for you.

CFML Example

<cftry>
    <cfset a = 10>
    <cfset b = 0>
    <cfset a = a / b>
    <cfoutput><b>Ans : </b>#a#</cfoutput>
    <cfcatch type="any">
        <cfdump var="#cfcatch.Message#">
    </cfcatch>
    <cffinally>
        <br /><b>Finally Block :</b>
        <cfdump var="#a#">
    </cffinally>
</cftry>


CFScript Example
<cfscript>
    a = 10;
    b = 0;
    try{
        a = a / b;
        writeOutput('<b>Ans : #a#</b>');
    }catch(Any e){
        writeDump(e.message);
    }finally{
        writeOutput('<br /><b>Finally Block : </b>');
        writeDump(a);
    }
</cfscript>

Output

Division by zero.
Finally Block : 10

 




wrap text in cfgrid - cf9

Posted by Akash Bavlecha on March 11, 2010
  1 comments

Hi ,

We have been updated to cf9 from cf8 and we noticed that text in cfgrid column in not wrapped. Its just because of changes made in version 3 of ext. Turns out the fix is pretty simple. Look at the css code below which has to be applied on a page where you require to wrap text in cfgrid.

CF8:

<style>
.x-grid-row-selected td{color:black;}
.x-grid-col{white-space: normal ! important;}
.x-grid-cell-text {white-space: normal !important;}
</style>

CF9:

<style>
.x-grid3-cell-inner {white-space: normal ! important; display:block;}
</style>

 




Date Difference Excluding Weekend.

Posted by Bharat Patel on March 10, 2010
  2 comments

Hello,

Recently I need to find the date difference between two dates excluding weekends. So I have made sql server scalar function to get date difference excluding weekends. You need to pass only two arguments as start date and end date. Refer below example. I hope it may be helpful.

Declare @startDate date, @endDate date;
Set @startDate = CONVERT(date,'03/11/2010');
Set @endDate = CONVERT(date,'03/25/2010');

select dbo.fun_getDateDiffExcludeWeekEnd(@startDate,@endDate)

Basically datediff function start count from next to @startDate so if you want to get count with @startdate you need add +1 in count.

select dbo.fun_getDateDiffExcludeWeekEnd(@startDate,@endDate) + 1

Otherwise you can write it like below syntax.

select dbo.fun_getDateDiffExcludeWeekEnd(dateadd(d,-1,@startDate),@endDate)

Function for get date different with exclude weekends.

CREATE FUNCTION [dbo].[fun_getDateDiffExcludeWeekEnd] 
(
    @start datetime,
    @end datetime
)
RETURNS numeric
AS
BEGIN
    DECLARE @WeedDay numeric = 0;
    DECLARE @TotalDayCount numeric = 0;
    
    SELECT @WeedDay = datepart(weekday,@end);
    
    IF @WeedDay = 1
        BEGIN
            SELECT @end = DATEADD(d,1,@end);
        END
    IF @WeedDay = 7
        BEGIN
            SELECT @end = DATEADD(d,2,@end);
        END
    SELECT @TotalDayCount = (DateDiff(d, @start, @end) - ( DateDiff(ww, @start, @end) * 2));
    RETURN @TotalDayCount
END




Finding real IP address in coldfusion

Posted by Akash Bavlecha on March 09, 2010
  0 comments

Hello,

I was facing one problem before few days as i was storing ip addresses in my database.
when i looked into database i amazed that there were some entries of ip addresses which were not true.

If anyone is connected to internet through Proxy server then using 'CGI.REMOTE_ADDR' just returns the IP address of proxy server not of the user's machine.

What we need to do is to compare 'CGI.HTTP_X_Forwarded_For' variable against 'CGI.REMOTE_ADDR'.

 

<cfif CGI.HTTP_X_Forwarded_For EQ ""><!--- Checking proxy address --->
       <cfset real_ipaddress = CGI.REMOTE_ADDR>
<cfelse>
       <cfset real_ipaddress = CGI.HTTP_X_Forwarded_For>
</cfif>