Page 1 of 1

ProductDataSource.LoadForCriteria Add Ability to Sort...

Posted: Fri Oct 08, 2010 4:26 pm
by sweeperq
We need to use LoadForCriteria for a certain aspect of our site, but it doesn't support the "default" admin sort order, nor does it support sorting by manufacturer name.

Would be awesome if ProductDataSource.LoadForCriteria had the ability to sort by manufacturer and the catalog node order by.

Alternatively, if ProductDataSource.NarrowSearch had the ability to have criteria specified, that would be equally awesome.

Re: ProductDataSource.LoadForCriteria Add Ability to Sort...

Posted: Sat Oct 09, 2010 5:21 am
by s_ismail
sweeperq wrote:We need to use LoadForCriteria for a certain aspect of our site, but it doesn't support the "default" admin sort order.
LoadForCriteria method load products according to admin default order. I think may be you provide some sort expression in sortExpression parameter,It should be empty or just pass 'string.empty' as sortExpression like this

Code: Select all

ProductDataSource.LoadForCriteria("YOUR_SEARCH_CRITERIA", string.Empty)
sweeperq wrote: nor does it support sorting by manufacturer name.
Would be awesome if ProductDataSource.LoadForCriteria had the ability to sort by manufacturer and the catalog node order by.
For sorting by manufacturer you need to write a custom query. ProductDataSource.LoadForCriteria method don't support sorting by manufacturer. To write a custom query have a look at this one http://wiki.ablecommerce.com/index.php/Custom_Queries
sweeperq wrote: if ProductDataSource.NarrowSearch had the ability to have criteria specified, that would be equally awesome.
Using ProductDataSource.NarrowSearch you can sort like this

Code: Select all

List<Product> products = ProductDataSource.NarrowSearch("YOUR_SEARCH_CRITERIA", 0, 0, 0, 0, "Manufacturer");

Re: ProductDataSource.LoadForCriteria Add Ability to Sort...

Posted: Sat Oct 09, 2010 8:36 am
by jmestep
The loadforcriteria produces a sortable collection, so you should be able to add sorting after it loads. If you Google on sortable collection C# you will find some code. You can also follow the pattern of Able's comparer on a page like CategoryGrid4

Code: Select all


   private class ManufacturerComparer : IComparer
    {
        SortDirection _SortDirection;
        public ManufacturerComparer(SortDirection sortDirection)
        {
            _SortDirection = sortDirection;
        }

        #region IComparer Members
        public int Compare(object x, object y)
        {
            string xMan = string.Empty;
            Product pX = null;
            if (((CatalogNode)x).CatalogNodeType == CatalogNodeType.Product)
                pX = ((Product)((CatalogNode)x).ChildObject);
            if ((pX != null) && (pX.Manufacturer != null)) xMan = pX.Manufacturer.Name;
            string yMan = string.Empty;
            Product pY = null;
            if (((CatalogNode)y).CatalogNodeType == CatalogNodeType.Product)
                pY = ((Product)((CatalogNode)y).ChildObject);
            if ((pY != null) && (pY.Manufacturer != null)) yMan = pY.Manufacturer.Name;
            if (_SortDirection == SortDirection.Ascending)
                return (xMan.CompareTo(yMan));
            return (yMan.CompareTo(xMan));
        }
        #endregion
    }

Re: ProductDataSource.LoadForCriteria Add Ability to Sort...

Posted: Thu Oct 14, 2010 12:50 pm
by sweeperq
Thanks for the responses. s_ismail, what I'm seeing when passing an empty string for the sort order is that the products are loading by ProductId ASC, not the order that I have assigned them in the Admin. The problem with custom queries is that I have not seen how I can populate a set of objects with them. And for the NarrowSearch, it accepts a search string ("Diabetic Socks"), not boolean SQL criteria ("ProductID IN (5, 9, 10)").

Judy, thanks for going through the trouble of putting together that snippet of code for me. I thought about doing that, but the categories have several products so there is paging involved. If I do as suggested, the query would return a page of products, then I would sort the collection on manufacturer. Problem is that the manufacturer may be represented on pages 1, 2, and 3, so they won't all be sorted together as intended.