Yii2 - Data Not Coming To vova07 / yii2-imperavi-widget (textarea) - php

I've just installed vova07 / yii2-imperavi-widget through php composer.phar require --prefer-dist vova07/yii2-imperavi-widget "*".
And, it's working fine for inserting any content to database table. But, I'm in awkward situation when fetching data into this this widget
I want to fetch the data. So, I created one 'value' => $postDetails['desc']. Data Not Coming to it.
<?= $form->field($modelEditPost, 'desc')->widget(Widget::className(), [
'settings' => [
'value' => $postDetails['desc'],
'lang' => 'ru','minHeight' => 200,'plugins' => ['clips','fullscreen']]
]);?>
But, when i'm including
<?= $form->field($modelEditPost, 'desc')->textarea(['rows' => 6,'value'=>$postDetails['desc']]) ?>
(which was previously present to fetch data) along with widget, Data coming to widget (But, not in textarea))
<?= $form->field($modelEditPost, 'desc')->textarea(['rows' => 6,'value'=>$postDetails['desc']]) ?>
<?= $form->field($modelEditPost, 'desc')->widget(Widget::className(), [
'settings' => [
'value' => $postDetails['desc'],
'lang' => 'ru','minHeight' => 200,'plugins' => ['clips','fullscreen']]
]);?>
Actually, I want data to be fetched into widget.
Please help me to rectify this issue.

Use $modelEditPost->desc = $postDetails['desc']:
$modelEditPost->desc = $postDetails['desc'];
<?= $form->field($modelEditPost, 'desc')->widget(Widget::className(), [
'settings' => ['lang' => 'ru','minHeight' => 200,'plugins' => ['clips','fullscreen']]
]);?>

Related

Select2 keep showing "Searching" in ajax with pivot (n-n) in Laravel Backpack 5 Pro

I have 2 models: Order and Product, each has belongsToMany with pivot set properly.
In OrderCrudController, I also use FetchOperation and make a fethProducts() function as below:
use \Backpack\CRUD\app\Http\Controllers\Operations\FetchOperation;
public function fetchProducts()
{
return $this->fetch([
'model' => \App\Models\Product::class,
'searchable_attributes' => ['name','sku']
]);
// return $this->fetch(\App\Models\Product::class); <-- I also tried this one
}
protected function setupCreateOperation()
{
CRUD::setValidation(OrderRequest::class);
// other fields
$this->crud->addField([
'name' => 'products',
'type' => 'relationship',
'pivotSelect' => [
'attribute' => 'name',
'ajax' => true,
],
'subfields' => [
[
'name' => 'quantity',
'type' => 'number',
],
],
]);
}
But it comes to unexpected behavior when I search the product, the select2 field remains "searching" though the request successfully retrieved the data.
screenshot - select2 field
screenshot - ajax results
PS: this field works perfectly without subfields, no vendor overrides etc., so I think I've set everything correctly.
Anyone can help?
This was asked two months ago but somehow I missed it, just noticed it today after someone opened an issue on the GitHub repository.
I am happy you guys found a solution for it but unfortunatelly I cannot recommend it as using the select2_from_ajax will miss the functionality to don't allow the selection of the same pivots twice, otherwise you will have undesired consequences when saving the entry.
I've just submitted a PR to fix this issue, I will ping you guys here when it's merged, probably by next Monday.
Cheers
I just came accross this exact problem and after quite some research and trials, i finally found a solution !
The problem seems to be related to the relationship field type inside the pivotSelect. Try to use select2_from_ajax instead and don't forget to set method to POST explicitly, that worked for me like a charm.
Here is what you might try in your case :
$this->crud->addField([
'name' => 'products',
'type' => 'relationship',
'pivotSelect' => [
'attribute' => 'name',
'type' => 'select2_from_ajax',
'method' => 'POST',
'data_source' => backpack_url('order/fetch/products') // Assuming this is the URL of the fetch operation
],
'subfields' => [
[
'name' => 'quantity',
'type' => 'number',
],
],
]);

Yii2 - Only one select get options

I got an issue. I have 3 select inputs, that i want to fill in with same options. First input gets options, but another two don't. I've tried everything. Last thing, that i tried was select 3 different queries and fill each one separately. Unfortunately, i get same issue.
Thanks for advices.
Controller
$dataPbxObj1 = Sipend::find()
->select('cc_sip_end.*')
->leftJoin('cc_reseller_to_pbx', '`cc_reseller_to_pbx`.`ID_PBX` = `cc_sip_end`.`id`')
->where(["in", "cc_reseller_to_pbx.id_cc_reseller", $reseller->id_cc_reseller])->all();
$dataPbxObj2 = Sipend::find()
->select('cc_sip_end.*')
->leftJoin('cc_reseller_to_pbx', '`cc_reseller_to_pbx`.`ID_PBX` = `cc_sip_end`.`id`')
->where(["in", "cc_reseller_to_pbx.id_cc_reseller", $reseller->id_cc_reseller])->all();
$dataPbxObj3 = Sipend::find()
->select('cc_sip_end.*')
->leftJoin('cc_reseller_to_pbx', '`cc_reseller_to_pbx`.`ID_PBX` = `cc_sip_end`.`id`')
->where(["in", "cc_reseller_to_pbx.id_cc_reseller", $reseller->id_cc_reseller])->all();
$dataPbx1 = ArrayHelper::map($dataPbxObj1,'id','popis');
$dataPbx2 = ArrayHelper::map($dataPbxObj2,'id','popis');
$dataPbx3 = ArrayHelper::map($dataPbxObj3,'id','popis');
View (all this selects are same)
<?=$form->field($modelSip, 'ID_PBX')->widget(Select2::className(),
["data" => $dataPbx3,'hideSearch' => true]) ?>
You probably needs to use unique IDs - by default Yii generates ID based on field name, but with 3 identical fields, IDs will be the same, and Select2 init will apply only for first of them.
<?=$form->field($modelSip, 'ID_PBX')->widget(Select2::className(), [
'options' => ['id' => 'ID_PBX1'],
'data' => $dataPbx,
'hideSearch' => true,
]) ?>
<?=$form->field($modelSip, 'ID_PBX')->widget(Select2::className(), [
'options' => ['id' => 'ID_PBX2'],
'data' => $dataPbx,
'hideSearch' => true,
]) ?>
<?=$form->field($modelSip, 'ID_PBX')->widget(Select2::className(), [
'options' => ['id' => 'ID_PBX3'],
'data' => $dataPbx,
'hideSearch' => true,
]) ?>
BTW: you don't need to query options list 3 times, you can do it once and use the same result in 3 fields.

How to pass php variable to PluginEvent config Slider Kartik extension for Yii2

I use Slider Kartik extension for Yii2. There is PluginEvent options, that i want to config to my needs. So here is my code:
<?php echo Slider::widget([
'name'=>'rating_1',
'value'=>7,
'sliderColor'=>Slider::TYPE_GREY,
'pluginEvents' => [
'slide' => "function(slideEvt) {
$('#testVal. ').text(slideEvt.value);
}",
],
]);
?>
<span>Current Slider Value: <span id="testVal">3</span></span>
But in my HTML layout i used models attribute like: $value['strategy_title']
Is there is the way to put my $value['strategy_title'] instead of #testVal?
You need to concatenate php variable,
<?= Slider::widget([
'name' => 'rating_1',
'value' => 7,
'sliderColor' => Slider::TYPE_GREY,
'pluginEvents' => [
'slide' => "function(slideEvt) {
$('#".$value['strategy_title']."').text(slideEvt.value);
}",
],
]) ?>

How can I make typeahead show database information in yii2?

I want to make search in my project. I use typeahead but it's not working. This is my code:
<?php
echo '<label class="control-label">Select Repository</label>';
$template = '<div><p class="repo-language">{{no_telepon}}</p>' .
'<p class="repo-name">{{nama}}</p>' .
'<p class="repo-description">{{email}}</p></div>';
echo Typeahead::widget([
'name' => 'twitter_oss',
'options' => ['placeholder' => 'Filter as you type ...'],
'dataset' => [
[
'prefetch' => Penerima::find()->all(),
'datumTokenizer' => "Bloodhound.tokenizers.obj.whitespace('value')",
'display' => 'value',
'templates' => [
'notFound' => '<div class="text-danger" style="padding:0 8px">Unable to find repositories for selected query.</div>',
'suggestion' => new JsExpression("Handlebars.compile('{$template}')")
]
]
]
]);
?>
This question was asked long time a go.
I also faced the same problem, but i could figure-out this.
for future reference i add this post.
in your controller
$result = SampleModel::find()
->select('Attribute_name')
->where('name LIKE "%' . $searchParameter .'%"')
->asArray()
->all();
return Json::encode($result);
here you need to get the database value as "associative array", you can get that from using "asArray()".
then as you see return value as Json encode.
in your "View"
<?php
echo Typeahead::widget([
'name' => 'sampleName',
'options' => ['placeholder' => 'Filtering data ...'],
'scrollable' => true,
'pluginOptions' => ['highlight'=>true],
'dataset' => [
[
'remote' => [
'url' => Yii::$app->urlManager->createUrl(['sample/action']) .
'?searchParameter=%QUERY',
'wildcard' => '%QUERY'
],
'datumTokenizer' => "Bloodhound.tokenizers.obj.whitespace('Atribute_name')",
'display' => 'Atribute_name',
'limit' => 10,
],
],
'pluginEvents' => [
'typeahead:select' => 'function(e, s) {
EnableUserDetailsTypeAhead(s);
}',
]
]);
?>
here few things to be consider.
calling to the controller action. you can do that.
Yii::$app->urlManager->createUrl(['sample/action']) .
'?searchParameter=%QUERY',
'wildcard' => '%QUERY'
],
the below lines inside data set must be provide.
'datumTokenizer' => "Bloodhound.tokenizers.obj.whitespace('Atribute_name')",
'display' => 'Atribute_name',
you will get the expected data.
this sample code i have tested and this is working
From the docs:
prefetch: array, configuration for the prefetch options object. Refer documentation for the options you can set for this parameter. The return data must be Json encoded and converted to an associative array of the format [['value' => 'data1'], ['value' => 'data2'],...], where value is the fixed key set in display
You are passing an array of objects instead of an array of key value pairs. You can use asArray to create a list of objects. You will need to change display to the name of the field containing the data:
'prefetch' => Penerima::find()->select('title')->asArray()->all(),

how to use Yii Eselect2 to save more data?

I use Eselect2 Yii extension to give users multiple choice but only last choice is submitted via POST. Why?
This is my html, in which I also tried to manage all choices with array but without success
<pre>echo $form->labelEx($model,'city_id');
$this->widget('ext.select2.ESelect2', array(
'name' => 'Form[field]',
'data' => City::model()->getCitie`enter code here`s(),
'options' => array('width' => '30%','allowClear'=>true),
'htmlOptions'=>array(
'options'=>array(''=>array('value'=>null,'selected'=>null, 'name'=>'field'),),
'multiple'=>'multiple',
)
));
</pre>
I tried to specify 'name' field as single field and as an array but I have the same problem: only last value is sended.
You should simply use an array :
Instead of
'name' => 'Form[field]',
You should try :
'name' => 'Form[field][]',

Categories