1

Is there any way I can display the most selected items first in a drop-down?

E.g I have a drop-down which is loaded with 6 items when the application loads:

  • A
  • B
  • C
  • D
  • E
  • F


Lets say the user selects item-C and submits the form.
When the user comes back to this form, I want to sort the dropdown by the most selected items, So in this case the dropdown would be like this:

  • C
  • A
  • B
  • D
  • E
  • F

Is there any jquery plugin that can do this? or do I need to have a table in my database that will store the most selected items?(this would be my last resort)

Luthando Ntsekwa
  • 4,192
  • 6
  • 23
  • 52
  • Data related to most selection should be persisted on backend side. – Vaibhav Mar 16 '15 at 06:48
  • @Vaibhav so the best option would be to store the most selected items in my database? – Luthando Ntsekwa Mar 16 '15 at 06:54
  • Yes, so whenever you login again then also you would be having the most selected values. – Vaibhav Mar 16 '15 at 06:56
  • @Vaibhav this could work but it means whenever i need this feature on some other drop-down with different data, I will have to add another table. – Luthando Ntsekwa Mar 16 '15 at 07:01
  • Not table, probably a column in some table. And this depends on your requirement. If after logout, or you dont want to have most selected property then we can have an implementation on client side. – Vaibhav Mar 16 '15 at 07:03

1 Answers1

1

You did not specify whether you want any user to see the frequency of responses by all users, or to have the same user always see the answers he/she selected most often.

If you want the former, you need some persistence on server side, e.g. very simple database (two columns: selectedOption, howManyTimesSelected). Even storing in a file would do, but you should then provide some way of securing against race condition (e.g. allow the file to be open for writing by one instance only).

If you want the latter, you can store that information in a cookie on client's side.

dust
  • 502
  • 6
  • 19
  • I want the same user to always see the answers he/she selected most often. The user can login today > do some selections > submit >logout. When he/she login again, i want the most selected items to be at the top. – Luthando Ntsekwa Mar 16 '15 at 09:34
  • In that case just use cookies, no need to do anything on the server side. See for example this answer: http://stackoverflow.com/questions/1458724/how-to-set-unset-cookie-with-jquery Set your cookie to the current order of selected responses, and read that order when loading the page next time. That's all. Just bear in mind that the response order will be saved on client side. This means that if they e.g. clean the history in their browser or use a different browser, the order will be reset to default. If this is an issue, then you still need to use server-side persistence. – dust Mar 16 '15 at 09:45