Après le view helper pour la progressbar de jQuery UI, je me suis amusé à coder un view helper pour les bouttons. Le principe reste le même, cependant j’ai ajouté un paramètre pour gérer le passage des évènements à la création du boutton.
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category ZendX
* @package ZendX_JQuery
* @subpackage View
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Slider.php 20165 2010-01-09 18:57:56Z bkarwin $
*/
/**
* @see ZendX_JQuery_View_Helper_UiWidget
*/
require_once "ZendX/JQuery/View/Helper/UiWidget.php";
/**
* jQuery Button View Helper
*
* @uses Zend_Json
* @package ZendX_JQuery
* @subpackage View
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class ZendX_JQuery_View_Helper_Button extends ZendX_JQuery_View_Helper_UiWidget
{
/**
* Create a jQuery button
*
* @link http://docs.jquery.com/UI/Button
* @param string $id
* @param string $value
* @param array $params
* @param array $attribs
* @param array $events
* @return string
*/
public function button($id, $value, array $params = array(), array $attribs = array(), array $events = array())
{
if(!isset($attribs['id'])) {
$attribs['id'] = $id;
}
$jqh = ZendX_JQuery_View_Helper_JQuery::getJQueryHandler();
$params = ZendX_JQuery::encodeJson($params);
$events = $this->getEvents($events);
$js = sprintf('%s("#%s").button(%s)%s;', $jqh, $attribs['id'], $params, $events);
$this->jquery->addOnLoad($js);
$html = "<button ".$this->_htmlAttribs($attribs)." >";
$html .= $value;
$html .= '</button>';
return $html;
}
public function getEvents($events)
{
if(!count($events)) return '';
else {
$retour = '';
foreach ($events as $event => $function) {
$retour .= '.'.$event.'('.$function.')';
}
return $retour;
}
}
}
Pour l’utiliser on place le fichier Button.php (avec le code ci-dessus) dans le dossier ZendX/JQuery/View/Helper/ et pour l’utiliser :
echo $this->button("button1", "Mon Button", array('icons' => new Zend_Json_Expr("{primary: 'ui-icon-gear'}")),
array(), array('click'=> "function(){ alert('Hello World !'); }" ));
Enjoy !




