Joomla! (DAY 10) - Joomsport (DAY 8): Create my own JElement
来源:互联网 发布:电子商务网店美工 编辑:程序博客网 时间:2024/05/17 06:22
This is a trial on how to create a new JElement for menu type parameter: 'mon'.
Before the view can receive the parameter, we need a interface for user to set the parameter; before that, there should be a list for user to pick up value for that parameter, means parameter values should be formed first of all.
Step 1: Add one function to file '/administrator/components/com_joomsport/admin.joomsport.html.php', let's name it 'bl_VintestMenu', and it is responsible to output the HTML of the value list:
function bl_VintestMenu( $rows, $option ){$jsf = JRequest::getVar('function', 'jSelectArticle', '', 'string');JHTML::_('behavior.tooltip');?><table class="adminlist"><thead><tr><th width="2%" align="left"><?php echo JText::_( 'Num' ); ?></th><th class="title"><?php echo JText::_( 'Name' ); ?></th></tr></thead><tbody><?php$k = 0;if( count( $rows ) ){for ($i=0, $n=count( $rows ); $i < $n; $i++){$row = $rows[$i];JFilterOutput::objectHtmlSafe($row);?><tr class="<?php echo "row$k"; ?>"><td><?php echo $i+1; ?></td><td><a href="javascript:window.parent.<?php echo $jsf;?>('<?php echo $row->num?>', '<?php echo htmlspecialchars($row->name, ENT_QUOTES, 'UTF-8')?>', 'mon');"><?php echo $row->name; ?></a></td></tr><?php}}?></tbody></table><?php}
This function receive an array, within which each element should have two field: 'num', and 'name'. So next up is to generate these data. One point: the javascript function 'jSelectArticle' will use its 3rd parameter 'mon' as the parameter's name.
Step 2: Add one function to generate (parameter) value list to file '/administrator/components/com_joomsport/admin.joomsport.php':
function BL_Vintest_Menu($option){$monChar = "月";$monNum = array(9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7);$length = count($monNum);$row = array();for ($i=0; $i<$length; $i++){$month = new stdClass;$month->num = $monNum[$i];$month->name = $monNum[$i] . $monChar;$row[] = $month;}joomsport_html::bl_VintestMenu($row, $option);}
This function generate an array (called '$row') of object '$month', which is 'stdClass' type, this is for simulating the data queried from database. And stdClass is a trick to get C style struct in PHP, or call it associative array. And array '$row' will be passed to 'joomsport_html::bl_VintestMenu'.
Step 3: Add a task for mapping 'BL_Vintest_Menu', the task is called 'vintest_menu':
switch (JRequest::getCmd('task')){...// test by Vincentcase 'vintest_menu': BL_Vintest_Menu($option); break;...}
At this point, we can take a test, this list will be popped up by clicking parameter element's button:
Step 4: create file '/administrator/components/com_joomsport/elements/vintest.php', since the file name is 'vintest', the class it holds should be 'JElementVintest'.
<?phpdefined('_JEXEC') or die( 'Restricted access' );class JElementVintest extends JElement{var $_name = 'vin-test';function fetchElement($name, $value, &$node, $control_name){global $mainframe;$db=& JFactory::getDBO();$doc =& JFactory::getDocument();$template = $mainframe->getTemplate();$fieldName= $control_name.'['.$name.']';$article->title = '';$monChar = "月";if($name == 'mon'){if ($value){$article->title = $value . $monChar;}else{$article->title = JText::_('Select Month');}$task = 'vintest_menu';}$js = "function jSelectArticle(id, title, object) " ."{document.getElementById(object + '_id').value = id;document.getElementById(object + '_name').value = title;document.getElementById('sbox-window').close();}";$doc->addScriptDeclaration($js);$link = 'index.php?option=com_joomsport&task='.$task.'&tmpl=component&object='.$name;JHTML::_('behavior.modal', 'a.modal');$html = "\n".'<div style="float: left;"><input style="background: #ffffff;" type="text" id="'.$name.'_name" value="'.htmlspecialchars($article->title, ENT_QUOTES, 'UTF-8').'" disabled="disabled" /></div>';$html .= '<div class="button2-left"><div class="blank"><a class="modal" title="'.$article->title.'" href="'.$link.'" rel="{handler: \'iframe\', size: {x: 650, y: 375}}">'.JText::_('Select').'</a></div></div>'."\n";$html .= "\n".'<input type="hidden" id="'.$name.'_id" name="'.$fieldName.'" value="'.(int)$value.'" />';return $html;}}
There is some points:
the '$task' is 'vintest_menu', match the task we just added;
the '$name' is expected to be 'mon', which is our parameter name;
Step 5: Create a view that utilizes this parameter, I will reuse my previous created view, it is called 'test'.
To make use of the 'mon' parameter, we need to modify '/components/com_joomsport/views/test/tmpl/default.xml':
<?xml version="1.0" encoding="utf-8"?><metadata><layout title="Test Layout"><message><![CDATA[Test Layout]]></message></layout><state><name>Test Layout</name><description>Test Layout</description><url addpath="/administrator/components/com_joomsport/elements"><param name="mon" type="vintest" default="0" label="Select Vintest" description="VinTest" /></url><params></params></state></metadata>
Then, we can take a loot at the parameter in action:
Step 6: receive the parameter 'mon' in the view 'test'
<?phpdefined( '_JEXEC' ) or die( 'Restricted access' );jimport( 'joomla.application.component.view');class bleagueViewtest extends JView{function display($tpl = null){$mon = JRequest::getVar( 'mon', 0, '', 'int' );$s = "This is a test string";$this->assignRef('testStr',$s);$this->assignRef('mon', $mon);parent::display($tpl);}}
Step 7: show the data in template.
<?php // no direct accessdefined('_JEXEC') or die('Restricted access'); ?> <table border="1px"> <tr> <th>header0</th><th>header1</th><th>header2</th> </tr> <tr> <td><?php echo $this->testStr ?></td><td></td><td></td> </tr> <tr> <td></td><td></td><td></td> </tr> <tr> <td></td><td><?php echo $this->mon ?></td><td></td> </tr> </table>
REFS:
http://www.webmaster-source.com/2009/08/20/php-stdclass-storing-data-object-instead-array/
- Joomla! (DAY 10) - Joomsport (DAY 8): Create my own JElement
- Joomla! (DAY 8) - Joomsport (DAY 6):
- Joomla! (DAY 6) - Joomsport (DAY 4): Tackling with View's Parameters - JElement
- Insight Joomla! (DAY 5) - Examing Joomsport (DAY 3): My First View
- Joomla! (DAY 9) - Joomsport (DAY 7): Create a Real View 'Home VS Away'
- Joomla! (DAY 7) - Joomsport (DAY 5): Localization - Adding Traditional Chinese
- Insight Joomla! (DAY 4) - Examing Joomsport (DAY 2) - they are the same DAY!
- My day
- My First Day
- My first day!
- MY LUCKY DAY
- MY LUCKY DAY
- My first blog day
- My first day here.
- My working day !
- My first day
- This my first day!
- My First Day
- hibernate inverse 作用 inverse="true" 作用
- GSM、GPRS、EDGE、2G、3G与WAP的关系
- 装饰者模式(Decorator)(04)
- 64位Windows2003 Enterprise sp2 上64位Oracle10.2.0.1升级到Oracle10.2.0.3具体步骤
- 浏览器内核和种类
- Joomla! (DAY 10) - Joomsport (DAY 8): Create my own JElement
- 电脑环境出问题了
- 解决 android自带API Demos MeidaPlayer无法播放视频
- window中对ubuntu通过samba共享出来的文件没有写权限/无法修改
- Oracle to_char格式化函数
- 一份网站的商业计划书内容概要
- 虚拟主机cPanel面板查看服务器的访问日志
- oracle 外部表使用小结
- eclipse pydev source code 查看技巧