Click here to Skip to main content
Click here to Skip to main content

Checkbox List With Filtering jQuery Widget

By , 18 Sep 2012
 

Introduction

We used checkbox-list controls back at the good old days for desktop applications. This is a lightweight implementation of checkbox list control as a jQuery UI widget.

Background

First of all, we need to populate the listbox with data. I used a hard-coded JSON data model. Each item has a text property and a value property.

var dataModel = [
      {text: 'checkbox-1 caption', value:'1'}
]; 

And selection data returned within the same data model.

Using the Code

checkList widget can be applied easily with a div element. 

<div id='myCheckList'></div> 
$('myCheckList').checkList({
      listItems: dataModel,
      onChange: selChange
});   

We can set the listbox items on creation by passing the listItems parameter or after widget created set data model manually by calling setData method. Also as you see, we have an onChange event. Event is fired whenever any item's check state is changed. In the demo application, I used this event to display the selected elements.

We can simply get the selected elements by calling getSelection method. This returns the same data model we use to set the listbox items.

function selChange(){ 
      var selection = $('#myCheckList').checkList('getSelection'); 
      $('#selectedItems').text(JSON.stringify(selection)); 
}     

Filtering

Filtering capability is implemented without any Ajax calls. It's simple, we check every listitem in the listbox if it contains the filter string. Then we show the matched items and hide the unmatched ones. While showing and hiding listbox items, we can apply some jQuery effects. The default effect is defined as blink in the options.

Have fun.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Evren Yortuçboylu
Software Developer (Senior)
Turkey Turkey
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
BugBug on chromememberreminus1 May '13 - 1:28 
On Chrome you must change two lines in the file jquery.ui.checklist.js:

var state = $(this).attr('checked');
o.objTable.find('.chk:visible').attr('checked', setState);

into

var state = $(this).prop('checked');
o.objTable.find('.chk:visible').prop('checked', setState);
 
Solved
QuestionselChange event fires for each item in the listmemberdunwan17 Oct '12 - 7:18 
I really dig the simplicity of your Checkbox list. Well done!
 
I noticed that when a single item is selected from the list the selChange event fires for each item in the list which can cause a performance hit for large lists.
 
Is there any way to prevent this?
AnswerRe: selChange event fires for each item in the listmemberholmm12 Nov '12 - 1:01 
did you have any luck preventing this?
AnswerRe: selChange event fires for each item in the listmemberSXP22 Nov '12 - 21:12 
Here is a fix for onChange event.
 
1. open jquery.ui.checkList.js
2. find this line - el.delegate('.chk','click', function(){self._selChange()});
3. replace it with - el.delegate('#'+itemId,'click', function(){self._selChange()});
4. problem solved Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 18 Sep 2012
Article Copyright 2012 by Evren Yortuçboylu
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid