Ordering a select drop down from distinct values from query

ok, so I have Select field in a form that is populated from distinct values that are returned from a query. I want those to be sorted in a certain order, they are fraction values so I can’t just sort them alphabetically etc. The order should be like 1/8”, 3/16”, and 1/4”, I can’t user a straight static value in the select as not every product has the same options available and should only show the ones that are there.

I think we need to see some sample data, data to be selected and data not to be.

It’s exact format will be highly relevant

ok, here is the page, and then a sample of the data. I query the data set and then filter it with a dataview to get the SKU for the order etc. all works as it should other than the order of the thickness etc

Always fractional or could it be say, “1 3/16”

Metric is soooo much easier!

always fraction. Agreed on the metric!

Is the data for the thickness stored in the database like 1/8" or just 1/8 ?

1/8”

Probably going to be best to apply a sort at App connect level becuase you cant use calculated fields in query manager, custom query would be neccessary, perhaps at bapp connect end split() on “/” then divide part[0] by part[1] to create a decimal value to sort on.

Oh, and replace that perky inches sign with null first

A custom formatter of course, may give more flexibility

Yes, we brits invented the imperial measurement system then ditched it in the 70s/80s for decimal, money first then the rest.
Imagine it the UK still had 12 pennies in a shilling, 20 shillings (240 pennies) in a pound. Accounting would be fun!

1 Like

You can create a custom formatter that formats bald eagles per donut to regular numbers.

dmx.Formatters('string', {
  baldEagleToMetric: function(str) {
    str = str.replace(/"/g, '').trim();
    let result = 0;
    if (str.includes('/')) {
      const parts = str.split(' ');
      if (parts.length > 1) {
        const wholeNumber = parseFloat(parts[0]);
        const fraction = parts[1].split('/');
        const numerator = parseFloat(fraction[0]);
        const denominator = parseFloat(fraction[1]);
        result = wholeNumber + (numerator / denominator);
      } else {
        const fraction = str.split('/');
        const numerator = parseFloat(fraction[0]);
        const denominator = parseFloat(fraction[1]);
        result = numerator / denominator;
      }
    } else {
      result = parseFloat(str);
    }
    return result;
  }
});

and then as you can't directly apply formatters when sorting the data view, apply it directly on the dynamic select expression, so it becomes:

dmx-bind:options="data_view1.data.sort(`thickness.baldEagleToMetric()`)"

Maybe in the future updates @patrick can extend the sort option of the data view to support formatters so you can apply it there directly.

1 Like

That would be brilliant, pushing my luck here. What about applying formatters directly to database fields to transform them in the query?

Please open a feature request :slight_smile:

1 Like

@teodor I think your formatter is doing the job! if not I’ll check back. Thanks for your help!

1 Like

Already did that some time ago

Formatters, cast and convert would be nice

(only been 7 years)

1 Like