Class yii\jui\Sortable

Inheritanceyii\jui\Sortable » yii\jui\Widget » yii\base\Widget
Available since extension's version2.0
Source Code https://github.com/yiisoft/yii2-jui/blob/master/Sortable.php

Sortable renders a sortable jQuery UI widget.

For example:

echo Sortable::widget([
    'items' => [
        'Item 1',
        ['content' => 'Item2'],
        [
            'content' => 'Item3',
            'options' => ['tag' => 'li'],
        ],
    ],
    'options' => ['tag' => 'ul'],
    'itemOptions' => ['tag' => 'li'],
    'clientOptions' => ['cursor' => 'move'],
]);

See also http://api.jqueryui.com/sortable/.

Public Properties

Hide inherited properties

Property Type Description Defined By
$clientEventMap array Event names mapped to what should be specified in .on(). yii\jui\Sortable
$clientEvents array The event handlers for the underlying jQuery UI widget. yii\jui\Widget
$clientOptions array The options for the underlying jQuery UI widget. yii\jui\Widget
$itemOptions array List of HTML attributes for the item container tags. yii\jui\Sortable
$items array List of sortable items. yii\jui\Sortable
$options array The HTML attributes for the widget container tag. yii\jui\Sortable

Protected Properties

Hide inherited properties

Property Type Description Defined By

Public Methods

Hide inherited methods

Method Description Defined By
init() Initializes the widget. yii\jui\Widget
renderItems() Renders sortable items as specified on $items. yii\jui\Sortable
run() Renders the widget. yii\jui\Sortable

Protected Methods

Hide inherited methods

Method Description Defined By
registerClientEvents() Registers a specific jQuery UI widget events yii\jui\Widget
registerClientOptions() Registers a specific jQuery UI widget options yii\jui\Widget
registerWidget() Registers a specific jQuery UI widget asset bundle, initializes it with client options and registers related events yii\jui\Widget

Property Details

Hide inherited properties

$clientEventMap protected property

Event names mapped to what should be specified in .on(). If empty, it is assumed that event passed to clientEvents is prefixed with widget name.

protected array $clientEventMap = [
    
'activate' => 'sortactivate',
    
'beforeStop' => 'sortbeforestop',
    
'change' => 'sortchange',
    
'create' => 'sortcreate',
    
'deactivate' => 'sortdeactivate',
    
'out' => 'sortout',
    
'over' => 'sortover',
    
'receive' => 'sortreceive',
    
'remove' => 'sortremove',
    
'sort' => 'sort',
    
'start' => 'sortstart',
    
'stop' => 'sortstop',
    
'update' => 'sortupdate',
]
$itemOptions public property

List of HTML attributes for the item container tags. This will be overwritten by the "options" set in individual $items. The following special options are recognized:

  • tag: string, defaults to "li", the tag name of the item container tags.

See also \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.

public array $itemOptions = []
$items public property

List of sortable items. Each item can be a string representing the item content or an array of the following structure:

[
    'content' => 'item content',
    // the HTML attributes of the item container tag. This will overwrite "itemOptions".
    'options' => [],
]
public array $items = []
$options public property

The HTML attributes for the widget container tag. The following special options are recognized:

  • tag: string, defaults to "ul", the tag name of the container tag of this widget.

See also \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.

public array $options = []

Method Details

Hide inherited methods

init() public method

Defined in: yii\jui\Widget::init()

Initializes the widget.

If you override this method, make sure you call the parent implementation first.

public void init ( )

                public function init()
{
    parent::init();
    if (!isset($this->options['id'])) {
        $this->options['id'] = $this->getId();
    }
}

            
registerClientEvents() protected method

Defined in: yii\jui\Widget::registerClientEvents()

Registers a specific jQuery UI widget events

protected void registerClientEvents ( $name, $id )
$name string

The name of the jQuery UI widget

$id string

The ID of the widget

                protected function registerClientEvents($name, $id)
{
    if (!empty($this->clientEvents)) {
        $js = [];
        foreach ($this->clientEvents as $event => $handler) {
            if (isset($this->clientEventMap[$event])) {
                $eventName = $this->clientEventMap[$event];
            } else {
                $eventName = strtolower($name . $event);
            }
            $js[] = "jQuery('#$id').on('$eventName', $handler);";
        }
        $this->getView()->registerJs(implode("\n", $js));
    }
}

            
registerClientOptions() protected method

Defined in: yii\jui\Widget::registerClientOptions()

Registers a specific jQuery UI widget options

protected void registerClientOptions ( $name, $id )
$name string

The name of the jQuery UI widget

$id string

The ID of the widget

                protected function registerClientOptions($name, $id)
{
    if ($this->clientOptions !== false) {
        $options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions);
        $js = "jQuery('#$id').$name($options);";
        $this->getView()->registerJs($js);
    }
}

            
registerWidget() protected method

Defined in: yii\jui\Widget::registerWidget()

Registers a specific jQuery UI widget asset bundle, initializes it with client options and registers related events

protected void registerWidget ( $name, $id null )
$name string

The name of the jQuery UI widget

$id string

The ID of the widget. If null, it will use the id value of $options.

                protected function registerWidget($name, $id = null)
{
    if ($id === null) {
        $id = $this->options['id'];
    }
    JuiAsset::register($this->getView());
    $this->registerClientEvents($name, $id);
    $this->registerClientOptions($name, $id);
}

            
renderItems() public method

Renders sortable items as specified on $items.

public string renderItems ( )
return string

The rendering result.

                public function renderItems()
{
    $items = [];
    foreach ($this->items as $item) {
        $options = $this->itemOptions;
        $tag = ArrayHelper::remove($options, 'tag', 'li');
        if (is_array($item)) {
            if (!isset($item['content'])) {
                throw new InvalidConfigException("The 'content' option is required.");
            }
            $options = array_merge($options, ArrayHelper::getValue($item, 'options', []));
            $tag = ArrayHelper::remove($options, 'tag', $tag);
            $items[] = Html::tag($tag, $item['content'], $options);
        } else {
            $items[] = Html::tag($tag, $item, $options);
        }
    }
    return implode("\n", $items);
}

            
run() public method

Renders the widget.

public void run ( )

                public function run()
{
    $options = $this->options;
    $tag = ArrayHelper::remove($options, 'tag', 'ul');
    echo Html::beginTag($tag, $options) . "\n";
    echo $this->renderItems() . "\n";
    echo Html::endTag($tag) . "\n";
    $this->registerWidget('sortable');
}