English 中文(简体)
第1项
原标题:sort items in a dropdown list without the first item

我有以下代码将项目分类为下级清单:

function sortDropDownListByText(selectId) {
    $(selectId).html($(selectId + " option").sort(function(a, b) {
       return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
    })) 
}

除非在我的第一个项目有** ——从名单上挑选和列入项目”的信息中填写,否则该作品会受到罚款。

是否有办法将选定清单中的项目分类,并将“请选择条目”作为清单中的第一个项目。

EDIT:

www.un.org/Depts/DGACM/index_spanish.htm 针对一些答复,“请选择的项目总价值为0”。

最佳回答
function sortDropDownListByText(selectId) {
    var foption = $( # + selectId +   option:first );
    var soptions = $( # + selectId +   option:not(:first) ).sort(function(a, b) {
       return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
    });
    $( #  + selectId).html(soptions).prepend(foption);              

};

这是你的职责。

问题回答

从理论上讲,我将通过删除“请选择”条目来处理这个问题,对名单进行分类,然后在进行分类后再次加以更正。

将第一个项目从选箱中删除,然后在选箱后的第一个位置上加起来。

$(selectId).html($(selectId + " option:not( :first-child )").sort(function(a, b) {
       return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
    }))

如何总是将该项目退回<代码>-1?

$(selectId).html($(selectId + " option").sort(function(a, b) {
   return a.text == "Please select an item from the list" ? -1 : a.text < b.text ? -1 : 1;
});

更有活力:

$(selectId).html($(selectId + " option").sort(function(a, b) {
   return a.text == $(selectId +  option:first ).text ? -1 : a.text < b.text ? -1 : 1;
});

如果“请选择......”办法具有与它相关的某种价值(如“dummyVal”),你可以在你的比较职能中使用:

function sortDropDownListByText(selectId, dummyVal) {
    $(selectId).html($(selectId + " option").sort(function(a, b) {
        if (a.value == dummyVal) {
            return -1;
        }
        return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
    })) 
}

首先,你们必须保持所选择的价值,一旦你完成重新计算所保存的价值。

#store the selected value.
var selectedVal = $(selectId).val(); 

# start sorting.
$(selectId).html( $(selectId+" option").sort(function(a, b) {
       return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
    }));

#re-select the preserved value.
$(selectId).val(selectedVal); 




相关问题
How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

Can I Nest OrderBy in .NET?

This doesn t seem to work as I intend. VB.NET: Dim x = Model.Discussions.OrderByDescending(Function(d) d.Messages.OrderByDescending(Function(m) m.Sent).First.Sent) For Each d As Discussion In x ....

sorting elements javascript

I m looking for a way to sort my elements, but it isn t as easy as it sounds. Please let me explain My elements are grouped per 6 elements (thumbnails), each x represents a thumbnail However all ...

热门标签