Sorted Products - Customization

Miscellaneous Actinic / SellerDeck patches and anything not covered above.
Post Reply
jadetechnology
Posts: 1
Joined: Mon Apr 18, 2011 1:04 pm

Sorted Products - Customization

Post by jadetechnology »

Hi.
I have been implementing the customization code for the sorted products add-on.

I have entered the following lines of code into the JavaScript in the appropriate places.

Code: Select all

capacity: spans[i].getAttribute('capacity'),
if ( seqtype == 'capacity' )  prodlines.sort(function(a,b){return (b.capacity <= a.capacity) ? -1 : 1;});
spanids[i].setAttribute('capacity', prodlines[i].capacity);
[/color]

The scripting is basically working but with a few exceptions.
I have entered these customizations in order to sort my products depending on their capacity in Kg.
When I use the sort by capacity funtion the list puts things in order starting from 9 Kg, then 8 Kg and so on as expected. However, items with 11 Kg or 12 Kg capacity are listed at the bottom of the list, probably because the value begins with 1.

I tried altering the code to:

Code: Select all

capacity: spans[i].getAttribute('capacity'),
if ( seqtype == 'highlow' )  prodlines.sort(function(a,b){return (b.capacity <= a.capacity) ? -1 : 1;});
spanids[i].setAttribute('capacity', prodlines[i].capacity);
[/color]

but this did not work. Any ideas on how I can get my list into order would be great.
norman
Site Admin
Posts: 1252
Joined: Sat Feb 07, 2004 9:55 pm

Re: Sorted Products - Customization

Post by norman »

You need to make sure that your variable contains a numeric value only. E.g. 9, 10, etc. Not 9Kg, 10Kg, etc.

Then do a numeric comparison:

Code: Select all

if ( seqtype == 'capacity' )  prodlines.sort(function(a,b){return (a.capacity - b.capacity)});
Or you could use values like 8Kg, etc and strip all non numeric characters before comparing:

Code: Select all

if ( seqtype == 'capacity' )  prodlines.sort(function(a,b){return (a.capacity.replace(/[^\d\.]/g,'') - b.capacity.replace(/[^\d\.]/g,''))});
N.B. Both above untested.

Note that I now have a new version of this add-on that uses jQuery. The supporting code is much more concise and easier to tweak.
Norman
Post Reply