Custom QTextBox with jQuery plugin

Login or register to post comments
4 replies [Last post]
wizard's picture
Offline
Joined: 11/30/2009

Hi folks!

I have a problem with firing events with a custom created TextBox, and I don't have much experience with creating a custom form control with a jquery plugin. The code of this TextBox looks like this:

<?php

class QAutoDateTextBox extends QTextBox {
       
        public function
__construct($objParentObject, $strControlId = null) {
            if (
$objParentObject)
               
parent::__construct($objParentObject, $strControlId);

        }
       
        public function
Validate() {
           
            return
parent::Validate();
        }       

        public function
GetScript(){
            if( !
$this->blnVisible )return '';
            if( !
$this->blnEnabled )return '';
           
            return
sprintf('$j("#%s").autodate()',$this->strControlId);          
        }

        public function
GetEndScript() {
            if( !
$this->blnVisible )return '';
            if( !
$this->blnEnabled )return '';
           
$strJavaScript = $this->GetScript();
           
            return
'$j().ready(function() {'.$strJavaScript.';});';
        }

        public function
__set($strName, $mixValue) {
           
$this->blnModified = true;
            switch (
$strName) {
               
                default:
                    try {
                       
parent::__set($strName, $mixValue);
                    } catch (
QCallerException $objExc) {
                       
$objExc->IncrementOffset();
                        throw
$objExc;
                    }
                    break;
            }
        }

        public function
__get($strName) {
            switch (
$strName) {
               
                default:
                    try {
                        return
parent::__get($strName);
                    } catch (
QCallerException $objExc) {
                       
$objExc->IncrementOffset();
                        throw
$objExc;
                    }
            }
        }
    }

?>

Here is a piece of code used in a form:

<?php
$txtDate
= new QAutoDateTextBox($this);
$txtDate->Name = 'Date';
$txtDate->AddAction(new QBlurEvent(), new QAjaxAction('doSomething'));
?>

The problem with this control is that it doesn't fires any event(action). I tried QBlurEvent, QFocusOutEvent, QFocusEvent...

Just a note about jquery plugin used, it works on blur event for more details about it http://manueljoaosilva.com/jquery-autodate-demo/

I'll be grateful for any help.

Offline
Joined: 04/22/2009

Hi,

Have you tried using Firebug to see whether you get any Javascript errors? I am not a great JQuery programmer, but I suspect that overriding $j().ready might cause this. Have a look in the QDateRangePicker plugin and how they handle the JQuery javascript. Looks quite elegant the way they did it.

Cheers

Helge

EDIT: BTW that would make a great plugin. ;-)

Offline
Joined: 02/02/2010

change your GetEndScript method to

public function GetEndScript() {
   $jscript = $this->GetScript();
   if($jscript)
       return  $jscript . '; ' . parent::GetEndScript();
   else
       return parent::GetEndScript();
}

should work. In QJqButton for example it is done the same way.
If it doesn't work try removing the if's in GetScript and in GetEndScript.

public function GetScript(){       
            return sprintf('$j("#%s").autodate()',$this->strControlId);         
        }

public function GetEndScript() {
       return  $this->GetScript() . '; ' . parent::GetEndScript();
}

wizard's picture
Offline
Joined: 11/30/2009

Thank you guys for replays, and you nailed it Mike! :)
I changed the GetEndScript method as you proposed, and actions are working now! Thank you again! :)

@Helge
Yes, I was thinking about wrapping it in a plugin, and contribute it to QCubed community :) Just need some free time and to think a little about how to do it, and maybe change the JavaScript a little bit.

(I was going through other plugins to see how did they created it, but I was looking on the wrong place...)

Offline
Joined: 04/22/2009

You are a champ ;-)

Cheers

Helge