Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mhunesi committed Jul 1, 2020
0 parents commit 63a842b
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 0 deletions.
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
Yii2 Select2 Extension
======================
Yii2 Select2 Extension

Installation
------------

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
composer require --prefer-dist mhunesi/yii2-select2 "*"
```

or add

```
"mhunesi/yii2-select2": "*"
```

to the require section of your `composer.json` file.


Usage
-----

Once the extension is installed, simply use it in your code by :

``` php
<?= \mhunesi\select2\Select2Widget::widget([
'id' => 'my-id',
'name' => 'my-name',
'items' => ['1' => 'Deneme']
]) ?>
```

``` php
<?= $form
->field($model, 'address')
->widget(\mhunesi\select2\Select2Widget::className(),
[
'items' => [1 => 'Address Title 1', 2 => 'Address Title 2'],
'options' => [
'options' => [
1 => [
'data-title' => 'Address Title 1',
'data-name' => 'Mustafa Hayri',
'data-lastname' => 'ÜNEŞİ',
'data-country' => 'Country',
'data-province' => 'Province',
'data-phone' => '+90 542 999 99 99',
'data-address' => '214 West 36th Street',
],
2 => [
'data-title' => 'Address Title 2',
'data-name' => 'Mustafa Hayri',
'data-lastname' => 'ÜNEŞİ',
'data-country' => 'Country',
'data-province' => 'Province',
'data-phone' => '+90 542 999 99 99',
'data-address' => '214 West 36th Street',
]
]
],
'clientOptions' => [
'templateResult' => new JsExpression('
function(item){
if (!item.id) {
return item.text;
}
var data = $(item.element).data();
var template = $(
`<strong>${data.title}</strong> <br>
<span>${data.address} - ${data.province} / ${data.country}</span> <br>
<span>${data.name} ${data.lastname} ${data.phone}</span>`
);
return template;
}
')
]
]
) ?>
```

19 changes: 19 additions & 0 deletions Select2Asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace mhunesi\select2;

class Select2Asset extends \yii\web\AssetBundle
{
public $sourcePath = '@bower/select2/dist';

public $css = [
'css/select2.min.css',
];

public $js = [
'js/select2.min.js',
];

public $depends = [
'yii\web\JqueryAsset'
];
}
122 changes: 122 additions & 0 deletions Select2Widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace mhunesi\select2;

use mhunesi\select2\Select2Asset;
use yii\widgets\InputWidget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\View;

/**
* This is just an example.
*/
class Select2Widget extends InputWidget
{

/**
* @var array $items the option data items. The array keys are option values, and the array values
* are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too).
* For each sub-array, an option group will be generated whose label is the key associated with the sub-array.
* If you have a list of data models, you may convert them into the format described above using
* [[\yii\helpers\ArrayHelper::map()]].
*
* @see [[\yii\helpers\Html::activeDropDownList()]]
*/
public $items = [];
/**
* @var array the options for the underlying Select2 JS plugin.
* Please refer to the plugin Web page for possible options.
*
* @see https://select2.github.io/options.html#core-options
*/
public $clientOptions = [];
/**
* @var array the event handlers for the underlying Select2 JS plugin.
* Please refer to the corresponding plugin Web page for possible events.
*
* @see https://select2.github.io/options.html#events
*/
public $clientEvents = [];

/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->initPlaceholder();
}

/**
* Select2 plugin placeholder check and initialization
*/
protected function initPlaceholder()
{
$multipleSelection = ArrayHelper::getValue($this->options, 'multiple');

if (!empty($this->options['prompt']) && empty($this->clientOptions['placeholder'])) {
$this->clientOptions['placeholder'] = $multipleSelection
? ArrayHelper::remove($this->options, 'prompt')
: $this->options['prompt'];

return null;
} elseif (!empty($this->options['placeholder'])) {
$this->clientOptions['placeholder'] = ArrayHelper::remove($this->options, 'placeholder');
}
if (!empty($this->clientOptions['placeholder']) && !$multipleSelection) {
$this->options['prompt'] = is_string($this->clientOptions['placeholder'])
? $this->clientOptions['placeholder']
: ArrayHelper::getValue((array)$this->clientOptions['placeholder'], 'placeholder', '');
}
}

/**
* @inheritdoc
*/
public function run()
{
if ($this->hasModel()) {
echo Html::activeDropDownList($this->model, $this->attribute, $this->items, $this->options);
} else {
echo Html::dropDownList($this->name, $this->value, $this->items, $this->options);
}
$this->registerClientScript();
}

/**
* @inheritdoc
*/
public function registerClientScript()
{
$view = $this->getView();

$this->registerBundle($view);

$options = !empty($this->clientOptions)
? Json::encode($this->clientOptions)
: '';

$id = $this->options['id'];

$js[] = ";jQuery('#$id').select2($options);";
if (!empty($this->clientEvents)) {
foreach ($this->clientEvents as $event => $handler) {
$js[] = "jQuery('#$id').on('$event', $handler);";
}
}

$view->registerJs(implode("\n", $js));
}

/**
* Registers asset bundle
*
* @param View $view
*/
protected function registerBundle(View $view)
{
Select2Asset::register($view);
}
}
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "mhunesi/yii2-select2",
"description": "Yii2 Select2 Extension",
"type": "yii2-extension",
"keywords": ["yii2","extension","select2"],
"license": "MIT",
"authors": [
{
"name": "Mustafa Hayri ÜNEŞİ",
"email": "mhunesi@gmail.com"
}
],
"require": {
"yiisoft/yii2": "~2.0.0",
"bower-asset/select2": "~4.0"
},
"autoload": {
"psr-4": {
"mhunesi\\select2\\": ""
}
}
}

0 comments on commit 63a842b

Please sign in to comment.