Yii 2-过滤gridview之外的搜索结果

i5desfxk  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(149)

有没有办法过滤网格视图之外的搜索结果?我想这样做,因为它几乎是不可能的,我使自定义的外观与默认网格。
这是我用来搜索的方法:

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>
c3frrgcw

c3frrgcw1#

  • DataProvider* 不是数据本身,它是数据的 Package 器,在调用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编码

iszxjhcz

iszxjhcz2#

你不需要使用 AJAX 调用任何东西,你必须使用javascript小部件(yiiGridView)。
在我的例子中,我必须过滤Gridview之外的数据。我添加了一个复选框来过滤已删除的数据。如果选中,已删除的数据(标志=1)将被列出,否则所有数据(标志=0)将被列出。工作示例如下图所示。
Custom Filter Image 1
Custom Filer Image 2

Step: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
希望这有帮助。如果你想的话,你可以尝试输入文本。编码快乐!

相关问题