Yii vs Highcharts vs Ajax - ajax

here is a part of my view:
<?php
echo CHtml::dropDownList('al_ev_id', $model->al_ev_id,$listaDateAl,
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('stat/ajaxStoricoSubDetails'),
'update'=>'#storicoSubDetails',
),
'class' => 'rowform',
));
...
?>
<div id="storicoSubdetails">
...
<?php
$this->Widget('ext.highcharts.HighchartsWidget', array(
'options'=>array(
'title' => array('text' => 'A title'),
'xAxis' => array(
'categories' => $model->streamLabel
),
'yAxis' => array(
'title' => array('text' => 'another title')
),
'series' => array(
array('name' => $model->labelGraph, 'data' => $model->streamData)
)
)
));
?>
...
</div>
the controller in the action 'ajaxStoricoSubDetails' reload the same $this->Widget('ext.highcharts.HighchartsWidget' etc, etc
the objective of this code is to update the starting x-value of the graph, modifying it with the dropdown
and here is my problem:
the first rendering of highcharts graph is ok; but, when I change the value in the DropDown, thus starting the ajax part, graph is not rendered
highcharts return an error 16 code, that means
Highcharts already defined in the page
in fact in the second rendering code (the ajax fired part) I find
<script src="http://code.highcharts.com/highcharts.js"></script>
that I think is the problem
is there any way to avoid this
Any other suggestions?

It's not the best long term solution, but...
If <script src="http://code.highcharts.com/highcharts.js"></script> is at the top of your page now, move it in <div id="storicoSubdetails"> instead.
That way the script reference should be overwritten when the ajax stuff reloads everything in the storicoSubdetails div, and you will only have 1 script reference on the page.

Related

Validate Select2 in Yii2 via AJAX

I have Yii2 application which uses the Kartik plugin to initialize Select2 dropdowns in forms.
I have one particular Select2 which uses AJAX call to get the data for the drop down options.
<?=
$form->field($model, 'court_house_id', ['enableAjaxValidation' => true, 'selectors' => ['input' => '#' . $id . "-court-house"],'template' => FormHelper::GenerateFieldTemplate([6])])
->widget(Select2::classname(), [
'options' => ['id' => $id . "-court-house", 'placeholder' => Yii::t('app', 'Search court house...')],
'hashVarLoadPosition' => \yii\web\View::POS_READY,
'pluginOptions' => [
'dropdownParent' => new JsExpression("$('#$modalWindowId')"),
'allowClear' => true,
'minimumInputLength' => 2,
'language' => [
'errorLoading' => new JsExpression("function () { return '" . Yii::t('app', 'Search...') . "'; }"),
],
'ajax' => [
'url' => app\components\UrlMaker::link('data/court-house-list'),
'dataType' => 'json',
'data' => new JsExpression('function(params) { return {q:params.term}; }')
],
'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
'templateResult' => new JsExpression('function(courthouse) { return courthouse.text; }'),
'templateSelection' => new JsExpression('function (courthouse) { return courthouse.text;}'),
]])
->label(Yii::t('app', 'Court House'), ['class' => FormHelper::GenerateLabelClassTemplate([3])]);
?>
Intentionally pasting all of the code, although most of it is irrelevant I would assume.
I have this loaded in multiple dynamically created forms thus all the strange ids and selectors. However, the form has different dropdown which controls whether some of the fields are shown (and required) or not. This particular field above is only shown in one of the scenarios which all the other variations of the form do not have it. So the model has the following validation:
[['court_house_id', 'staff'], 'required', 'on' => self::SCENARIO_ONE],
By the way staff is just a regular text field and everything works for it.
In order to change the scenario, I have the following line in the view with the form:
<?php $model->scenario = \app\models\MyModel::SCENARIO_ONE; ?>
The problem is that when I submit the form empty, the staff field gets marked in red as invalid but the court house is marked in green as valid although it is empty.
If I go into the model and remove the 'on' => self::SCENARIO_ONE part then everything works as expected - on empty submit the court house field also lights up in red but that would be a problem for the rest of my scenarios where this field is not needed.
Any ideas what might be causing the problem and how to resolve it?
Try to set the scenario in controller before calling save() method, for example
$model = new MyModel(['scenario' => MyModel::SCENARIO_ONE])

Codeigniter: creating helper with associative array

I have a number of social networking links (about 5) in the footer of my site and I need to generate data for each of the anchor link’s TITLE and URL - and ICON (image).
It’s not really worth creating a new table for only 5 rows, so I’m thinking a helper might be a the answer - some sort of associative array.
However, I’m a bit unsure how to construct the helper’s array - and completely clueless as to how to loop the results in the view file.
Any help, or any links to useful examples, is greatly appreciated!
In your helper:
function footer_link_data()
{
return array(
array(
'title' => 'Facebook',
'url' => 'http://facebook.com',
'icon' => 'http://placehold.it/64x64'
),
array(
'title' => 'Twitter',
'url' => 'http://twitter.com',
'icon' => 'http://placehold.it/64x64'
),
array(
'title' => 'Pinterest',
'url' => 'http://pinterest.com',
'icon' => 'http://placehold.it/64x64'
),
);
}
In your view:
<?php
$links = footer_link_data();
foreach($links as $link): ?>
<?php echo $link['icon'] ?>
<?php
endforeach; ?>
Alternatively, you can just create the HTML statically. For 5 links having it generated dynamically is kind of pointless.

Drupal 7 Ajax Forms - select element

I've got a custom module form in Drupal 7. The code looks like this
function form_example_dynamic_form($form, &$form_state) {
$form['tables'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array("2012", "2013")),
'#title' => t('Select year:'),
'#ajax' => array(
'callback' => 'form_example_dynamic_myajax',
'wrapper' => 'abcd',
'effect' => 'fade',
'method' => 'replace',
),
);
return $form;
}
function form_example_dynamic_myajax($form, $form_state) {
return $form_state['values']['tables'];
}
So this simple code should update #abcd tag with the value of the 'tables' select. And the problem is that it works only for the first selection. If I chose 2013 it returns "2013" text in my #abcd tag. But, when I chose 2012, an #abcd tag stays unchanged. It still apperas 2013.
Here is the example: http://kuzaj.yamandi.com/pl/form_example_dynamic
Does anyone has any idea how to solve it?
So the 'method' => 'replace' attribute REPLACES whole wrapper into new text. Example:
There is eg div:
First choice (eg 2013) changes it to (note that there is no more an #abcd tag):
2013
Then again if the form element is changed it searches for an #abcd tag but there is nothing like that becouse it has been already replaced with "2013". So instead of using 'replace' method it should be 'html'. Html method replace content of tag, but the tag stays "untouched".

How to translate form labels in Zend Framework 2?

I'm not getting it!.. Can please someone explain, how to translate form labels? A simple example would be great.
Thank you in advance!
class Search\Form\CourseSearchForm
...
class CourseSearchForm extends Form {
...
public function __construct(array $cities) {
parent::__construct('courseSearch');
...
$this->add(array(
'name' => 'city',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Stadt',
'value_options' => $this->cities,
'id' => 'searchFormCity',
),
));
...
}
}
view script /module/Search/view/search/search/search-form.phtml
<?php echo $this->form()->openTag($form); ?>
<dl>
...
<dt><label><?php echo $form->get('city')->getLabel(); ?></label></dt>
<dd><?php echo $this->formRow($form->get('city'), null, false, false); ?></dd>
...
</dl>
<?php echo $this->form()->closeTag(); ?>
<!-- The formRow(...) is my MyNamespace\Form\View\Helper (extends Zend\Form\View\Helper\FormRow); the fourth argument of it disables the label. -->
The module/Application/config/module.config.php is configured:
return array(
'router' => ...
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),
'translator' => array(
'locale' => 'de_DE',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => ...
'view_manager' => ...
);
I also edited my view and use the FormLabel view helper:
<dt><label><?php echo $this->formLabel($form->get('city')); ?></label></dt>
Furthermore I debugged the FormLabel at the place, where the tranlator is used (lines 116-120) -- seems to be OK.
But it's still not working.
EDIT
The (test) items for labels, I added to the de_DE.po file manually, are tranlated. The ZF2 side problem was actually, that I was using $form->get('city')->getLabel() instead of $this->formlabel($form->get('city')) in th view script.
The problem is now, that the labels are not added to the de_DE.po file. But it's not a ZF2 issue anymore, so I've accept Ruben's answer and open a new Poedit question.
Instead of using:
<?php echo $form->get('city')->getLabel(); ?>
You should use the formlabel view helper. This helper automatically uses your translator during rendering if you have inserted it in your ServiceManager. Most likely you will have it in your Application's module module.config.php:
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
Once you do use the formlabel view helper:
echo $this->formLabel($form->get('city'));
And of course make sure your translations are in your .po file.
i think your problem is that you label are not detected by poedit (or similar tool), so you have to add them manually to your poedit catalogs (.po)
to make your label strings detected by tools like poedit, your strings need to be used inside a translate() function or _() (other function can be added in Catalog/properties/sources keyword)
as the _() function is not user in ZF2 (today) so a tiny hack is to add a function like this in your index.php (no need to modify anything, this way, in poedit params):
// in index.php
function _($str)
{
return $str;
}
and in your code, just use it when your strings are outside of a translate function
//...
$this->add(array(
'name' => 'city',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => _('myLabel') , // <------ will be detected by poedit
'value_options' => $this->cities,
'id' => 'searchFormCity',
),
));
//...
or like this if you prefer
$myLabel = _('any label string'); // <--- added to poedit catalog
//...
'options' => array(
'label' => $myLabel ,
'value_options' => $this->cities,
'id' => 'searchFormCity',
),
#Ruben says right!
Me I use PoEdit to generate my *.mo files and to be sure to get all translations in the file, I create somewhere (in view for example) a file named _lan.phtml with all text to be translated :
<?php echo $this->translate("My label");
... ?>
Of course, Poedit has to be configured to find my keywords. check this to how to configure it
All solutions don't use the power of ZF2. You must configure your poedit correctly :
All things are here :
http://circlical.com/blog/2013/11/5/localizing-your-twig-using-zend-framework-2-applications

cakephp passing selection to JS Helper

I want to get the selected schoolFilter form value and place in [SELECTED VALUE HERE].
$data = $this->Js->get('#SchoolFilter')->serializeForm(array('isForm' => true, 'inline' => true));
$this->Js->get('#SchoolSchoolId')->event(
'change', $this->Js->request(
array('action' => 'assign_school_ads/', [SELECTED VALUE HERE], array(
'update' => '#results',
'data' => $data,
'async' => true,
'dataExpression' => true,
'method' => 'POST'
)
)
);
// School Filter
$schoolFilter = $this->Form->create('School', array('id' => 'SchoolFilter');
$schoolFilter .= $this->Form->input('school_id', array('label'=>'Schools', 'empty'=>'- select -');
$schoolFilter .= $this->Form->end();
I have seen variations on this question but without any clear answer, except to just forget using JS Helper. Is it possible within the context of JS Helper? And if not, can I get the value using regular JQuery, then inject it into JS Helper.
Use following code :-
$this->Js->get('#SchoolFilter');
$this->Js->event('change', $this->Js->request(array('controller'=>'put-ur-controller-ame-here','action' => 'assign_school_ads'),array('async' => true,'update' => '#results','method' => 'post','dataExpression'=>true,'data'=> $this->Js->serializeForm(array('isForm' => true,'inline' => true)))));
the serialize() function sends the form data to the php action so we can see which option was selected and decide what to update in the ajax call.
the form data will be found in $this->data in the action (just like after a form has been submited).
Don't forget to add $this->Js->writeBuffer(); in your layout just before the body closing tag. Otherwise all the ajax code will not be added to your page.

Resources