开发者

Sort Checkbox List on the basis of checked property

I have a check-box-List which I bind to a master dataTable(DTA)... I have another dataTable (DTB) which has the values that needs to checked in the check-box-List... So I loop through all items in the check-box-list to see if it exists in the DTB and set checked = true for those items that exists.

Now I want to show the checked items first in the Check-box-list box and the unchecked items below开发者_运维百科 that.

Is there any way I can do it... A similar solution for List-Box could also be helpful. A Javascript hint is welcome too.

Thanks - Raja


if you want to sort the check-box-list on the server side, you can first add the items from DTB to the check-box-list and set their Selected value to true, then add the others form DTA and for each item in DTA make sure it's not already in the list of items.
when inserting the two lists make sure they are sorted according to a secondary sort criteria if you need one.

if you don't need the sorting to take place on the server side you can use jquery to do that quite easily.
you need to get the check_box_list_client_id from the server, you can do that using
$('#<%= CheckBoxList1.ClientID %>') jquery selector.


    $(function() {  
        // get the containing element - should be an HTML table
        var cbl = $('#check_box_list_client_id');
        // check if the jquery element has any items in it
        if (cbl.length) {
            // get all the table rows, and filter out all those which
            // doesn't contain a checked checkbox
            var cbElements = cbl.find('TR').filter(function(index, element) {  
                return $(this).find('input:checked').length;  
            });  
            // take each table row containing a checked checkbox and place it
            // at the top of your check-box-list element we called cbl
            cbElements.each(function() {  
                $(this).prependTo(cbl);  
            });  
        }  
    });

thats it, hope it got you where you needed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜