PHP code example of kr0lik / yii2-list-filter

1. Go to this page and download the library: Download kr0lik/yii2-list-filter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kr0lik / yii2-list-filter example snippets



use yii\web\Controller;
use kr0lik\listFilter\Filter;
use app\models\YourModel;

class YourController extends Controller
{
    public function actionIndex()
    {
	$query = YourModel::find();
		
	$filter = new Filter();
		
	$filter->add('default')->setTitle('Parameter Default')->setScope('byParameter');
        $filter->add('collection', Filter::PARAMETER_COLLECTION)->setScope('byCollectionParameter');
        $filter->add('price', Filter::PARAMETER_RANGE)->setTitle('Range Parameter')->setScope('byPrice')->setUnit('$');
        $filter->add('hasPrice', Filter::PARAMETER_BOOLEAN)->setScope(function($query, $select) {
            return $select ? $query->hasPrice() : $query;
        })->addValue(true, 'Checked by default')->addSelect(true);
		
	$filter->getParameter('default')->addValue('id', 'checkbox', 'Url', 'title');
		
	$filter->getParameter('price')
	    ->setMinValue(0)
            ->setMaxValue(100)
	    ->setStep(1);
		
	$filter->getParameter('collection')
            ->add('collection1')
            ->setTitle('Collection 1')
	    ->addValue(true, 'Checked by default')->addSelect(true);
			
	$filter->getParameter('collection')
            ->add('collection2', Filter::PARAMETER_RANGE)
            ->setTitle('Collection 2');	
				
		
	$filter->getParameter('collection')->getParameter('collection2')
	    ->setMinValue(1)
            ->setMaxValue(12.5)
	    ->setStep(0.5);
		
	$dataProvider = new ActiveDataProvider([
            'query' => $query->byFilter($filter),
	    ...
	]);
		
	return $this->render('index', ['filter' => $filter, 'dataProvider' => $dataProvider]);
    }
}


use yii\db\ActiveQuery;
use kr0lik\listFilter\FilterQueryTrait;

class YourModelQuery extends ActiveQuery
{
    use FilterQueryTrait;
}


use yii\helpers\Html;
use yii\widgets\ListView;
use kr0lik\listFilter\Filter;