JQuery Mobile Custom Filter - Checkboxes

时光毁灭记忆、已成空白 提交于 2019-12-12 03:19:06

问题


I'm trying to filter a list of check box items, I want any checked items to be included in the list regardless if they satisfy the search criteria. My idea was to use a custom search and to add a '~' to the front of the data-filtertext attribute of any checked items, then in my search function return true if the '~' is there or if the regular criteria is met. First the code below has huge problems in addition to it not working, note the ! on the test for checked - the status hasn't updated at the point it's being checked, I'd love it if someone could help me make the jquery right. Second, how do I set the data-filtertext properly? Third, thoughts on if any of this is going to work?

$('#my_id').find('.ui-btn').on('click', function(){  
         var id = this.htmlFor;  
         var checked = ! $('#' + id).is(':checked');
         if (checked)
         {
           var filter_text = '~' + this.innerHTML;
           this.attributes['data-filtertext'] = filter_text;
         }
         else
         {
           this.attributes['data-filtertext'] = this.innerHTML;
         }     
      });

回答1:


You don't need to alter the filter text attribute. Instead use the filterCallback event of the filterable widget to see if the item is checked or contains the text.

So given this list:

<form>    
    <input data-type="search" id="filterControlgroup-input">
</form>
<div id="myList" data-role="controlgroup" data-filter="true" data-input="#filterControlgroup-input">
    <input type="checkbox" name="checkbox-v-2a" id="checkbox-v-2a" data-filtertext="seven">
    <label for="checkbox-v-2a" >One</label>
    <input type="checkbox" name="checkbox-v-2b" id="checkbox-v-2b">
    <label for="checkbox-v-2b">Two</label>
    <input type="checkbox" name="checkbox-v-2c" id="checkbox-v-2c">
    <label for="checkbox-v-2c">Three</label>
</div>

Attach the callback event. If the checkbox is checked, return false (don't filter out) otherwise check both the text and data-filtertext to see if the item meets the filter criteria:

$(document).on("pagecreate", "#page1", function () {
    $("#myList").filterable('option', 'filterCallback', checkedOrMatch);
});

function checkedOrMatch(idx, searchValue) {
    var ret = false;
    var $chkbox = $(this).find('input[type="checkbox"]')
    var IsChecked = $chkbox.is(':checked');
    if (IsChecked) {
        //always show checked items in list regardless of filter criteria
        ret = false;
    } else if (searchValue && searchValue.length > 0) {
        searchValue = searchValue.toLowerCase();
        var text = $(this).text().toLowerCase();
        var filttext = $chkbox.data("filtertext") || '';
        filttext = filttext.toLowerCase();
        //if neither text nor filtertext contain searchvalue, return true to filter out
        if (text.indexOf(searchValue) < 0 && filttext.indexOf(searchValue) < 0) {
                ret = true; //filter this one out
        }        
    }    
    return ret;
}

DEMO



来源:https://stackoverflow.com/questions/32524891/jquery-mobile-custom-filter-checkboxes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!