GithubHelp home page GithubHelp logo

heartshare / yii2-widget-checkbox-multiple Goto Github PK

View Code? Open in Web Editor NEW

This project forked from diiimonn/yii2-widget-checkbox-multiple

0.0 2.0 0.0 70 KB

Input widget to render multiple select box.

PHP 33.45% CSS 12.09% JavaScript 54.46%

yii2-widget-checkbox-multiple's Introduction

yii2-widget-checkbox-multiple

Input widget to render multiple select box. Using jQuery ajax.

Installation

To install with composer:

$ php composer.phar require diiimonn/yii2-widget-checkbox-multiple "dev-master"

or add

"diiimonn/yii2-widget-checkbox-multiple": "dev-master"

to the require section of your composer.json file.

Usage

view:

...
use diiimonn\widgets\CheckboxMultiple;
use yii\helpers\Url;
...

<?= $form->field($model, 'books')->widget(CheckboxMultiple::className(), [
    'dataAttribute' => 'name',
    'scriptOptions' => [
        'ajax' => [
            'url' => Url::toRoute(['books']),
        ],
    ],
    'placeholder' => Yii::t('app', 'Select ...'),
]) ?>

or not use ActiveForm

...
use diiimonn\widgets\CheckboxMultiple;
use yii\helpers\Url;
...

<?= CheckboxMultiple::widget([
    'model' => $model,
    'attribute' => 'books',
    'dataAttribute' => 'name',
    'scriptOptions' => [
        'ajax' => [
            'url' => Url::toRoute(['books']),
        ],
    ],
    'placeholder' => Yii::t('app', 'Select ...'),
]) ?>

Settings

  • data : array the select option data items. The array keys are option values, and the array values are the corresponding option labels. If not set this option the 'attribute' will be relation name.
  • dataAttribute : string attribute name in relation models if not set 'data'.
  • scriptOptions : array options for customize script settings.
  • placeholder : string
  • options : array options for widget tag.
  • spinnerOptions : array options for yii2-widget-spinner-canvas.

scriptOptions

  • templateItem : string, html
  • templateCheckbox : string, html
  • templateResultItem : string, html
  • templateInput : string, html
  • templateResultError : string, html
  • templateResultWarning : string, html
  • templatePlaceholder : string, html
  • warningMessage : string
  • errorMessage : string
  • defaultCheckbox :: boolean If 'true' and not selected items will be selected checkbox with empty value. Default 'true'.
  • limit : integer Max count selected items.
  • slimScroll : array
  • wait : integer Time out in millisecond before ajax request.

Customize scriptOptions example

...
'scriptOptions' => [
    'defaultCheckbox' => false,
    'limit' => 10,
    'templateItem' => Html::tag('li', Html::tag('span', '{text}') .
            Html::tag('span', Html::tag('span', '', [
                'class' => 'glyphicon glyphicon-remove',
            ]), [
                'class' => 'checkbox-multiple-remove-item',
            ]), [
                'class' => 'checkbox-multiple-item',
            ]),
    'templateInput' => Html::textInput('checkbox-multiple-input', '', [
            'class' => 'form-control input-sm',
        ]),
    'slimScroll' => [
        'color' => '#333',
        'railOpacity' => 0.5,
        'railColor' => '#666666',
    ],
],

controller:

...
use yii\db\Query;
...

public function actionBooks()
{
    Yii::$app->response->format = 'json';

    $json = new \stdClass();

    $query = new Query();
    $query->select([
        'id' => 'id',
        'text' => 'name'
    ]);
    $query->from(BookModel::tableName());

    if ($search = Yii::$app->request->post('search', '')) {
        $query->where(['like', 'username', $search]);
    }

    $query->orderBy([
        'name' => SORT_ASC
    ]);

    if ($itemsId = Yii::$app->request->post('itemsId', [])) {
        $query->andWhere(['not in', 'id', $itemsId]);
    }

    $query->limit(20);

    $command = $query->createCommand();
    $data = $command->queryAll();
    $json->results = array_values($data);

    return $json;
}

yii2-widget-checkbox-multiple's People

Contributors

diiimonn avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.