Skip to content

Commit

Permalink
add cascade options
Browse files Browse the repository at this point in the history
  • Loading branch information
PutraSudaryanto committed Apr 30, 2019
1 parent 14379e6 commit 6f2ac27
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion Selectize.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,60 @@

namespace ommu\selectize;

use yii\helpers\Url;
use yii\web\JsExpression;
use yii\helpers\Json;
use yii2mod\selectize\SelectizeAsset;
use yii\helpers\Inflector;

class Selectize extends \yii2mod\selectize\Selectize
{
/**
* {@inheritdoc}
*/
public $cascade = false;

/**
* @var string the parameter name
*/
public $queryParam = 'query';

/**
* Register client assets
*/
protected function registerAssets()
{
$view = $this->getView();
SelectizeAsset::register($view);
$js = '$("#' . $this->getInputId() . '").selectize(' . $this->getPluginOptions() . ');';
if($this->cascade) {
$inputIdVar = Inflector::underscore(Inflector::id2camel($this->getInputId()));
$jsVar = "var $inputIdVar, f_$inputIdVar;";
$js = "f_$inputIdVar = $('#".$this->getInputId()."').selectize(".$this->getPluginOptions().");\n$inputIdVar = f_".$inputIdVar."[0].selectize;";
$view->registerJs($jsVar, $view::POS_HEAD);
} else
$js = '$("#' . $this->getInputId() . '").selectize(' . $this->getPluginOptions() . ');';
$view->registerJs($js, $view::POS_END);
}

/**
* Get plugin options in the json format
*
* @return string
*/
public function getPluginOptions()
{
if ($this->url !== null) {
$url = Url::to($this->url);
$queryParam = $this->queryParam;
$this->pluginOptions['load'] = new JsExpression("
function (query, callback) {
if (!query.length) return callback();
$.getJSON('$url', { $queryParam: encodeURIComponent(query) }, function (data) { callback(data); })
.fail(function () { callback(); });
}
");
}

return Json::encode($this->pluginOptions);
}
}

0 comments on commit 6f2ac27

Please sign in to comment.