Static dropdown search in list

Hi,
I am trying to do search in list for message_type column. It is a selectbox. I could not search in list page

Model:
public function search($params) {
$query = Message::find();

    // add conditions that should always apply here

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $this->load($params);

    if (!$this->validate()) {
        // uncomment the following line if you do not want to return any records when validation fails
        // $query->where('0=1');
        return $dataProvider;
    }

    // grid filtering conditions
    $query->andFilterWhere([
        'id' => $this->id,
    ]);

    $query->andFilterWhere(['like', 'message', $this->message]);
    $query->andFilterWhere(['like', 'message_type', $this->message_type]);

    return $dataProvider;
}

Form:
$listData = array(‘1’ => ‘Text’, ‘2’ => ‘Image’, ‘3’ => ‘audio’, ‘4’ => ‘video’)

<?= $form->field($model, 'message_type')->dropDownList($listData, array('onchange' => 'getmessagetype(this.value)', 'id' => 'message_type', 'prompt' => 'Select Message Type')); ?>

I have stored integers in database.I have printed Message type correctly but i couldnt able to search in list.

Help…

What do you get in $params?

It gives the following output
Array
(
[r] => message
)
It gave message since my url is http://localhost/yii-application/backend/web/index.php?r=message

I dont know how to code for static options to filter in list view

My select box code is

Message Type Select Message Type Text Image audio video

Add whole search model with rules and search form in question. Difficult to conclude anything without it.

Model :

<?php namespace app\models; use yii\base\Model; use yii\data\ActiveDataProvider; use app\models\Message; /** * MessageSearch represents the model behind the search form of `app\models\Message`. */ class MessageSearch extends Message { /** * {@inheritdoc} */ public function rules() { return [ [['id', 'message_type', 'status'], 'integer'], [['message', 'message_type','sch_date'], 'safe'], ]; } /** * {@inheritdoc} */ public function scenarios() { // bypass scenarios() implementation in the parent class return Model::scenarios(); } /** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Message::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider([ 'query' => $query, ]); $this->load($params); // echo "
";        print_r($params);exit;
        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
        ]);

        $query->andFilterWhere(['like', 'message', $this->message]);
        $query->andFilterWhere(['like', 'message_type', $this->message_type]);

        return $dataProvider;
    }

}


Form:

<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\jui\DatePicker;

/* @var $this yii\web\View */
/* @var $model app\models\Message */
/* @var $form yii\widgets\ActiveForm */
//echo "
";print_r($model);
//echo "
";print_r($model->id);exit;
$listData = array('1' => 'Text', '2' => 'Image', '3' => 'audio', '4' => 'video');

$this->registerJsFile("js/script.js");
?>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>

<!--<? = $form->field($model, 'message_type')->textInput() ?>-->
<?= $form->field($model, 'message_type')->dropDownList($listData, array('onchange' => 'getmessagetype(this.value)', 'id' => 'message_type', 'prompt' => 'Select Message Type')); ?>
<?php
if (isset($model->id)) {
    if ($model->message_type == 1) {
        echo $form->field($model, 'message')->textInput(['maxlength' => true, 'id' => 'text_msg']);
    } else {
        echo $form->field($model, 'message')->fileInput(['maxlength' => true, 'id' => 'text_msg']);
        echo $form->field($model, 'hidden_path')->hiddenInput(['maxlength' => true, 'id' => 'hidden_path', 'name' => 'hidden_path', 'value' => $model->message])->label(false);
        echo Html::a('View', Yii::getAlias('@imageUploadUrl') . '/' . $model->message, ['class' => 'btn btn-success', 'target' => '_blank', 'id' => 'view_btn']);
    }
} else {
    echo $form->field($model, 'message')->textInput(['maxlength' => true, 'id' => 'text_msg']);
}
?>

<?= $form->field($model, 'sch_date')->widget(DatePicker::classname(), ['clientOptions' => ['dateFormat' => 'yy-mm-dd', 'autocomplete' => 'off']]) ?>


<div class="form-group">
    <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>

<?php ActiveForm::end(); ?>

It’s not your search form. This is the from to save data. :roll_eyes: If you were using GII to generate CRUD. You have file named _search.php

Sorry:

List Code:

<?php use yii\helpers\Html; use yii\grid\GridView; use yii\widgets\Pjax; use yii\helpers\Url; /* @var $this yii\web\View */ /* @var $searchModel app\models\MessageSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ //echo "
";print_r($dataProvider);exit;
$this->title = 'Messages';
$this->params['breadcrumbs'][] = $this->title;
?>
<h1><?= Html::encode($this->title) ?></h1>

<p>
    <?= Html::a('Create Message', ['create'], ['class' => 'btn btn-success']) ?>
</p>

<?php Pjax::begin(); ?>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<?php

// echo “

”;
// print_r($dataProvider);
// exit;
?>
<?=
GridView::widget([
‘dataProvider’ => $dataProvider,
‘filterModel’ => $searchModel,
‘columns’ => [
[‘class’ => ‘yii\grid\SerialColumn’],
‘id’,
// ‘message_type’,
[
‘label’ => ‘Message Type’,
‘value’ => function ($model) {
return $model->messagetype();
}
],
[
‘label’ => ‘Message’,
‘value’ => function ($model) {
return $model->messageview();
},
‘format’ => ‘raw’,
],
[
‘attribute’ => ‘sch_date’,
‘value’ => function ($model) {
return $model->schdateformat();
},
],
// ‘sch_date’,
// ‘status’,
[‘class’ => ‘yii\grid\ActionColumn’],
],
]);
?>
<?php Pjax::end(); ?>

This file is used to search data. You were not using it nor you added filter in gridview.

Anyways, Try this in GridView

'label' => 'Message Type',
'value' => function ($model) {
    return $model->messagetype();
},
'filter' => ['1' => 'Text', '2' => 'Image', '3' => 'audio', '4' => 'video'],

@Preethi_vu see updates