Commit dcde2c67 by Qiang Xue

Merge pull request #1109 from resurtm/fixes-1104-gii-table-autocomplete

Fixes #1104. Model generator table names auto complete.
parents 50ba7602 4cb1a264
...@@ -111,6 +111,17 @@ abstract class Generator extends Model ...@@ -111,6 +111,17 @@ abstract class Generator extends Model
} }
/** /**
* Returns the list of auto complete values.
* The array keys are the attribute names, and the array values are the corresponding auto complete values.
* Auto complete values can also be callable typed in order one want to make postponed data generation.
* @return array the list of auto complete values
*/
public function autoCompleteData()
{
return [];
}
/**
* Returns the message to be displayed when the newly generated code is saved successfully. * Returns the message to be displayed when the newly generated code is saved successfully.
* Child classes may override this method to customize the message. * Child classes may override this method to customize the message.
* @return string the message to be displayed when the newly generated code is saved successfully. * @return string the message to be displayed when the newly generated code is saved successfully.
......
...@@ -26,12 +26,14 @@ class GiiAsset extends AssetBundle ...@@ -26,12 +26,14 @@ class GiiAsset extends AssetBundle
*/ */
public $css = [ public $css = [
'main.css', 'main.css',
'typeahead.js-bootstrap.css',
]; ];
/** /**
* @inheritdoc * @inheritdoc
*/ */
public $js = [ public $js = [
'gii.js', 'gii.js',
'typeahead.js',
]; ];
/** /**
* @inheritdoc * @inheritdoc
......
...@@ -201,3 +201,11 @@ body { ...@@ -201,3 +201,11 @@ body {
.DifferencesInline .ChangeReplace del { .DifferencesInline .ChangeReplace del {
background: #e99; background: #e99;
} }
/* additional styles for typeahead.js-bootstrap.css */
.twitter-typeahead {
display: block !important;
}
.twitter-typeahead .tt-hint {
padding: 6px 12px !important;
}
/* always keep this link here when updating this file: https://github.com/jharding/typeahead.js-bootstrap.css */
.twitter-typeahead .tt-query,
.twitter-typeahead .tt-hint {
margin-bottom: 0;
}
.tt-dropdown-menu {
min-width: 160px;
margin-top: 2px;
padding: 5px 0;
background-color: #fff;
border: 1px solid #ccc;
border: 1px solid rgba(0,0,0,.2);
*border-right-width: 2px;
*border-bottom-width: 2px;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
-webkit-box-shadow: 0 5px 10px rgba(0,0,0,.2);
-moz-box-shadow: 0 5px 10px rgba(0,0,0,.2);
box-shadow: 0 5px 10px rgba(0,0,0,.2);
-webkit-background-clip: padding-box;
-moz-background-clip: padding;
background-clip: padding-box;
}
.tt-suggestion {
display: block;
padding: 3px 20px;
}
.tt-suggestion.tt-is-under-cursor {
color: #fff;
background-color: #0081c2;
background-image: -moz-linear-gradient(top, #0088cc, #0077b3);
background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#0088cc), to(#0077b3));
background-image: -webkit-linear-gradient(top, #0088cc, #0077b3);
background-image: -o-linear-gradient(top, #0088cc, #0077b3);
background-image: linear-gradient(to bottom, #0088cc, #0077b3);
background-repeat: repeat-x;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff0088cc', endColorstr='#ff0077b3', GradientType=0)
}
.tt-suggestion.tt-is-under-cursor a {
color: #fff;
}
.tt-suggestion p {
margin: 0;
}
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace yii\gii\components; namespace yii\gii\components;
use yii\gii\Generator; use yii\gii\Generator;
use yii\helpers\Json;
/** /**
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
...@@ -30,10 +31,18 @@ class ActiveField extends \yii\widgets\ActiveField ...@@ -30,10 +31,18 @@ class ActiveField extends \yii\widgets\ActiveField
if (isset($hints[$this->attribute])) { if (isset($hints[$this->attribute])) {
$this->hint($hints[$this->attribute]); $this->hint($hints[$this->attribute]);
} }
$autoCompleteData = $this->model->autoCompleteData();
if (isset($autoCompleteData[$this->attribute])) {
if (is_callable($autoCompleteData[$this->attribute])) {
$this->autoComplete(call_user_func($autoCompleteData[$this->attribute]));
} else {
$this->autoComplete($autoCompleteData[$this->attribute]);
}
}
} }
/** /**
* Makes filed remember its value between page reloads * Makes field remember its value between page reloads
* @return static the field object itself * @return static the field object itself
*/ */
public function sticky() public function sticky()
...@@ -41,4 +50,17 @@ class ActiveField extends \yii\widgets\ActiveField ...@@ -41,4 +50,17 @@ class ActiveField extends \yii\widgets\ActiveField
$this->options['class'] .= ' sticky'; $this->options['class'] .= ' sticky';
return $this; return $this;
} }
/**
* Makes field auto completable
* @param array $data auto complete data (array of callables or scalars)
* @return static the field object itself
*/
public function autoComplete($data)
{
static $counter = 0;
$this->inputOptions['class'] .= ' typeahead-' . (++$counter);
$this->form->getView()->registerJs("jQuery('.typeahead-{$counter}').typeahead({local: " . Json::encode($data) . "});");
return $this;
}
} }
...@@ -113,6 +113,18 @@ class Generator extends \yii\gii\Generator ...@@ -113,6 +113,18 @@ class Generator extends \yii\gii\Generator
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function autoCompleteData()
{
return [
'tableName' => function () {
return $this->getDbConnection()->getSchema()->getTableNames();
},
];
}
/**
* @inheritdoc
*/
public function requiredTemplates() public function requiredTemplates()
{ {
return ['model.php']; return ['model.php'];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment