简体   繁体   English

如何在视图中启用查询按钮?

[英]How do I enable my Query button in my view?

I add another table field in my view , but the search button disappeared , How can I retrieve this form button ? 我在视图中添加了另一个表格字段,但是搜索按钮消失了,如何检索此表单按钮?

SaleItems = And a table mysql database. SaleItems =和一个表mysql数据库。

<?php

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'formatter' => ['class' => 'yii\i18n\Formatter','nullDisplay' => ''],
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            **[
            'attribute' => 'Data',
                'content' => function(SaleItems $model, $key, $index, $column) {
                    return date('d/m/Y', strtotime($model->sale->date));
                    },
                'header' => 'DATA'
            ],**
    ]); ?>
</div>

在此输入图像描述

The search field disappears because in not setted properly in searchModel .. 搜索字段消失,因为在searchModel中未正确设置in。

for this you must extend the related modelSeacrh adding the relation with the related model and the filter for the field you need .. 为此,您必须扩展相关的modelSeacrh,并添加与相关模型的关系以及所需字段的过滤器。

for this you must set in the base mode, the relation 为此,您必须在基本模式下设置关系

public function getSale()
{
   return $this->hasOne(Sale::className(), ['id' => 'sale_id']);
}

/* Getter for sale data */
 public function getSaleData() {
  return $this->sale->data;
}

then setting up the search model declaring 然后设置搜索模型声明

/* your calculated attribute */
public $date;

/* setup rules */
public function rules() {
   return [
     /* your other rules */
    [['data'], 'safe']
  ];
}

and extending 并扩展

 public function search($params) {

adding proper sort, for data 为数据添加适当的排序

$dataProvider->setSort([
    'attributes' => [
         .....
        'data' => [
            'asc' => ['tbl_sale.data' => SORT_ASC],
            'desc' => ['tbl_sale.data' => SORT_DESC],
            'label' => 'Data'
        ]
    ]
]);

proper relation 适当的关系

 if (!($this->load($params) && $this->validate())) {
    /**
     * The following line will allow eager loading with country data 
     * to enable sorting by country on initial loading of the grid.
     */ 
    $query->joinWith(['sale']);
    return $dataProvider;
}

and proper filter condition 适当的过滤条件

// filter by sale data
$query->joinWith(['sale' => function ($q) {
    $q->where('sale.data  = ' . $this->data . ' ');
}]);

Once you do al this the searchModel contain the information for filter and the search field is showed in gridview 完成此操作后,searchModel包含过滤器的信息,并且搜索字段显示在gridview中

What in this answer is just a list of suggestion you can find detailed tutorial in this doc (see the scenario 2 and adapt to your need) http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/ 您可以在此文档中找到详细的教程(请参阅方案2并适应您的需要),此答案只是建议列表。http://www.yiiframework.com/wiki/621/filter-sort-by-calculated -相关场合的GridView-YII-2-0 /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM