Friday, July 24, 2009

Vertical Subcategory Listing in Entity XMLPackage

Last week, I was dead worried on how to do this.  The default in ASPDNSF is horizontal listing.  Although it's okay for my client to display that way, he wanted a vertical listing more.  I consulted the forum but to no avail.  Finally, I came up with a solution.  It's a very long one, and I must admit, it's not the best one either.  I'm hoping there's a shorter one, but for now, since I have a tight deadline, I'm okay with this solution.

Here's what I did.  I created a new XSLT function that accepts the parameters WebsiteID (This is multistore so WebsiteID is necessary), ParentCategoryID, and the NodeCount (although personally, I don't think this is necessary anymore).  This function does all the work.  I can't think of anything else that will only touch the XMLPackage so I went deeper.

In my XSLT function, I created an ArrayList of Items that will hold the nodes (or the subcategories) of my query.  I also created a temporary ArrayList that will hold each field of my subcategories.  This temporary one will be stored in ArrayList Items.

then made this code:
// sql query to store categor fields into temporary ArrayList
// then stores the temporary ArrayList to Items

double SubCategoryCount = Items.Count;

int ItemsPerColumn = (int) Math.Ceiling(SubCategoryCount / 3); // I only have three columns
int counter = 0;
StringBuilder str = new StringBuilder();

foreach (ArrayList al in Items) //traverses all subcategories in Items
        {
            if (counter < ItemsPerColumn)
            {
                str.Append("<tr>");
                // code for the first column

                if ((counter + ItemsPerColumn) < Items.Count) // checks if this index is not yet out of bounds
                {
                     ArrayList al2 = (ArrayList)Items[counter + ItemsPerColumn];
                     // code for the second column
                }

                if ((counter + ItemsPerColumn + ItemsPerColumn) < Items.Count) // checks if this index is not yet out of bounds
                {
                     ArrayList al3 = (ArrayList)Items[counter + ItemsPerColumn + ItemsPerColumn];
                     // code for the third column
                }

                str.Append("</tr>");
                counter++;
            }
            else
            {
                break;
            }
        }
This is a very long solution but works fine with me, so I'm sticking to this for now.


No comments: