I'm Having a bit of a issue with Depdrop from Kartik-v and Dynamic Form from Wbraganca. The problem is when I load the update form, the data isn't loaded into the second dynamic field. Until i add a new row then the data is shown.
.
It doesn't give any error. All I want is for the data to be shown on the second row and so on.
Code in the form
<?php $catList=ArrayHelper::map(Hs::find()->all(), 'hscode', 'hscode' );?>
<?= $form->field($modelsItems, "[{$i}]hscode")->dropDownList($catList, ['id'=>'cat-id','prompt'=>'Select...']);?>
</div>
<div class="col-sm-6" style="width: 200px">
<?= $form->field($modelsItems, "[{$i}]hsproduct")->widget(Depdrop::classname(), [
'options'=>['id'=>'product-id'],
'pluginOptions'=>[
'placeholder' => FALSE,
'initialize' => true,
'depends'=>['cat-id'], // the id for cat attribute
'url'=> Url::to(['hs/subcat']),
]
]);
?>
</div>
<div class="col-sm-6" style="width: 170px">
<?php $companylist=ArrayHelper::map(Company::find()->all(), 'company', 'company' );?>
<?= $form->field($modelsItems, "[{$i}]company")->dropDownList($companylist, ['id'=>'company-id','prompt'=>'Select...']);?>
</div>
<div class="col-sm-6" style="width: 170px">
<?= $form->field($modelsItems, "[{$i}]variety")->widget(DepDrop::classname(), [
'options'=>['id'=>'cp-varitey'],
'pluginOptions'=>[
'placeholder' => FALSE,
'initialize' => true,
'depends'=>['company-id'], // the id for cat attribute
'url'=> Url::to(['company/subcat2'])
]
])->label('Variety');
?>
</div>
For those who having this same problem i solve it by changing the id. The first ID was cat-id but when you create a new dynamic field the id then change to cat-0--id for some reason. The way i solve it was by changing the id to this
'id'=>"cat-".$i."--id" and should do this for the rest of the id. My product id now is 'id'=>"product-".$i."--id".
I hope this helps some one. Thanks
Related
I have a select2 field that is filled by the symfony form builder. The field is nullable/clearable but I would like to set it to default to null.
->add('rfaLabMapSecondaryRfa', ChoiceType::class, [
'choices' => $this->customOptionsRepository->getRFAs(),
'attr' => ['class' => 'select2_rfa'],
])
The twig HTML looks like this:
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label class="control-label fixed_select2_fields" style="display: block;" for="rfa_lab_map_rfaLabMapSecondaryRfa">Secondary RFA:</label>
<div id="secondary_rfa">
{{ form_widget(form.rfaLabMapSecondaryRfa) }}
</div>
</div>
</div>
</div>
I've tried messing with it using jQuery but can't seem to get it to default to null. I've searched other posts and they say to add an option. Not sure how I can do that with pre-built form collectives. Thanks for the help in advance
$('.select2_rfa').select2({
width: '100%',
placeholder: 'No secondary RFA',
allowClear: true
})
Managed to solve it by prepending to the select2 class in JQuery
$('.select2_rfa').prepend('<option selected=""></option>').select2({
width: '100%',
placeholder: 'No secondary RFA',
allowClear: true
})
The empty option selected allows the form to start null and still offer all possible choices.
I have the following codes:
<?= $form->field($model, 'parent_id')->dropDownList($categories, ['class' => 'select']) ?>
<?= $form->field($model, "[{$lang['id']}]anchors",['template' => $template])->textarea(['class' => 'form-control editor']);?>
The first line is the dropdown list with multiple choices. Among the dropdown choices there is an option($model->parent_id ==10) which I would like to make it in a way that, when a user selects that option the second line to be inactive or hidden.
I tried to solve it with jquery but it didn't work.
Please share with me your solutions on this issue
Thank you in advance
Your Form
<?= $form->field($model, 'parent_id')->dropDownList($categories, ['id' => 'myParentField', 'prompt' => '--- Select Parent ---']) ?>
<div id="showField" style="display:none">
<?= $form->field($model, "[{$lang['id']}]anchors",['template' => $template])->textarea(['class' => 'form-control editor']);?>
</div>
Register Js in Your Form like below
<?php
$this->registerJs(<<<JS
$(document).ready(function(){
$('#myParentField').on('change', function() {
if (($('#myParentField').val()) == '10') {
$("#showField").show();
} else {
$("#showField").hide();
}
});
});
JS
);
?>
Refer Working with Client Scripts (Yii2 Register Js)
when I click on the action of a controller and i disable pagination, the web page won't load...
the problem is when i click on the action controller which is some sort of:
public function actionMyview(){
return $this->render("//myview/index");
}
in the view code I'm making some sort of MVVM (i'm calling the ActiveDataProvider into it):
<?php
set_time_limit(0);
use yii\helpers\Html;
use yii\grid\GridView;
use frontend\models\myModel;
use yii\web\View;
use yii\data\ActiveDataProvider;
$dataProvider2 = new ActiveDataProvider([
'query' => myModel::find(),
'pagination' => false,
]);
$this->title = 'Listing 3 thousand records';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="ventas-model-index">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Create manually a new record', ['create'], ['class' => 'btn btn-success col-xs-4 col-md-4 col-sm-4 col-lg-4']) ?>
<div class="col-xs-3 col-md-3 col-sm-3 col-lg-3" >
<input type="text" class="form-control" id="search-criteria" placeholder="please write a text in order to search" width=""/>
</div>
<span class="col-xs-5 col-md-5 col-sm-5 col-lg-5"> </span>
<?php
$this->registerJs("
var rows = jQuery('.table tr');
$('#search-criteria').keyup(function() {
var val = jQuery.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
rows.show().filter(function() {
var text = jQuery(this).text().replace(/\s+/g, ' ').toLowerCase();
return !~text.indexOf(val);
}).hide();
});
",
View::POS_END,
'ventanamodal001');
?>
</p>
<?php echo GridView::widget([
'dataProvider' => $dataProvider2,
'columns' => [
[
'attribute' => 'date',
'format' => ['date', 'php:d/m/Y']
],
["label"=>'First Column',"attribute"=>"first","value"=>function($data0){
return myModel::aBolivares($data0->first);
}],
["label"=>'second Column',"attribute"=>'second',"value"=>function($data0){
return myModel::aBolivares($data0->second);
}],
["label"=>"totals","value"=>function($data0){
$totals=$data0->first+$data0->secod;
return myModel::aBolivares($totals);
},"format"=>"html","enableSorting"=>true,"attribute"=>"first"],
['label'=>'description',
'value'=>function ($data){
return empty($data->Observ)?" ":"<p class='glyphicon glyphicon-eye-open'></p>";
},
"format"=>"html","attribute"=>"Observ"],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
the code sample will iterate into a yii2 ActiveDataProvider object given to the yii\grid\GridView;... when I upload into a remote location it will fail: (I'm using HostGator shared hosting).
IT'S IMPORTANT TO SAY: IT WILL FAIL WHEN GIVEN MORE THAN 3 THOUSAND RECORDS == 3000 records directly from DB. If you use let's say 1500 records it will slow down but will work... only when injecting a high volume of records will fail...
I would like to render JQuery UI sortable list (I am using Yii2 JUI widget), to be populated with data from database table. I am passing data from database to the view and have it in a variable. The item HTML structure looks like this:
<div class="row row-item dist simple-border">
<div class="col-md-4">
<img alt="Bootstrap Image Preview" src="<IMAGE_PATH>" />
</div>
<div class="col-md-8">
<h4>
<ITEM DESCRIPTION>
</h4>
</div>
</div>
My question is: What is the best way to populate items inside the Widget, as it is impossible to execute php code within.
<?= yii\jui\Sortable::widget([
'items' => [
------<expected foreach php loop for each model>------
['content' =>
<This is where one HTML item goes>
],
------<end foreach>------
],
'options' => ['class' => 'connectedSortable',],
'clientOptions' => ['cursor' => 'move', 'connectWith' => '.connectedSortable']
]) ?>
My first idea was to create a function that returns item content as a string, but it doesn't seem like a good, efficient practice. Any ideas would be greatly appreciated. Regards.
EDIT: So far, I have created a function, which renders mentioned above items by concatenation. Still, I don't think this is the proper way to do it. I am still new to Yii and would be thankful for any tips. Regards.
I have found such solution for my problem, feel free to review and improve it.
Controller
public function actionIndex()
{
$searchModel = new Exchange();
$dataProvider = $searchModel->getOwnerItems(Yii::$app->request->queryParams);
$ownerItems = $this->getOwnerItems($dataProvider);
return $this->render('index', [
'ownerItems' => $ownerItems,
]);
}
protected function getOwnerItems($dataProvider){
$items=array();
foreach($dataProvider->models as $model){
$item=array('content' => '<div class="row row-item dist simple-border"> <div class="col-md-4"> <img alt="Bootstrap Image Preview" src="uploads/'.$model->image.'" /> </div> <div class="col-md-8"> <h4>'. $model->name .'</h4> </div> </div>');
array_push($items,$item);
}
return $items;
}
View
<?= yii\jui\Sortable::widget([
'items' => $ownerItems,
'options' => ['class' => 'connectedSortable',],
'clientOptions' => ['cursor' => 'move', 'connectWith' => '.connectedSortable']
]) ?>
I am aware of possible imperfection of this solution, but I have no other ideas at the moment. Regards
I using form type in symfony 2.8.x.
I need form attribute text in view.
form type:
$builder->add( 'category_id', ChoiceType::class,
array( 'label'=>'Category',
'constraints'=>array( new Regex(array("pattern"=>"'([^0-9]*)$'si",
"message"=>"Required field!"))
),
'choices'=>$categoriesRepo->getAllActive(),
'choices_as_values'=>true,
'choice_label' => 'getName',
'choice_value' => 'getId',
'attr'=>array( 'class'=>'form-control',
'help'=>'Help message.',
)));
In view:
<?php echo $view->render("XXXBundle:XXX:form_element.html.php", array('form'=>$templateForm['category_id']))?>
form_element.html.php
<div class="form-group">
<?php echo $view['form']->label($form, null, array('required'=>false, 'label_attr'=>array('class'=>'col-md-3 control-label'))) ?>
<div class="col-md-4">
<?php echo $view['form']->widget($form, array('attr'=>array('help'=>false))) ?>
<span class="help-block error"><?php echo $view['form']->errors($form) ?></span>
<span class="help-block"> HELP_MESSAGE </span>
</div>
</div>
I need "help" attribute to HELP_MESSAGE in view.
There is a solution to this problem?
Thank you!
You are the first Symfony user I've ever seen who renders PHP files instead of twig. Congratulations.
The real business part of your code you haven't posted, so it's hard to give a definitive answer, but somewhere at the bottom of your controller's action function there will be a call to $this->render('XXXBundle:XXX:template.html.php');
The 'Symfony way' is to pass the help message in that:
$help_message = 'My Help Message';
$this->render('XXXBundle:XXX:template.html.php', array('help_message' => $help_message));
In your template file:
<?php echo($view['help_message']);?>