如何在Yii框架中创建新的TextInput并使其成为filterSelector 我在模型中做了这个:
<input name='searchfield' id='searchModel'>
'filterSelector' => '#searchModel',
和searchModel:
if ($this->company_name) {
$query->andFilterWhere(['like', 'company_name', $this->company_name]);;
}
但问题是gridview正在刷新而没有显示结果
答案 0 :(得分:1)
你把它标记为Yii,但从语法上看它可能是Yii2。我打算给你一些我工作的东西:
这将是你的控制者:
public function actionSomething()
{
$criteria = new CDbCriteria();
if(isset($_GET['query']))
{
$query = $_GET['query'];
$criteria->compare('attribute_of_model', $query, true, 'OR');
// you can add more attributes with which you want to compare query, that is why I added 'OR' if you remove 'OR' it defaults to 'AND'
}
$dataProvider=new CActiveDataProvider("YourModel", array('criteria'=>$criteria));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
这就是你的观点:
<form method="get">
<input type="search" placeholder="filter" name="query" value="<?=isset($_GET['query']) ? CHtml::encode($_GET['query']) : '' ; ?>" />
<input type="submit" value="search" />
</form>
这应该有效。如果您正在开发Yii2,那么您将不得不稍微改变它。