Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ActiveField.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\gii\components;
  8. use yii\gii\Generator;
  9. use yii\helpers\Json;
  10. /**
  11. * @author Qiang Xue <qiang.xue@gmail.com>
  12. * @since 2.0
  13. */
  14. class ActiveField extends \yii\widgets\ActiveField
  15. {
  16. /**
  17. * @var Generator
  18. */
  19. public $model;
  20. /**
  21. * @inheritdoc
  22. */
  23. public function init()
  24. {
  25. $stickyAttributes = $this->model->stickyAttributes();
  26. if (in_array($this->attribute, $stickyAttributes)) {
  27. $this->sticky();
  28. }
  29. $hints = $this->model->hints();
  30. if (isset($hints[$this->attribute])) {
  31. $this->hint($hints[$this->attribute]);
  32. }
  33. $autoCompleteData = $this->model->autoCompleteData();
  34. if (isset($autoCompleteData[$this->attribute])) {
  35. if (is_callable($autoCompleteData[$this->attribute])) {
  36. $this->autoComplete(call_user_func($autoCompleteData[$this->attribute]));
  37. } else {
  38. $this->autoComplete($autoCompleteData[$this->attribute]);
  39. }
  40. }
  41. }
  42. /**
  43. * Makes field remember its value between page reloads
  44. * @return $this the field object itself
  45. */
  46. public function sticky()
  47. {
  48. $this->options['class'] .= ' sticky';
  49. return $this;
  50. }
  51. /**
  52. * Makes field auto completable
  53. * @param array $data auto complete data (array of callables or scalars)
  54. * @return $this the field object itself
  55. */
  56. public function autoComplete($data)
  57. {
  58. static $counter = 0;
  59. $this->inputOptions['class'] .= ' typeahead typeahead-' . (++$counter);
  60. foreach ($data as &$item) {
  61. $item = ['word' => $item];
  62. }
  63. $this->form->getView()->registerJs("yii.gii.autocomplete($counter, " . Json::htmlEncode($data) . ");");
  64. return $this;
  65. }
  66. }