我已經提出了一個問題,以開發joomla(2.5)模塊。我有自定義字段類型addlocation的xml文件。Joomla 2.5後端添加自定義字段使用jQuery和JFormField
<fieldset name="addLocations" label="Add Locations" addfieldpath="/modules /mod_pr_weather/elements">
<field type="addlocation" name="locations"></field>
</fieldset>
並具有addlocation.php文件,
<?php
defined('JPATH_BASE') or die;
jimport('joomla.form.formfield');
class JFormFieldAddlocation extends JFormField {
protected $type = 'Addlocations';
protected function getInput() {
$addFrom = '<div id="pr_maindiv"> <h1>'.JText::_('MOD_PR_WEATHER_HEADER_ADD_LOCATION').'</h1>'.$this->getForm("single").'</div>';
$addFromBlock = '<div id="pr_maindiv_b" style="clear:both;"><h1>'.JText::_('MOD_PR_WEATHER_HEADER_ADD_LOCATION').'</h1>'.$this->getForm("multi").'</div>';
$doc = JFactory::getDocument();
$doc->addScriptDeclaration('
jQuery(document).ready(function(){
jQuery("#pr_maindiv_b").css("display","none");
jQuery("#jform_params_slideshow_style").change(function(){
if(jQuery(this).val()==2){
jQuery("#pr_maindiv").css("display","none");
jQuery("#pr_maindiv_b").css("display","block");
}else{
jQuery("#pr_maindiv").css("display","block");
jQuery("#pr_maindiv_b").css("display","none");
}
});
});
');
return $addFrom . $addFromBlock;
}
private function getForm($type = 'single') {
$form_type_tooltip = 'class="hasTip" title="' . JText::_('MOD_PR_WEATHER_ADDLOCATION_TOOLTIP') . '"';
$from='';
if ($type=='single') {
$from = '<p><label'.$form_type_tooltip.'>'.JText::_('MOD_PR_WEATHER_ADDLOCATION_LABEL').'</label></p><p><input type="text" value="GEOLOCATION" placeholder="GEOLOCATION" name="single_location"/></p>
<p><input type="button" id="addMore" value="more"></p>';
}else{
$from = '<div style="clear:both;" id="first"><h3>Frist Block</h3><p><label'.$form_type_tooltip.'>'.JText::_('MOD_PR_WEATHER_ADDLOCATION_LABEL').'</label></p><p><input type="text" value="GEOLOCATION" placeholder="GEOLOCATION" name="first_block_loc"/></p>
<p><input type="button" id="addMoreFrist" value="more"></p></div>';
$from .= '<div style="clear:both;" id="second"><h3>Second Block</h3><p><label'.$form_type_tooltip.'>'.JText::_('MOD_PR_WEATHER_ADDLOCATION_LABEL').'</label></p><p><input type="text" value="GEOLOCATION" placeholder="GEOLOCATION" name="second_block_loc"/></p>
<p><input type="button" id="addMoreSecond" value="more"></p></div>';
$from .= '<div style="clear:both;" id="third"><h3>Third Block</h3><p><label'.$form_type_tooltip.'>'.JText::_('MOD_PR_WEATHER_ADDLOCATION_LABEL').'</label></p><p><input type="text" value="GEOLOCATION" placeholder="GEOLOCATION" name="third_block_loc"/></p>
<p><input type="button" id="addMoreThird" value="more"></p></div>';
$from .= '<div style="clear:both;" id="fourth"><h3>Fourth Block</h3><p><label'.$form_type_tooltip.'>'.JText::_('MOD_PR_WEATHER_ADDLOCATION_LABEL').'</label></p><p><input type="text" value="GEOLOCATION" placeholder="GEOLOCATION" name="fourth_block_loc"/></p>
<p><input type="button" id="addMoreFourth" value="more"></p></div>';
}
return $from;
}
}
而對於按鍵式觸發js文件:
jQuery("#addMore").click(function(){
jQuery("#pr_maindiv").append("<p style='clear:both;'><input type='text' value='GEOLOCATION' placeholder='GEOLOCATION' name='single_location[]'/></p>");
});
jQuery("#addMoreFrist").click(function(){
jQuery("div#first").append("<p style='clear:both;'><input type='text' value='GEOLOCATION' placeholder='GEOLOCATION' name='first_block_loc[]'/></p>");
});
jQuery("#addMoreSecond").click(function(){
jQuery("div#second").append("<p style='clear:both;'><input type='text' value='GEOLOCATION' placeholder='GEOLOCATION' name='second_block_loc[]'/></p>");
});
jQuery("#addMoreThird").click(function(){
jQuery("div#third").append("<p style='clear:both;'><input type='text' value='GEOLOCATION' placeholder='GEOLOCATION' name='third_block_loc[]'/></p>");
});
jQuery("#addMoreFourth").click(function(){
jQuery("div#fourth").append("<p style='clear:both;'><input type='text' value='GEOLOCATION' placeholder='GEOLOCATION' name='fourth_block_loc[]'/></p>");
});
當按鈕類型單擊事件發生,完美地追加到後端的形式。但是當保存點擊時,每個xml字段類型值都被保存,但是自定義字段類型不會保存到數據庫params列中。爲什麼?如果我的問題不清楚,請通知我上傳完整文件。 謝謝。
感謝先進。請你給我一些教程。 (請不要joomla文檔,因爲我想添加更多的選項,點擊add_more增加後端表單中輸入字段的數量)。 – 2013-03-11 12:37:57