kartik-v/yii2-field-range — pull request #5 by andrewblake1 (2014-07-11 07:15:04+00:00)
Title: FieldRange for GridView filters

Altered FieldRange.php to assume vertical stacking of inputs when separator is empty. This meant adding from and to container and input classes in a couple of places to make selecting the inputs for styling easier and more generic - was initially difficult to identify and style some of the top and bottom borders of the from and to widgets in css (not sure if they were deliberately missing or accidentally missing).

Have tested and working for my use for date, datetime, money, ordinary text input, and spin. Note that have only styled spins vertical buttons, and the money widget won't show placeholders if allowZero is true until the plugin author merges pull request 110 to allow nulls - this currently causes 0.00 ti show instead of the greyed placeholder text "From" and "To".

In order to use in GridView need to add the new attributes into the search model, search model rules e.g.

class AccountSearch extends Account
{
public $from_annual_charge;
public $to_annual_charge;
public $from_balance;
public $to_balance;
public $from_booking_charge;
public $to_booking_charge;
public $from_rate;
public $to_rate;
public $from_seat_charge;
public $to_seat_charge;
public $from_sms_charge;
public $to_sms_charge;
public $from_summary_charge;
public $to_summary_charge;
public $from_ticket_charge;
public $to_ticket_charge;

public function rules()
{
return [
[['address_id', 'user_id'], 'integer'],
[['annual_charge', 'from_annual_charge', 'to_annual_charge', 'balance', 'from_balance', 'to_balance', 'booking_charge', 'from_booking_charge', 'to_booking_charge', 'rate', 'from_rate', 'to_rate', 'seat_charge', 'from_seat_charge', 'to_seat_charge', 'sms_charge', 'from_sms_charge', 'to_sms_charge', 'summary_charge', 'from_summary_charge', 'to_summary_charge', 'ticket_charge', 'from_ticket_charge', 'to_ticket_charge'], 'number'],
[['phone_work'], 'safe'],
];
}

public function scenarios()
{
// bypass scenarios() implementation in the parent class
return \yii\base\Model::scenarios();
}

public function search($params)
{
$query = Account::find();

$dataProvider = new ActiveDataProvider([
'query' => $query,
]);

if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}

$query->andFilterWhere(['address_id' => $this->address_id]);
if(!is_null($this->from_annual_charge) && $this->from_annual_charge != '') $query->andWhere('`annual_charge` >= :from_annual_charge', [':from_annual_charge' => $this->from_annual_charge]);
if(!is_null($this->to_annual_charge) && $this->to_annual_charge != '') $query->andWhere('`annual_charge` <= :to_annual_charge', [':to_annual_charge' => $this->to_annual_charge]);
if(!is_null($this->from_balance) && $this->from_balance != '') $query->andWhere('`balance` >= :from_balance', [':from_balance' => $this->from_balance]);
if(!is_null($this->to_balance) && $this->to_balance != '') $query->andWhere('`balance` <= :to_balance', [':to_balance' => $this->to_balance]);
if(!is_null($this->from_booking_charge) && $this->from_booking_charge != '') $query->andWhere('`booking_charge` >= :from_booking_charge', [':from_booking_charge' => $this->from_booking_charge]);
if(!is_null($this->to_booking_charge) && $this->to_booking_charge != '') $query->andWhere('`booking_charge` <= :to_booking_charge', [':to_booking_charge' => $this->to_booking_charge]);
$query->andFilterWhere(['like', 'phone_work', $this->phone_work]);
if(!is_null($this->from_rate) && $this->from_rate != '') $query->andWhere('`rate` >= :from_rate', [':from_rate' => $this->from_rate]);
if(!is_null($this->to_rate) && $this->to_rate != '') $query->andWhere('`rate` <= :to_rate', [':to_rate' => $this->to_rate]);
if(!is_null($this->from_seat_charge) && $this->from_seat_charge != '') $query->andWhere('`seat_charge` >= :from_seat_charge', [':from_seat_charge' => $this->from_seat_charge]);
if(!is_null($this->to_seat_charge) && $this->to_seat_charge != '') $query->andWhere('`seat_charge` <= :to_seat_charge', [':to_seat_charge' => $this->to_seat_charge]);
if(!is_null($this->from_sms_charge) && $this->from_sms_charge != '') $query->andWhere('`sms_charge` >= :from_sms_charge', [':from_sms_charge' => $this->from_sms_charge]);
if(!is_null($this->to_sms_charge) && $this->to_sms_charge != '') $query->andWhere('`sms_charge` <= :to_sms_charge', [':to_sms_charge' => $this->to_sms_charge]);
if(!is_null($this->from_summary_charge) && $this->from_summary_charge != '') $query->andWhere('`summary_charge` >= :from_summary_charge', [':from_summary_charge' => $this->from_summary_charge]);
if(!is_null($this->to_summary_charge) && $this->to_summary_charge != '') $query->andWhere('`summary_charge` <= :to_summary_charge', [':to_summary_charge' => $this->to_summary_charge]);
if(!is_null($this->from_ticket_charge) && $this->from_ticket_charge != '') $query->andWhere('`ticket_charge` >= :from_ticket_charge', [':from_ticket_charge' => $this->from_ticket_charge]);
if(!is_null($this->to_ticket_charge) && $this->to_ticket_charge != '') $query->andWhere('`ticket_charge` <= :to_ticket_charge', [':to_ticket_charge' => $this->to_ticket_charge]);
$query->andFilterWhere(['user_id' => $this->user_id]);

return $dataProvider;
}
}