Hi there,
The database is a huge list of items which all have 4 properties (size, covertype, color, weight). The form is for filtering these items and after submitting the form the user is displayed all items which correspong the the choices he made in the form.
I have a simple ActiveForm with 4 dropdowns. One dropdown per property. It is mandatory to select a size and a covertype. Color and weight selections are optional.
At the moment the user makes his selections in the dropdowns and after hitting the submit button he is displayed all items in the database which have the properties he selected.
My goal is to dynamically change the label of the submit button after a selection was made in one of the dropdowns, to display how many items in the database are corresponding to the current selection.
For example:
-
When in none of the dropdowns a selection was made, then the button is simply disabled.
-
When the user has selected a size and a covertype the the ‘Submit’-button’s label is sth. like “Show 32 items”. The user can then press the submit button or narrow his search further.
-
When the user has also selected a color then the ‘Submit’-buttons label updates to “Show 12 items”.
-
When the user also selects something in the last dropdown (weight) the the label updates again “Show 3 items”.
-
In the case the user selected the properties in such a way that no item in the database can be found which fulfills his selections then the button should show “No items found” and be disabled.
I think that is a common way how searches on popular websites work. For example if you are looking for real estate, cars or hotels. You make some selections and you know how many results will be found before you submit your search.
I tried around with ajaxvalidation but when i enable ajaxvalidation my submit-button isnt doing anything any longer XD
Here is some of my code. Stripped down to the important parts.
SiteController.php
public function actionIndex()
{
$model = new ItemFilter();
if ($model->load(Yii::$app->request->post()))
{
return $this->actionShowitems($model);
}
return $this->render('index', ['model' => $model]);
}
index.php
...
...
<?php $form = ActiveForm::begin([
'id' => 'item-filter',
//'enableAjaxValidation' => true,
'enableClientValidation' => true,
],); ?>
<div class="row">
<div class="col-lg-8">
<?= $form->field($model, 'size')
->label(false)
->dropdownList($sizes, ['prompt'=>'Choose a size...']);
?>
</div>
</div>
<div class="row">
<div class="col-lg-8">
<?= $form->field($model, 'cover')
->label(false)
->dropdownList($coverTypes, ['prompt'=>'Choose a covertype...']);
?>
</div>
</div>
<div class="row">
<div class="col-lg-8">
<?= $form->field($model, 'color')
->label(false)
->dropdownList($colors, ['prompt'=>'Choose a color...']);
?>
</div>
</div>
<div class="row">
<div class="col-lg-8">
<?= $form->field($model, 'weight')
->label(false)
->dropdownList($weights, ['prompt'=>'Choose a weight...']);
?>
</div>
</div>
<div class="row">
<div class="col-lg-offset-2 col-lg-10">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
</div>
</div>
<?php ActiveForm::end() ?>
ItemFilter.php
class ItemFilter extends Model
{
public $size;
public $cover;
public $color;
public $weight;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
[['size', 'cover'], 'required'],
[['color', 'weight'], 'default'],
];
}
}
Any help would be really appreciated.