javascript - Sorting in a dropdown -



javascript - Sorting in a dropdown -

i'm populating few drop downs json, works expected @ moment i'm trying sort values in drop downs. drop downs works fine, except 1 of them has numbers in, in case lists 1, 10, 12, 2.

any ideas how can maintain sorting alphabetically else sort work numeric values too?

here's js (this replicates each field - should seek find way create reuseable):

var populategenres = _.map(data.genres, function (val) { homecoming '<option>' + val + '</option>'; }).join(); var target = '#genrefield'; $('#genrefield').html(populategenres); rearrangeselect(target);

here's sort js:

function rearrangeselect(target) { $(target).html($(target + " option").sort(function(a, b) { homecoming a.text == b.text ? 0 : a.text < b.text ? -1 : 1 })) }

my html in format:

<td> <select id="genrefield" class="form-control"></select> </td> <td> <select id="authorfield" class="form-control"></select> </td>

function rearrangeselect(target) { $(target).html($(target + " option").sort(function(a, b) { // u can utilize 'getvalue' function // "a.text == 'some string'" homecoming 'some string' (string type), // "a.text == '10'" homecoming 10 (number type) var aval = getvalue(a.text); var bval = getvalue(b.text); homecoming aval == bval ? 0 : aval < bval ? -1 : 1; })); } function getvalue(val) { var asnumber = parsefloat(val); homecoming isnan(asnumber) ? val : asnumber; }

javascript jquery

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -