Check/Uncheck All Checkbox by Jquery

Just copy this function.

function InitCheckAll(checkAllSelector, checkboxSelector) {
    $(checkAllSelector).change(function() {
        var e = this;
       
        $(checkboxSelector).each(function() {
            this.checked = e.checked;
        });
    });

    $(checkboxSelector).change(function() {
        if (this.checked == true) {
            var isCheckAll = true;
            $(checkboxSelector).each(function() {
                if(!this.checked) {
                    return isCheckAll = false;
                }
            });
            $(checkAllSelector)[0].checked = isCheckAll;
        } else {
            if ($(checkAllSelector)[0].checked == true) {
                $(checkAllSelector)[0].checked = false;
            }
        }
    });
}
Then you can declare select/deselect all checkbox just a line code. Notice that the paramaters should be selector string. For example:
InitCheckAll('#yourMasterCheckBoxID', '.yourChildCheckBoxClass')

Posted in , | Leave a comment

Replace multi string in Javascript

There is only way is replacing the string by regular expression. For example:

s = s.replace(/yourPaternString/g, 'yourNewValue');
However, the patern string may not work when it contains metacharacter of regex, such as . $ ^ { [ ( | ) * + ? \. We need to escape these characters by following code:
patern = patern.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
text = text.replace(new RegExp(patern, 'g'), 'yourNewValue');

Leave a comment

Split string in SQL Server

Just run this function.
Notice that empty value between delimiter will return NULL, you can customize this by modify the line "insert into @tParts values( null )"

CREATE FUNCTION [dbo].[SplitString]
(
    @sString nvarchar(MAX),
    @cDelimiter nchar(1)
)
RETURNS @tParts TABLE ( part nvarchar(MAX) )
AS
BEGIN
    if @sString is null return
    declare @iStart int,
      @iPos int
    if substring( @sString, 1, 1 ) = @cDelimiter 
    begin
     set @iStart = 2
     insert into @tParts
     values( null )
    end
    else 
     set @iStart = 1
    while 1=1
    begin
     set @iPos = CHARINDEX(@cDelimiter, @sString, @iStart )
     if @iPos = 0
      set @iPos = len( @sString )+1
     if @iPos - @iStart > 0   
      insert into @tParts
      values ( substring( @sString, @iStart, @iPos-@iStart ))
     else
      insert into @tParts
      values( null )
     set @iStart = @iPos+1
     if @iStart > len( @sString ) 
      break
    end
    RETURN

END

Leave a comment

Search

Swedish Greys - a WordPress theme from Nordic Themepark. Converted by LiteThemes.com.