Use id or index on select controls?
Hi,
Say that I create a web开发者_运维技巧page where the users can set diffrent filters on the front page. Some of the filters is dropdowns or selectlists (these controls contains option emelents that holds value and text).
<select>
<option value="">Miles</option>
<option value="0">100 miles</option>
<option value="1">200 miles</option>
<option value="2">300 miles</option>
</select>
This could also involve countries, cities and so on.
The text is just for display but the value are to be mapped to somekind of object on the server side.
The question is if the value should be a Id of the choosen object or if it should just be the selected index?
If we use Id it could mean that we get large numbers after a while if the mata data changes much, the id of one option could for example be 32000. The god thing with this is that it will never be a problem to know what value the user have choosen.
If we instead take the list of choises on the server and order it by a given property, after this we generate a value/id starting from 0 and up. This will make the HTML look bether and we will never have to think about to much changes in the meta data that results in high values. The problem is when mapping it to the object on the server side, we will have to order the list again(maby cached to avoid this) and then check the match. Another problem here would be if the meta data is changed so even if the user have selected index 3 that is object with id 2304 he will instead get object with the id 2302.
I have looked at a couple of pages but when it is about filters like miles and years it seemse like thay count indexes starting from 0 (my second choise) :
usually you want the value to be the key to the element in the database, that way you know it won't change. It doesn't matter if the value can be big, since it's really a string (so value="388283" is not any bigger than value="orange")
You have a value and a selectedIndex. There is no need for an ID, it is implied in the index.
What happens on the server is a database issue and is not related to the UI.
If we use Id it could mean that we get large numbers after a while if the mata data changes much, the id of one option could for example be 32000.
Most people do not consider this a problem.
[value==index] will make the HTML look bether
I don't think this really matters. Not many people are going to be looking at your source and worrying about the length of your value
s.
Another problem here would be if the meta data is changed so even if the user have selected index 3 that is object with id 2304 he will instead get object with the id 2302.
Indeed. Which is the real problem with value==index. I'd definitely avoid this in favour of value==unique-identifier.
when it is about filters like miles and years it seemse like thay count indexes starting from 0
Can't really tell from that whether it's an index or an identifier. It might be an id of a DB object representing ‘100 miles’, or a predefined Enum type where 0==100 miles. In either case you could re-order the options without breaking anything.
精彩评论