Trait kartik\base\WidgetTrait

Implemented bykartik\base\Html5Input, kartik\base\InputWidget, kartik\base\Widget, kartik\select2\Select2, kartik\tree\TreeView, kartik\tree\TreeViewInput
Available since version1.6.0

WidgetTrait manages all methods used by Krajee widgets and input widgets.

Public Methods

Hide inherited methods

MethodDescriptionDefined By
registerWidgetJs() Registers a JS code block for the widget. kartik\base\WidgetTrait

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
addAsset() Adds an asset to the view. kartik\base\WidgetTrait
getPluginScript() Returns the plugin registration script. kartik\base\WidgetTrait
hashPluginOptions() Generates a hashed variable to store the pluginOptions. The following special data attributes will also be setup for the input widget, that can be accessed through javascript : kartik\base\WidgetTrait
initDestroyJs() Generates the pluginDestroyJs script if it is not set. kartik\base\WidgetTrait
registerPlugin() Registers a specific plugin and the related events kartik\base\WidgetTrait
registerPluginOptions() Registers plugin options by storing within a uniquely generated javascript variable. kartik\base\WidgetTrait
setDataVar() Sets a HTML5 data variable. kartik\base\WidgetTrait

Method Details

addAsset() protected method

Adds an asset to the view.

protected void addAsset ( $view, $file, $type, $class )
$view \yii\web\View

The View object

$file string

The asset file name

$type string

The asset file type (css or js)

$class string

The class name of the AssetBundle

getPluginScript() protected method

Returns the plugin registration script.

protected string getPluginScript ( $name, $element null, $callback null, $callbackCon null )
$name string

The name of the plugin

$element string

The plugin target element

$callback string

The javascript callback function to be called after plugin loads

$callbackCon string

The javascript callback function to be passed to the plugin constructor

return string

The generated plugin script

hashPluginOptions() protected method

Generates a hashed variable to store the pluginOptions. The following special data attributes will also be setup for the input widget, that can be accessed through javascript :

  • 'data-krajee-{name}' will store the hashed variable storing the plugin options. The {name} token will be replaced with the plugin name (e.g. select2, ``typeahead etc.). This fixes issue #6.
protected void hashPluginOptions ( $name )
$name string

The name of the plugin

initDestroyJs() protected method

Generates the pluginDestroyJs script if it is not set.

protected void initDestroyJs ( )
registerPlugin() protected method

Registers a specific plugin and the related events

protected void registerPlugin ( $name, $element null, $callback null, $callbackCon null )
$name string

The name of the plugin

$element string

The plugin target element

$callback string

The javascript callback function to be called after plugin loads

$callbackCon string

The javascript callback function to be passed to the plugin constructor

registerPluginOptions() protected method

Registers plugin options by storing within a uniquely generated javascript variable.

protected void registerPluginOptions ( $name )
$name string

The plugin name

registerWidgetJs() public method

Registers a JS code block for the widget.

public void registerWidgetJs ( $js, $pos = \yii\web\View::POS_READY, $key null )
$js string

The JS code block to be registered

$pos integer

The position at which the JS script tag should be inserted in a page. The possible values are:

  • \yii\web\View::POS_HEAD: in the head section
  • \yii\web\View::POS_BEGIN: at the beginning of the body section
  • \yii\web\View::POS_END: at the end of the body section
  • \yii\web\View::POS_LOAD: enclosed within jQuery(window).load(). Note that by using this position, the method will automatically register the jQuery js file.
  • \yii\web\View::POS_READY: enclosed within jQuery(document).ready(). This is the default value. Note that by using this position, the method will automatically register the jQuery js file.
$key string

The key that identifies the JS code block. If null, it will use $js as the key. If two JS code blocks are registered with the same key, the latter will overwrite the former.

setDataVar() protected method

Sets a HTML5 data variable.

protected void setDataVar ( $name )
$name string

The plugin name