Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function actionDependedSelect($name, $parentId = null, $selectedId = null)
- {
- echo \backend\next\helpers\CategoryHelper::dependedSelect($name, $parentId, $selectedId);
- }
- // --------------------
- class CategoryHelper
- {
- /**
- * TODO: adjust interface to match Html::dropDownList().
- *
- * @param string $name
- * @param mixed|null $parentId
- * @param mixed|null $selectedId
- *
- * @return string
- */
- public static function dependedSelect($name, $parentId = '', $selectedId = '')
- {
- if (!$parentId) {
- $parentId = null;
- }
- if (!$selectedId) {
- $selectedId = null;
- }
- $category = Category::findOne($parentId);
- $html = '';
- if ($category) {
- $html = self::dependedSelect($name, $category->parent_id, $parentId);
- }
- /** @var Category[] $categories */
- $categories = Category::find()
- ->withI18n(Language::EN)
- ->select([
- 'name',
- 'lv_category.id',
- ])
- ->where([
- 'parent_id' => $parentId,
- ])
- ->indexBy('id')
- ->column();
- if ($categories) {
- $html .= '<select name="'.e($name).'">';
- $html .= '<option value=""></option>';
- $html .= Html::renderSelectOptions($selectedId, $categories);
- $html .= '</select>';
- }
- return $html;
- }
- }
- // ---------------------
- <script>
- function getCategoryId() {
- return $('#categories')
- .find('select')
- .map(function(idx, elem) {
- return elem.value;
- })
- .filter(function(idx, elem) {
- return elem;
- })
- .get()
- .pop();
- }
- $('#categories').on('change', 'select', function () {
- var categoryId = this.value;
- $('#categories').find('select').attr('disabled', 'disabled');
- $.get("<?= $this->createAbsoluteUrl('nextCategory/dependedSelect') ?>", {
- name: 'category_id',
- parentId: categoryId,
- selectedId: categoryId,
- }).done(function (data) {
- $('#categories').html(data);
- $('#categoryform-parentid').val(getCategoryId());
- });
- })
- <script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement