Need Help Sorting Categories

Store UI, layout, design, look and feel; Discussion on the customer facing pages of your online store. Cascading Style Sheets, Themes, Scriptlets, NVelocity and the components in the ConLib directory.
Post Reply
euroluxantiques
Commander (CMDR)
Commander (CMDR)
Posts: 118
Joined: Sat Dec 20, 2008 11:27 pm

Need Help Sorting Categories

Post by euroluxantiques » Mon Jun 20, 2011 3:06 pm

I have built a category flyout menu using CSS and the following code. It's based on the SimpleCategory conlib, but I want to be able to sort the categories based on ac_CatalogNodes.OrderBy field. The problem is that the CategoryCollection/CategoryDataSource.LoadForStore(sortExpression) doesn't support that field. To use that field, I have to use CatalogNodeCollection/CatalogNodeDataSource, but I can't figure out how to modify the rest of the code to then do what it does now without blowing it all up. As is, it works really well for me, but I just want to be able to sort the categories as they are in the admin panel. Any thoughts/suggstions would be very appreciated. Here's the code:

Code: Select all

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CommerceBuilder.Common;
using CommerceBuilder.Marketing;
using CommerceBuilder.Orders;
using CommerceBuilder.Utility;
using CommerceBuilder.Catalog;
using CommerceBuilder.Products;
using CommerceBuilder.Stores;


public partial class CategoryFlyout : UserControl
{
    StringBuilder sb = new StringBuilder();
    private CustomField _CustomField;

    protected void Page_Load(object sender, System.EventArgs e)
    {

        
        CategoryCollection subCategories = CategoryDataSource.LoadForStore();
  

        if (subCategories.Count == 0)
        {
            return;
        }

        

        

        sb.Append("<ul id='sidenav' class='level1'>");

        foreach (Category category in subCategories)
   
        {

            if (category.ParentId == 0)
             {

                CustomFieldCollection customFields = CustomFieldDataSource.LoadForCriteria(String.Format(" StoreId = {0} AND TableName='{1}' AND ForeignKeyId={2} ", Token.Instance.Store.StoreId, "ac_Categories", category.CategoryId));
                if (customFields != null && customFields.Count > 0)
                    _CustomField = customFields[0];

                string customField;
                if ((_CustomField != null) && (_CustomField.FieldValue != null))
                    customField=_CustomField.FieldValue;
                else
                    customField="";


                if (CategoryDataSource.CountForParent(category.CategoryId) > 0)
                {
                    //CustomFieldCollection customFields = CustomFieldDataSource.LoadForCriteria(String.Format(" StoreId = {0} AND TableName='{1}' AND ForeignKeyId={2} ", Token.Instance.Store.StoreId, "ac_Categories", category.CategoryId));
                    //sb.Append("<ul>");
                    sb.Append("<li id='" + customField + "' class='folder'><span>" + category.ThumbnailAltText + "</span><ul class='level2'><li><a class='submenu catTitle' href='" + Regex.Replace(category.NavigateUrl, "~", "") + "'>" + category.Name + "</a></li>");
                    RecursiveLoadChildren(category.CategoryId);
                    sb.Append("</ul></li>");
                }
                else
                {
                    sb.Append("<li id='" + customField + "' class='folder'><span>" + category.ThumbnailAltText + "</span><ul class='level2'><li><a class='submenu catTitle' href='" + Regex.Replace(category.NavigateUrl, "~", "") + "'>" + category.Name + "</a></li>");
                    sb.Append("</li>");
                }


            }

        }

        
        sb.Append("</ul>");
        CategoryDetail.Text = sb.ToString();
        
    }

    protected void RecursiveLoadChildren(int CatId)
        {
            CategoryCollection children = CategoryDataSource.LoadForParent(CatId, true);

            foreach (Category cat in children)
            {
                if (CategoryDataSource.CountForParent(cat.CategoryId)>0)
                {
                    sb.Append("<li class='subParent'><a href='" + Regex.Replace(cat.NavigateUrl, "~", "") + "' class='submenu'>" + cat.Name + " > </a><ul>");
                    RecursiveLoadChildren(cat.CategoryId);
                    sb.Append("</ul></li>");
                }
                else
                {
                    if (cat.CatalogNodes.Count > 0)
                    {
                        sb.Append("<li><a class='submenu' href='" + Regex.Replace(cat.NavigateUrl, "~", "") + "'>" + cat.Name + "</a></li>");
                    }
                }
            }

        }

    
 
    }

User avatar
crockettdunn
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 105
Joined: Sun Oct 26, 2008 6:32 pm
Contact:

Re: Need Help Sorting Categories

Post by crockettdunn » Tue Jul 08, 2014 10:53 pm

I changed
CategoryCollection subCategories = CategoryDataSource.LoadForStore();
to
CategoryCollection subCategories = CategoryDataSource.LoadForParent(0,true);

and it seems to be honoring the admin-specified OrderBy

Post Reply