Get parent ids

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 anos atrás
Hi all,

Just wondering if anyone has any code for 2.3 that will get me a list of parent id's from a category id?

I have a hard coded menu with categories, and want to check to see if any of the selected category's children are active, if so, change the class of the menu.

I have it so it's highlighted if you're on the top parent category, but any below it I presume I need to go up to find the parent and check that.

Thanks in advance,

Dave
12 anos atrás
You can do something like this in the catalogcontroller.   I think when you hit the top the Parent Cat Id=0 and you may need to play around with the condition of the loop whether you want the top or not/etc.  You can then update your catalog model to include this list of category id's.  This is probably the cleanest way, there maybe a quick and dirty making some calls directly from the view.



private List<int> GetParents(int categoryId)
        {
            List<int> categories = new List<int>();
            Category cat = _categoryService.GetCategoryById(categoryId);
            while (cat!=null &&  cat.ParentCategoryId!=null && cat.ParentCategoryId!=0 )
            {
                categories.Add(cat.ParentCategoryId);
                cat = _categoryService.GetCategoryById(cat.ParentCategoryId);
            }

            return categories;
        }

12 anos atrás
inspired wrote:
Hi all,

Just wondering if anyone has any code for 2.3 that will get me a list of parent id's from a category id?

I have a hard coded menu with categories, and want to check to see if any of the selected category's children are active, if so, change the class of the menu.

I have it so it's highlighted if you're on the top parent category, but any below it I presume I need to go up to find the parent and check that.

Thanks in advance,

Dave


use jquery to find the previous active id and then use that id to highlight the parent id, something like this

var activeId = $('.active').attr('id')

$('.active ').css('background-image', 'url(../active-image.png)');

$('.active a').css('color', 'Red!Important');
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.