Skip to content

Extension for Yii2 Framework to work with jQuery QueryBuilder

Notifications You must be signed in to change notification settings

andmemasin/yii2-querybuilder

 
 

Repository files navigation

jQuery QueryBuilder Extension for Yii 2

This is the jQuery QueryBuilder extension for Yii 2. It encapsulates QueryBuilder component in terms of Yii widgets, and thus makes using QueryBuilder component in Yii applications extremely easy

Yii2 Latest Stable Version Scrutinizer Code Quality Code Coverage Build Status Code Climate Total Downloads

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist leandrogehlen/yii2-querybuilder "*"

or add

"leandrogehlen/yii2-querybuilder": "*"

to the require section of your composer.json file.

jquery-querybuilder dependency is installed via npm. You need to make sure your project npm installs "jQuery-QueryBuilder": "^3.0.0".

The package will ue @npm alias to look for npm dependencies.

How to use

The extension depends the bootstrap css then is necessary adds it in AppAsset:

class AppAsset extends AssetBundle {

    ...

    public $depends = [
        ...
        'yii\bootstrap\BootstrapAsset', // or 'yii\bootstrap4\BootstrapAsset'
    ];

}

View:

use leandrogehlen\querybuilder\QueryBuilderForm;

<?php QueryBuilderForm::begin([
    'rules' => $rules,
    'builder' => [
        'id' => 'query-builder',
        'pluginOptions' => [
            'filters' => [
                ['id' => 'id', 'label' => 'Id', 'type' => 'integer'],
                ['id' => 'name', 'label' => 'Name', 'type' => 'string'],
                ['id' => 'lastName', 'label' => 'Last Name', 'type' => 'string']
            ]
        ]
    ]
 ])?>

    <?= Html::submitButton('Apply'); ?>
    <?= Html::resetButton('Reset'); ?>

<?php QueryBuilderForm::end() ?>

Controller:

use leandrogehlen\querybuilder\Translator;

public function actionIndex()
{
      $query = Customer::find();
      $rules = Json::decode(Yii::$app->request->get('rules'));
      if ($rules) {
          $translator = new Translator($rules,['currentParams'=>$query->params]);
          $query
            ->andWhere($translator->where())
            ->addParams($translator->params());
      }
      
      $dataProvider = new ActiveDataProvider([
          'query' => $query,
      ]);
    
      return $this->render('index', [
          'dataProvider' => $dataProvider,
          'rules' => $rules
      ]);

}

About

Extension for Yii2 Framework to work with jQuery QueryBuilder

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%