Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.40 KB | None | 0 0
  1.     public function actionDependedSelect($name, $parentId = null, $selectedId = null)
  2.     {
  3.         echo \backend\next\helpers\CategoryHelper::dependedSelect($name, $parentId, $selectedId);
  4.     }
  5.  
  6. // --------------------
  7.  
  8. class CategoryHelper
  9. {
  10.     /**
  11.      * TODO: adjust interface to match Html::dropDownList().
  12.      *
  13.      * @param string     $name
  14.      * @param mixed|null $parentId
  15.      * @param mixed|null $selectedId
  16.      *
  17.      * @return string
  18.      */
  19.     public static function dependedSelect($name, $parentId = '', $selectedId = '')
  20.     {
  21.         if (!$parentId) {
  22.             $parentId = null;
  23.         }
  24.  
  25.         if (!$selectedId) {
  26.             $selectedId = null;
  27.         }
  28.  
  29.         $category = Category::findOne($parentId);
  30.  
  31.         $html = '';
  32.         if ($category) {
  33.             $html = self::dependedSelect($name, $category->parent_id, $parentId);
  34.         }
  35.  
  36.         /** @var Category[] $categories */
  37.         $categories = Category::find()
  38.             ->withI18n(Language::EN)
  39.             ->select([
  40.                 'name',
  41.                 'lv_category.id',
  42.             ])
  43.             ->where([
  44.                 'parent_id' => $parentId,
  45.             ])
  46.             ->indexBy('id')
  47.             ->column();
  48.  
  49.         if ($categories) {
  50.             $html .= '<select name="'.e($name).'">';
  51.             $html .= '<option value=""></option>';
  52.             $html .= Html::renderSelectOptions($selectedId, $categories);
  53.             $html .= '</select>';
  54.         }
  55.  
  56.         return $html;
  57.     }
  58. }
  59.  
  60. // ---------------------
  61. <script>
  62.     function getCategoryId() {
  63.         return $('#categories')
  64.             .find('select')
  65.             .map(function(idx, elem) {
  66.                 return elem.value;
  67.             })
  68.             .filter(function(idx, elem) {
  69.                 return elem;
  70.             })
  71.             .get()
  72.             .pop();
  73.     }
  74.  
  75.     $('#categories').on('change', 'select', function () {
  76.         var categoryId = this.value;
  77.  
  78.         $('#categories').find('select').attr('disabled', 'disabled');
  79.  
  80.         $.get("<?= $this->createAbsoluteUrl('nextCategory/dependedSelect') ?>", {
  81.             name: 'category_id',
  82.             parentId: categoryId,
  83.             selectedId: categoryId,
  84.         }).done(function (data) {
  85.             $('#categories').html(data);
  86.             $('#categoryform-parentid').val(getCategoryId());
  87.         });
  88.     })
  89. <script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement