/**
*    @Author: Arash Karimzadeh
*    @Email:
*    @Desciption: Here, I post some of my recent
*    researches. Also you can see my code projects.
*/  
Ajax C # Chrome CMS dateNet Design Patterns includeMany JavaScript jBind Joomla jQuery Plugin rails ror RubyOnRails SQLite xul

+ All tags

Content View Hits : 345234
Bookmark and Share
How to Extend jQuery Editable PDF Print
Written by Arash Karimzadeh   
Tuesday, 13 January 2009 16:26

We can extended jQuery Editable plugin by extending its $.editableFactory. currently it supports text, password, textarea and drop-down. Let's see how we can extend it to support checkbox.

You can download the lastest version here and follow these steps to understand how we can extend it.
At this time jQuery Editable 1.1.0 is released and we are using it during our development. I am sure when you are reading this article newer versions of Editable are released and this sample must be compatible with those too.

What is $.editableFactory ?

$.editableFactory is a factory to provide the two functionalities. One for converting the the elements (elements which you've got and converted to editable by $.('selector').editable() ) content to input fields and another for retrieving data from inputs on submit.

the structure of $.editableFactory is


              
  1. $.editableFactory = {
  2. 'text': {
  3. toEditable: function($this,options){
  4. $this.append('data('editable.current')+'"/>');
  5. },
  6. getValue: function($this,options){
  7. return $this.children().val();
  8. }
  9. },
  10. 'password': {
  11. toEditable: function($this,options){
  12. $this.append('data('editable.current')+'"/>');
  13. },
  14. getValue: function($this,options){
  15. return $this.children().val();
  16. }
  17. },
  18. .....
  19. }

              
 

              
            

As you see first you define a type (such as password) which you will pass it to editable later as options.type.


              
  1. $('div').editable({type:'password'});

              
 

              
            

There are two functions defined for each type:

  1. toEditable: which will convert element content to input(s)
  2. getValue: for retrieving value(s) from input(s)

How to create checkbox for Editable?

  1. How to convert to content to checkbox inputs?
    
                      
    1. toEditable: function($this,options){
    2. $.each( options.options,
    3. function(){
    4. $('').appendTo($this)
    5. .val(this)
    6. .after(''+this+'');
    7. }
    8. )
    9. }
    
                      
     
    
                      
                    

    Let's discuss the above code:
    • options contains all parameters you pass to editable such as
      $('div').editable({ type:'password',onSubmit:submitMethod });
      options has options inside which we use to pass anything such as:
      $('div').editable({ type:'password',onSubmit:submitMethod,options:{'val1':'text1','val2':'text2','val3':'text3'} });
    • We iterate over options and create , set values for them and also append a span tag to show the value beside the checkbox.
    Your $.editableFactory would be like this:
    
                      
    1. $.editableFactory = {
    2. 'textarea': { .... },
    3. 'password': { .... },
    4. 'checkbox': {
    5. toEditable: function($this,options){
    6. $.each( options.options,
    7. function(){
    8. $('').appendTo($this)
    9. .val(this)
    10. .after(''+this+'');
    11. }
    12. )
    13. }
    14. }
    15. }
    
                      
     
    
                      
                    

    You can change the source code of jQuery Editable and paste the 'checkbox' code, we have developed, inside the $.editableFactory.
    Now create a div tag (with text val1,val2) and try something like this and test your code:
    $('div').editable({type:'checkbox',options:{'val1':'val1','val2':'val2','val3':'val3'});
    As you see your text will change to checkboxes whenever you click on it. But it will raise error on submit.(that's ok for now!)
  2. How to check the selected values?
    
                      
    1. var currentValues = $this.data('editable.current').split(',');
    2. $this.children(':checkbox').each(
    3. function(){
    4. if(currentValues.indexOf($(this).val())>-1)
    5. $(this).attr('checked', 'checked');
    6. }
    7. )
    
                      
     
    
                      
                    

    • Append this code inside your toEditable.
    • $this.data('editable.current') is useful for getting the real content of the element which now is replaced b inputs. it will return val1,val2.
    • The first line will convert your val1,val2 text to an array.
    • Then it will check each checkbox if the value exist in the array so it must be checked by default.
    Your code must be something like this:
    
                      
    1. $.editableFactory = {
    2. 'textarea': { .... },
    3. 'password': { .... },
    4. 'checkbox': {
    5. toEditable: function($this,options){
    6. $.each( options.options,
    7. function(){
    8. $('').appendTo($this)
    9. .val(this)
    10. .after(''+this+'');
    11. }
    12. )
    13. var currentValues = $this.data('editable.current').split(',');
    14. $this.children(':checkbox').each(
    15. function(){
    16. if(currentValues.indexOf($(this).val())>-1)
    17. $(this).attr('checked', 'checked');
    18. }
    19. )
    20. }
    21. }
    22. }
    
                      
     
    
                      
                    

    This will check the default selected values But still has error on submit.( that's ok for now! ;) )
  3. How to retrieve checked values for submission?
    The error was raised when you tried to submit the changes. Because the Editable tried to call getValue method of 'checkbox' which have not defined yet.
    
                      
    1. getValue: function($this,options){
    2. var items = [];
    3. $(':checkbox', $this).each(
    4. function(){
    5. if($(this).attr('checked'))
    6. items.push($(this).val());
    7. }
    8. )
    9. return items.join(',');
    10. }
    
                      
     
    
                      
                    

    • This code will traverse in checkboxes and find the which are checked and retrieve all as a string separated by ','
    Finally your code must be like this:
    
                      
    1. 'checkbox': {
    2. toEditable: function($this,options){
    3. $.each( options.options,
    4. function(){
    5. $('').appendTo($this)
    6. .val(this)
    7. .after(''+this+'');
    8. }
    9. )
    10. var currentValues = $this.data('editable.current').split(',');
    11. $this.children(':checkbox').each(
    12. function(){
    13. if(currentValues.indexOf($(this).val())>-1)
    14. $(this).attr('checked', 'checked');
    15. }
    16. )
    17. },
    18. getValue: function($this,options){
    19. var items = [];
    20. $(':checkbox', $this).each(
    21. function(){
    22. if($(this).attr('checked'))
    23. items.push($(this).val());
    24. }
    25. )
    26. return items.join(',');
    27. }
    28. }
    
                      
     
    
                      
                    

    Or you can download the file from here.

Tags: JavaScript | jQuery | Plugin

Last Updated on Monday, 27 September 2010 12:45