Jump to content

Allow us to sort autocomplete results in "\IPS\Helpers\Form"


teraßyte

Recommended Posts

As per title ^, in the default options for the class you have this line:

'orderResults'      => NULL|array                   // NULL or array( field, 'asc' ) where field is a mappable content item field (date, title, etc)

 

Unfortunately that option has effect ONLY when displaying values passed to the field. In my case I have an autocomplete field that allows only 1 item in it but I want to sort the autocomplete results by the same field/direction. That is not possible currently because you automatically sort based on the LENGTH of the field in MySQL on line 98:

foreach( $class::getItemsWithPermission( $where, 'LENGTH(' . $field . ') ASC', array( 0, 20 ), $this->options['permissionCheck'] ) as $item )

 

Can you make it so that orderResults gets applied on that query too? And if no value is specific for the field fallback to the current LENGTH() order, or you could simply add a new option to sort autocomplete results in a separate way completely.

 

EDIT

This is how I changed the code above after extending the class in my apps:

			$order = 'LENGTH(' . $field . ') ASC';
			if ( is_array( $this->options['orderResults'] ) )
			{
				if ( isset( $class::$databaseColumnMap[ $this->options['orderResults'][0] ] ) )
				{
					$order = $class::$databaseTable . '.' . $class::$databasePrefix . $class::$databaseColumnMap[ $this->options['orderResults'][0] ] . ' ' . $this->options['orderResults'][1];
				}
			}

			foreach( $class::getItemsWithPermission( $where, $order, array( 0, 20 ), $this->options['permissionCheck'] ) as $item )

 

Link to comment
Share on other sites

Can you give me an example of where/how/why you'd do this? We sort by length intentionally, because if you search for "adam" you will want the result "adam" before "adamantium" and 9 other results with a longer name, which would prevent you from selecting the correct matching result.

Link to comment
Share on other sites

3 hours ago, bfarber said:

Can you give me an example of where/how/why you'd do this? We sort by length intentionally, because if you search for "adam" you will want the result "adam" before "adamantium" and 9 other results with a longer name, which would prevent you from selecting the correct matching result.

In this specific case we are sorting lots of vehicles which include the year in their name, and we want to first show the newer years (20XX) in the results as opposed to the older years (190X).

EDIT: to clarify, with the current LENGTH() sorting all the years are in random order and it makes the users lose time trying to track down the right year they want since there is no defined order.

Link to comment
Share on other sites

Hello Brandon,

I feel obliged to remind you sorting in php itself here(or really anywhere where one is dealing with a query result set with a limit) is completely unreliable, thus this needs reworked to begin with. Let's say I have a result set needing ordered by submission, or ordered from z-a(it happens). To try to sort it in php *after* already limiting the result set means the results are wrong. The last 10 submissions, are not the last 10 if one pulls the result set limited from db not sorted by date, but then sorts it in php by date. That is merely 10 random records reordered. As it stands, the helper currently sorts by length in database, then resorts in php. This, as detailed above, is in no way reliable.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...