ProductDataSource.LoadForCriteria Add Ability to Sort...

Post feature requests to this forum and a pre-configured poll will automatically be created for you.
Post Reply
sweeperq
Commodore (COMO)
Commodore (COMO)
Posts: 497
Joined: Tue Jan 03, 2006 2:45 pm

ProductDataSource.LoadForCriteria Add Ability to Sort...

Post by sweeperq » Fri Oct 08, 2010 4:26 pm

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.

User avatar
s_ismail
Commander (CMDR)
Commander (CMDR)
Posts: 162
Joined: Mon Nov 09, 2009 12:20 am
Contact:

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

Post by s_ismail » Sat Oct 09, 2010 5:21 am

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");

User avatar
jmestep
AbleCommerce Angel
Posts: 8164
Joined: Sun Feb 29, 2004 8:04 pm
Location: Dayton, OH
Contact:

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

Post by jmestep » Sat Oct 09, 2010 8:36 am

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
    }
Judy Estep
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx

sweeperq
Commodore (COMO)
Commodore (COMO)
Posts: 497
Joined: Tue Jan 03, 2006 2:45 pm

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

Post by sweeperq » Thu Oct 14, 2010 12:50 pm

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.

Post Reply