17

Yii2-Selectivity

The yii2-selectivity is a Yii 2 wrapper for Selectivity.js. A modular and light-weight selection library for jQuery and Zepto.js.

Installation

The preferred way to install this extension is through composer.

Either run:

composer require --prefer-dist wbraganca/yii2-selectivity "*"

Or add to the require section of your composer.json file:

"wbraganca/yii2-selectivity": "*"

Usage

Select a single city

yii2-selectivity

Source

<?php

/* @var $cities array */
/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model app\modules\yii2extensions\models\SelectivityForm */

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use wbraganca\selectivity\SelectivityWidget;
?>

<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'city')->label(false)->widget(SelectivityWidget::classname(), [
    'pluginOptions' => [
        'allowClear' => true,
        'items' => $cities,
        'placeholder' => 'No city selected'
    ]
]) ?>

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

Select multiple cities

yii2-selectivity

Source

<?php

/* @var $cities array */
/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model app\modules\yii2extensions\models\SelectivityForm */

use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\JsExpression;
use yii\bootstrap\ActiveForm;
use wbraganca\selectivity\SelectivityWidget;

// init values
$model->cities = [2,15];
$selectivityValues = [];

foreach ($model->cities as $id) {
    $selectivityValues[] = ['id' => $id, 'text' => $cities[$id]];
}
?>

<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'cities')->label(false)->widget(SelectivityWidget::classname(), [
    'pluginOptions' => [
        'value' => $model->cities,
        'items' => $cities,
        'multiple' => true,
        'placeholder' => 'Type to search a city',
        'initSelection' => new JsExpression('function(data, callback) {
            $("#' . Html::getInputId($model, 'cities') . '").selectivity("data", ' . Json::encode($selectivityValues) . ')
        }')
    ]
]) ?>

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

For more options, visit: https://arendjr.github.io/selectivity/#api

Save

Prakash S

Prakash S

I would like to introduce myself as a Software professional opting for the career in software industry. I'm Prakash S, a MCA graduate and trained as industry level practice for Software technology. Basically I am a PHP Developer but now days exploring more in HTML5, CSS, AngularJS and jQuery libraries.