Yii2 GridView如何创建与GET参数链接到数组ModelSearch

时间:2019-01-07 15:56:47

标签: yii2

我想链接一些选项。

当我直接从GridView使用过滤器时,我得到了下一个URL /index?BookingSearch%5Bstatus%5D=1

当我使用链接时:

<?= Html::a('<i class="fa fa fa-arrow-right"></i> Confirm Details', ['index'], [
    'class' => 'btn bg-blue btn-flat',
    'data' => [
        'method' => 'get',
        'params' => [
            'status' => '1',
        ],
    ]
]); ?>

我得到了/index?status=1

这种方式对我不起作用。

如何通过链接正确发送GET请求以获取状态为1的所有帖子?

当我使用ActiveForm时,效果很好。

<?php $form = ActiveForm::begin([
    'action' => ['index'],
    'method' => 'get',
]);
?>
<?= $form->field($model, 'start')->textInput(['value' => 'hidden value']); ?>
<?= $form->field($model, 'end')->textInput(['value' => 'hidden value']) ?>

<div class="form-group">
    <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
    <?= Html::resetButton('Drop', ['class' => 'btn btn-default']) ?>
</div>

<?php ActiveForm::end(); ?>

我的BookingSearch以及所有工具都可以正常工作,但是我不会做出一些调整来快速访问数据。

namespace backend\models;

use common\models\Booking;
use Yii;
use yii\data\ActiveDataProvider;

/**
 * This is the model class for table "booking".
 *
 * @property int $id
 * @property int $phone
 * @property string $created
 * @property string $total_price
 * @property string $safe_deposit
 * @property int $safe_deposit_back
 * @property string $check_in
 * @property string $check_in_time
 * @property string $check_out
 * @property string $check_out_time
 * @property int $adult
 * @property int $child
 * @property string $animal
 * @property int $is_reviewed
 * @property string $is_prepayed
 * @property string $review
 * @property int $worker_id
 * @property int $object_id
 * @property int $client_id
 * @property int $status
 * @property string $booking_link
 * @property string $source
 */
class BookingSearch extends Booking
{

    const START_DATE = 'today';`enter code here`
    const END_DATE = '30';

    /**
     * @var string
     */
    public $start;


    /**
     * @var string
     */
    public $end;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'booking';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['object_id', 'status'], 'integer'],
            [['object_id', 'status', 'creation_date', 'first_name', 'second_name', 'start', 'end'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'phone' => Yii::t('app', 'Phone'),
            'created' => Yii::t('app', 'Created'),
            'total_price' => Yii::t('app', 'Total Price'),
            'safe_deposit' => Yii::t('app', 'Safe Deposit'),
            'safe_deposit_back' => Yii::t('app', 'Safe Deposit Back'),
            'check_in' => Yii::t('app', 'Check In'),
            'check_in_time' => Yii::t('app', 'Check In Time'),
            'check_out' => Yii::t('app', 'Check Out'),
            'check_out_time' => Yii::t('app', 'Check Out Time'),
            'adult' => Yii::t('app', 'Adult'),
            'child' => Yii::t('app', 'Child'),
            'animal' => Yii::t('app', 'Animal'),
            'is_reviewed' => Yii::t('app', 'Is Reviewed'),
            'is_prepayed' => Yii::t('app', 'Is Prepayed'),
            'review' => Yii::t('app', 'Review'),
            'worker_id' => Yii::t('app', 'Worker ID'),
            'object_id' => Yii::t('app', 'Object ID'),
            'client_id' => Yii::t('app', 'Client ID'),
            'status' => Yii::t('app', 'Status'),
            'booking_link' => Yii::t('app', 'Booking Link'),
            'source' => Yii::t('app', 'Source'),
        ];
    }

    public function search($params)
    {


        $query = Booking::find()->joinWith('client');

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 100,
            ],
            'sort' => [
                'defaultOrder' => [
                    'check_in' => SORT_ASC,
                ],
            ],
        ]);

        $dataProvider->sort->attributes['client.phone'] = [
            'asc' => ['client.phone' => SORT_ASC],
            'desc' => ['client.phone' => SORT_DESC],
        ];

        // load data and check validations
        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere(['like', 'status', $this->status]);

        //check if dates set for correct view
        if ($this->start <> null && $this->end <> null) {
            $query->andWhere(['>=', 'check_in', $this->start])
                ->andWhere(['<=', 'check_in', $this->end]);
        }

        return $dataProvider;
    }
}

这个动作

public function actionIndex()
{
    $searchModel = new BookingSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->get());


    return $this->render('index', [
        'dataProvider' => $dataProvider,
        'searchModel' => $searchModel,
    ]);
}

1 个答案:

答案 0 :(得分:0)

您可以使用getInputName()为参数生成输入名称:

<?= Html::a('<i class="fa fa fa-arrow-right"></i> Confirm Details', ['index'], [
    'class' => 'btn bg-blue btn-flat',
    'data' => [
        'method' => 'get',
        'params' => [
            Html::getInputName(BookingSearch::instance(), 'status') => '1',
        ],
    ]
]); ?>

或硬编码名称,但这可能很难重构:

<?= Html::a('<i class="fa fa fa-arrow-right"></i> Confirm Details', ['index'], [
    'class' => 'btn bg-blue btn-flat',
    'data' => [
        'method' => 'get',
        'params' => [
            'BookingSearch[status]' => '1',
        ],
    ]
]); ?>