Yii2 - 过滤gridview外的搜索结果

时间:2015-07-06 15:59:40

标签: php yii yii2 cgridview

有没有办法在gridview外过滤搜索结果?我想这样做,因为我几乎不可能使用默认网格进行自定义查看。

这是我用于搜索的方法:

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 个答案:

答案 0 :(得分:0)

DataProvider 不是数据本身,它是一种数据包装,您将在调用getModels()方法后收到。如果您想使用json_decode将其添加到search方法中:

//your filters above
if(Yii::$app->request->isAjax) {
    $query->asArray();
}

return $dataProvider;

actionSell()

if(Yii::$app->request->isAjax) {
    echo json_encode($dataProvider->getModels());
    return true;
}

代码执行如下操作:如果您使用ajax请求数据,则在您调用$dataProvider->getModels()时它将所有模型作为数组返回,之后它们将被json编码

答案 1 :(得分:0)

您无需进行ajax调用。您必须使用javascript小部件(yiiGridView)。

就我而言,我不得不在Gridview之外过滤数据。我添加了一个复选框来过滤已删除的数据。如果选中,将列出已删除的数据(标志= 1),否则将列出所有数据(标志= 0)。下图是工作示例。

Custom Filter Image 1

Custom Filer Image 2

步骤:1 我将复选框置于gridview小部件之外。

<input type="checkbox" name="ShippingChargeSearch[del_flg]" value="0" id="shippingcharge-shipping_area">

步骤:2

$(function(){
        $('#shippingcharge-shipping_area').click(function(e) {
            var chkVal = $(this).prop('checked');
            if(chkVal) {
                $('input[name="ShippingChargeSearch[del_flg]"]').val(1);
            }else{
                $('input[name="ShippingChargeSearch[del_flg]"]').val(0);
            }

        $('#w1').yiiGridView({'filterUrl':'/ec-admin/shipping-charge/index','filterSelector':'input[name="ShippingChargeSearch[del_flg]"]'});
        });
    });

步骤:3 在ModelSearch.php模型文件中添加了用于查询的动态参数值(在我的情况下为ShippingChargeSearch.php)

 /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params,$querys=null)
    {
        $delFlg = isset($params['ShippingChargeSearch']['del_flg']) ? $params['ShippingChargeSearch']['del_flg'] : 0;
        $query = ShippingCharge::find()
                    ->alias('t')
                    ->where(['del_flg'=>$delFlg]);
        if(!empty($querys)){
           $query->andWhere($querys);
        }
        // add conditions that should always apply here
.....

Image for Step 3

希望这会有所帮助。您可以根据需要尝试输入文本。祝您编码愉快!