有没有办法过滤网格视图之外的搜索结果?我想这样做,因为它几乎是不可能的,我使自定义的外观与默认网格。
这是我用来搜索的方法:
public function actionSell()
{
$searchModel = new ProductsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
if(Yii::$app->request->isAjax):
echo json_encode($dataProvider);
return true;
endif;
return $this->render('sell', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider
]);
}
搜索方法:
public function search($params)
{
$query = Products::find();
$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;
}
$query->andFilterWhere([
'id' => $this->id,
'user_id' => $this->user_id,
'your_price' => $this->your_price,
'available_stock' => $this->available_stock,
'shipping_costs_carrier' => $this->shipping_costs_carrier,
'shipping_costs_type' => $this->shipping_costs_type,
'shipping_costs_cost' => $this->shipping_costs_cost,
]);
$query->andFilterWhere(['like', 'inci', $this->inci])
->andFilterWhere(['like', 'inn', $this->inn])
->andFilterWhere(['like', 'fe', $this->fe])
->andFilterWhere(['like', 'n_cas', $this->n_cas])
->andFilterWhere(['like', 'einecs', $this->einecs])
->andFilterWhere(['like', 'iupac', $this->iupac])
->andFilterWhere(['like', 'restriction', $this->restriction])
->andFilterWhere(['like', 'function', $this->function])
->andFilterWhere(['like', 'trade_name', $this->trade_name])
->andFilterWhere(['like', 'inci_name', $this->inci_name])
->andFilterWhere(['like', 'component_1', $this->component_1])
->andFilterWhere(['like', 'component_2', $this->component_2])
->andFilterWhere(['like', 'country_id', $this->country_id])
->andFilterWhere(['like', 'state_id', $this->state_id])
->andFilterWhere(['like', 'address', $this->address]);
return $dataProvider;
}
AJAX功能的JS代码:
$('#product-sell-search').on('submit', function(){
var form = $(this);
$.ajax({
url: form.attr('action'),
type: 'get',
dataType: 'json',
data: form.serialize(),
success: function(data) {
console.log(data);
}
});
return false;
});
视图中的表单:
<form action="/products/sell" method="get" class="form-inline" id=product-sell-search accept-charset="utf-8" role=form>
<div class="form-group">
<label for="product">Your product name</label>
<input type="text" class="form-control product-name" name="ProductsSearch[inci]" placeholder="Search products..." >
</div>
<button type="submit" class="btn btn-black">Search</button>
</form>
2条答案
按热度按时间c3frrgcw1#
getModels()
方法后您将收到它。如果您想使用json_decode
,请将以下代码添加到您的search
方法中:以及在
actionSell()
中:该代码执行以下操作:如果你用 AJAX 请求你的数据,当你调用
$dataProvider->getModels()
时,它会以数组的形式返回所有的模型,然后它们将被json编码iszxjhcz2#
你不需要使用 AJAX 调用任何东西,你必须使用javascript小部件(yiiGridView)。
在我的例子中,我必须过滤Gridview之外的数据。我添加了一个复选框来过滤已删除的数据。如果选中,已删除的数据(标志=1)将被列出,否则所有数据(标志=0)将被列出。工作示例如下图所示。
Custom Filter Image 1
Custom Filer Image 2
Step:1我将复选框放在了gridview小部件之外。
步骤:2
步骤:3在ModelSearch.php模型文件中添加了动态参数值(在我的例子中是ShippingChargeSearch.php)
Image for Step 3
希望这有帮助。如果你想的话,你可以尝试输入文本。编码快乐!