Yii CJuiAutoComplete default display value and clearing it on click - autocomplete

I have below CJuiAutoComplete and when loading I want to display "Search" in the text field and on click I want to clear . I tried using "value" under options , but couldn't make it work . Thanks for your help
tried also
'htmlOptions'=>array('value'=>'Search',)
<?php
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'test1',
'source'=>'js: function(request, response) {
$.ajax({
url: "'.$this->createUrl('myAutoComplete/autoCompleate').'",
dataType: "json",
data: {
term: request.term,
brand: $("#type").val()
},
success: function (data) {
response(data);
}
})
}',
'options' => array(
'showAnim' => 'fold',
'select' => 'js:function(event, ui){ alert(ui.item.value) }',
'click'=>'js:function( event, ui ) {
alert("test");
return false;
}',
),
'htmlOptions'=>array('value'=>'Search',)
));
?>
Regards
UPDATE
directly putting 'value' =>'Search' worked .
Checking for click handler
Kiran

What you can do is give your widget an id and then you place the onClick event in the widget's htmlOptions and using JavaScript you clear the value.
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'id' => 'test1_id',
'name'=> 'test1',
'source'=>'js: function(request, response) {
$.ajax({
url: "'.$this->createUrl('myAutoComplete/autoCompleate').'",
dataType: "json",
data: {
term: request.term,
brand: $("#type").val()
},
success: function (data) {
response(data);
}
})
}',
'options' => array(
'showAnim' => 'fold',
'select' => 'js:function(event, ui){ alert(ui.item.value) }',
),
'htmlOptions' => array(
'onClick' => 'document.getElementById("test1_id").value=""'
)
));
You cannot put onClick in the options attribute as these are jQuery options for the CJuiAutocomplete, onClick is not defined in the JUI Autocomplete options.
Cheers

Old thread, but for newbies who land up here, its simple to add a html placeholder attribute in Yii CAutoComplete. See code below and add in the htmloptions line:
<?php $this->widget('CAutoComplete', array(
'model'=>$model,
'attribute'=>'tags',
'url'=>array('suggestTags'),
'multiple'=>true,
'htmlOptions'=>array('size'=>50,'placeholder'=>'Seperate tags with commas'),
)); ?>

Related

3 select in a form with Symfony

(sorry for my english...)
I need your help for my 3 selects in one form.
Select 1: I choose Pays
Select 2: I choose Regions who depend for the Pays
Select 3 : I choose Towns who depends Region
Actually I use javascript in Twig template to have options in the different select, but when I submit the Form, I doesn't have a value for the ville parameter in my object.
Here is my code for ville in the Form:
->add('ville', ChoiceType::class, [
'label' => 'Ville:',
'required' => true,
])
dd($partenaire)
console detail select after query
I have do with EntityType, but this load all the table...
Can someone help me ?
Tanks
I think you should add 2 new routes which return html, specifically the select HTML element with the data from the selected parent, ex:
#[Route('/regions-select', name: 'regions_select', methods: ['GET'])]
public function getRegionsSelect(Request $request): Response
{
$entity = new Entity();
$form = $this->createForm(
FooFormType::class,
$entity,
['pay' => $request->query->get('pay')]
);
return $this->render('_regions-select.html.twig', [
'form' => $form->createView(),
]);
}
Then the Form can look like this:
$builder
->add('district', EntityType::class, [
'placeholder' => 'Select Pay',
'label_attr' => ['class' => 'mb-0 d-block'],
'class' => Pay::class,
'choice_value' => 'id',
'choice_label' => 'name',
])
->add('region', EntityType::class, [
'class' => Region::class,
'query_builder' => function (RegionRepository $regionRepository) use ($options) {
return $regionRepository->regionsByPay((int) $options['pay']);
},
'choice_label' => 'name',
'choice_value' => 'id'
]);
You can create the twig file to render the form row for the regions select '_regions-select.html.twig':
{{ form_row(form.region, {
attr: {
'class': 'mb-3 form-control'
}
}) }}
And you can achieve this by listening to the change event or select event on the Pays dropdown via javascript like this:
$paySelect.on('change', (event) => {
const payId = event.target.value;
$.ajax({
url: BASE_URL+'/regions-select',
data: {
pay: payId,
},
success: function (html) {
const region = REGION_SELECT_FROM_CLASS_NAME //replace this with your jQuery/Javascript selector;
region.html($(html).find("option"));
region.val("").trigger("change");
},
});
});

How to set id for AJAX

I have following Jquery-Script, which works fine outside of yii2
<script>
$(function () {
$(".comment_button").click(function () {
var element = $(this);
var test = $("#_id_").val();
var dataString = '_id_=' + test;
if (test == '')
{
alert("No record, no action...!");
} else
{
$.ajax({
type: "POST",
url: '<?=\Yii::$app->urlManager->baseUrl?>/insert.php',
//url: 'insert.php',
data: dataString,
cache: false,
/*
success: function (html) {
$("#display").after(html);
document.getElementById('_id_').value = '';
*/
}
});
}
return false;
});
});
</script>
It works,'cause I have following html input box:
<textarea cols="30" rows="2" style="width:480px;font-size:14px; font-weight:bold" id="_id_" maxlength="145" ></textarea>
As u can see, jquery will take id= _ id _ in order to use AJAX.
I try to realize same thing with yii2, but I get following error:
bewerber_update?id=2:1925 Uncaught TypeError: Cannot set property 'value' of null
at Object.success (bewerber_update?id=2:1925)
at fire (jquery.js:3187)
at Object.fireWith [as resolveWith] (jquery.js:3317)
at done (jquery.js:8757)
at XMLHttpRequest.<anonymous> (jquery.js:9123)
This error will be thrown out,' cause I don't konw, how to set id correctly in yii2. I tried like this, but this is obviously the wrong way:
<?=
$form->field($model, 'beurteilung_fachlich')->widget(\dosamigos\ckeditor\CKEditor::className(), [
'id'=>'_id_',
'options' => ['rows' => 1],
'preset' => 'full'
])
?>
Any ideas, how to set id in a correct way?
You should pass id in options
<?= $form->field($model, 'beurteilung_fachlich')->widget(\dosamigos\ckeditor\CKEditor::className(), [
'options' => ['rows' => 1, 'id' => '_id_'],
'preset' => 'full'
]) ?>

Yii2 Modal Dialog on Gridview update button not load the update form

With reference to How to implement Yii2 Modal Dialog on Gridview view and update button?. I did my code below, but it popups a window with no update form. could anyone please let me know the correct code? (i use Kartik Gridview)
This is the code for column:
['class' => '\kartik\grid\ActionColumn',
'template'=>'{update}{delete}{view}',//{view}'//{view}{delete}',
'headerOptions' => ['width' => '20%', 'class' => 'activity- update-link',],
'contentOptions' => ['class' => 'padding-left-5px'],
'buttons' => [
'update' => function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-pencil"></span>','/projects/update?id='.$key.'', [
'class' => 'activity-update-link',
'title' => Yii::t('yii', 'Project Update'),
'data-toggle' => 'modal',
'data-target' => '#activity-modal',
'data-id' => $key,
'data-pjax' => '0',
]);
},
],
],
This is JS code:
<?php $this->registerJs(
'
function init_click_handlers(){
$(".activity-update-link").click(function(e) {
var fID = $(this).closest("tr").data("key");
$.get(
"update",
{
id: fID
},
function (data)
{
$("#activity-modal").find(".modal-body").html(data);
$(".modal-body").html(data);
$("#activity-modal").modal("show");
}
);
});
}
init_click_handlers(); //first run
$("#project_pjax_id").on("pjax:success", function() {
init_click_handlers(); //reactivate links in grid after pjax update
});
');
?>
<?php
Modal::begin([
'id' => 'activity-modal',
'header' => '<h4 class="modal-title">Update Project Testing</h4>',
'size'=>'modal-lg',
'footer' => '<a href="#" class="btn btn-primary" data-
dismiss="modal">Close</a>',
]); ?>
<div class='well'></div>
<?php Modal::end(); ?>
Thanks all for watching my question. I solved my issue by adding modal into main.js file of the assets folder using class instead of ID.

Module Forms - Store Value of addField to a Variable

I have something like in my custom module:
$fieldset->addField('orderinfo', 'link', array(
'label' => Mage::helper('web')->__('Order Info'),
'style' => "",
'href' => Mage::helper('adminhtml')->getUrl('adminhtml/sales_order/view', array('order_id' => $order_id)),
'value' => 'Magento Blog',
'after_element_html' => '',
));
And as you can see from the code, I am trying to link that field to the Order Tab in the back-end. I'm having trouble getting the ID though. I'm planning to just save the Order ID in the database, and then using the addField I could have the correct url.
But how can I save an addField value to a variable?
I want to store the value in "$order_id".
Is it possible?
I am not sure in which context you are using this fieldset but if it is used for example for creating or editing object you can try something like that:
In controller:
public function editAction()
{
$id = $this->getRequest()->getParam('id');
$model = Mage::getModel('module/model')->load($id);
Mage::register('model_name', $model);
}
and then in the block:
protected function _prepareForm()
{
$model = Mage::registry('model_name');
// add fieldset to form
$fieldset->addField('orderinfo', 'link', array(
'label' => Mage::helper('web')->__('Order Info'),
'style' => "",
'href' => Mage::helper('adminhtml')->getUrl('adminhtml/sales_order/view', array('order_id' => $model->getOrderId())),
'value' => 'Magento Blog',
'after_element_html' => '',
));
//rest of the elements
}
Answering my own post again. (src: https://magento.stackexchange.com/questions/682/module-forms-store-value-of-addfield-to-a-variable)

Zend_Form how to put <a> behind input text?

I try to put some HTML link behind input text and I try to do it's somthing like this:
$aElements[$iKey] = $oName = new Zend_Form_Element_Text($aValue['newsletter_question_answer_id']);
$oName->addDecorator('HtmlTag', array(
'tag' => 'a',
'href'=>'http://some_url.html',
'placement' => Zend_Form_Decorator_Abstract::APPEND
));
and my question is how can I put somthing between <a> and </a> ?
Best Regards
If You don't want to write Your own decorator You have to use callback:
$element->addDecorator('Callback', array(
'callback' => function($content, $element, $options) {
Zend_Debug::dump($content, 'content'); //elements decorated so far
Zend_Debug::dump($element, 'element'); //current element
Zend_Debug::dump($options, 'options'); //other options
return "{$options['label']}";
},
'option' => 'value', //everything but 'callback' and 'placement' gets
//passed to callback as option
'href' => 'http://example.com',
'label' => 'Link!',
'placement' => Zend_Form_Decorator_Abstract::APPEND
));
Ofcourse it's php5.3 style callback, but You can use oldstyle too.