Yii Framework Forum: Search function with a dropdown of results - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Search function with a dropdown of results Rate Topic: -----

#1 User is offline   Kyle96 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 25-July 17

Posted 25 July 2017 - 07:51 AM

I currently have a basic search function using my PostSearch model which is working fine however I would like to expand on it.

main.php - Search Form

$searchModel = new PostSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$form = ActiveForm::begin(['action' => ['/post/search'],'method' => 'get']); ?>
<?= $form->field($searchModel, 'subject')->textInput(array('placeholder' => 'Title...' ))->label(false); ?>
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?php ActiveForm::end();

PostSearch.php

public function search($params)
{
$query = Post::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,
'user_id' => $this->user_id,
'status' => $this->status,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'subject', $this->subject])
->andFilterWhere(['like', 'body', $this->body]);

return $dataProvider;
}

I am trying to achieve a similar result to that of the Yii documentation site. A user will begin to search and a dropdown of corresponding results will appear that the user can select from.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users