PHP code example of kak / itemselect

1. Go to this page and download the library: Download kak/itemselect 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/ */

    

kak / itemselect example snippets


 
    use kak\widgets\itemselect\ItemSelect;
    $countries = Country::find()->where(['status' => Country::STATUS_ACTIVE ])->all();

<?=ItemSelect::widget([
    'itemAttributeId' => 'id',
    'name' => 'ItemSelect[]',  // input name
    'value' => [3,5,7],        // select items by itemAttributeId
    'items' => [
        ['id' => 1 , 'name' => 'Foo1', 'flag_url' => '' ],
        ['id' => 2 , 'name' => 'Foo2', 'flag_url' => '' ],
        ['id' => 3 , 'name' => 'Foo3', 'flag_url' => '' ],
        ['id' => 4,  'name' => 'Foo4', 'flag_url' => '' ],
        ['id' => 5 , 'name' => 'Foo5', 'flag_url' => '' ],
        ['id' => 6 , 'name' => 'Foo6', 'flag_url' => '' ],
    ],
    'templateItem' => '<div><img src="{%=o.flag_url%}">{%=o.name%}</div>',
     'events' => [
        ItemSelect::EVENT_SELECT_ITEM => new yii\web\JsExpression("function(item, direction){
            $(item.target).css('background', 'lawngreen');
            console.log($(item.target).text(), direction);
        }"),
        ItemSelect::EVENT_UNSELECT_ITEM => new yii\web\JsExpression("function(item, direction){
            $(item.target).css('background', '');
            console.log($(item.target).text(), direction);
        }"),
        ItemSelect::EVENT_MOVE_ITEM => new yii\web\JsExpression("function(item, direction){
            console.log($(item.target).text(), direction);
        }"),
    ],
   
]);

<?=ItemSelect::widget([
    'itemAttributeId' => 'id',
    'name' => 'ItemSelect[]',
    'value' => [3,5,7],        // select items by itemAttributeId
    'items' => [
        ['id' => 1 , 'name' => 'Foo1', 'flag_url' => '' ],
        ['id' => 2 , 'name' => 'Foo2', 'flag_url' => '' ],
        ['id' => 3 , 'name' => 'Foo3', 'flag_url' => '' ],
        ['id' => 4,  'name' => 'Foo4', 'flag_url' => '' ],
        ['id' => 5 , 'name' => 'Foo5', 'flag_url' => '' ],
        ['id' => 6 , 'name' => 'Foo6', 'flag_url' => '' ],
    ],
    'viewItem' => '/controller-name/_item-select'
]);

<?=ItemSelect::widget([
    'itemAttributeId' => 'id',
    'name' => 'ItemSelect[]',
    'value' => [ 1,2,3,4,5],        // select items by itemAttributeId
    'items' => [
        ['id' => 1 , 'name' => 'Foo1', 'flag_url' => '' ],
        ['id' => 2 , 'name' => 'Foo2', 'flag_url' => '' ],
        ['id' => 3 , 'name' => 'Foo3', 'flag_url' => '' ],
        ['id' => 4,  'name' => 'Foo4', 'flag_url' => '' ],
        ['id' => 5 , 'name' => 'Foo5', 'flag_url' => '' ],
        ['id' => 6 , 'name' => 'Foo6', 'flag_url' => '' ],
    ],
    'itemView' => function($item, $index, $select, $widget){
        return sprintf(
            '<div><img src="%s">%s</div>', 
            $item['flag_url'], 
            $item['name']
        );
    }
]);


    use app\models\Service;
    use yii\helpers\ArrayHelper;
    
    $servics = Service::find()->where(['status' => Service::STATUS_ACTIVE ])->all();