Search code examples
javascriptjqueryajaxjeditableautogrow

Problems using jeditable and autogrow


I work on a Webproject using jQuery and CakePHP. I use jeditable as an inplace edit plugin. For textareas I extend it using the autogrow plugin.

Well, I have two problems with this:

  • First, autogrow does only work on Firefox, not on IE, Safari, Opera and Chrome.
  • Second, I need a callback event for jeditable, when its finished showing the edit-component, to recalculate the scrollbar

Im not so familiar with Javascript, so i can't extend/correct this two libraries by my own. Has anyone used another js-library for inplace edit with auto growing textareas (no complete editors like TinyMCE, I need a solution for plain text)?

I also found Growfield, it would work for other browsers, but there's no jeditable integration...

(sorry for my english)


Solution

  • I didn't see any problems using Autogrow with jeditable in any browsers but here is an implementation of Growfield with jeditable. It works much in the same way that the Autogrow plugin for jeditable does. You create a special input type for jeditable and just apply .growfield() to it. The necessary javascript is below, a demo can be found here.

    <script type="text/javascript">
    /*  This is the growfield integration into jeditable
        You can use almost any field plugin you'd like if you create an input type for it.
        It just needs the "element" function (to create the editable field) and the "plugin"
        function which applies the effect to the field.  This is very close to the code in the
        jquery.jeditable.autogrow.js input type that comes with jeditable.
     */
    $.editable.addInputType('growfield', {
        element : function(settings, original) {
            var textarea = $('<textarea>');
            if (settings.rows) {
                textarea.attr('rows', settings.rows);
            } else {
                textarea.height(settings.height);
            }
            if (settings.cols) {
                textarea.attr('cols', settings.cols);
            } else {
                textarea.width(settings.width);
            }
            // will execute when textarea is rendered
            textarea.ready(function() {
                // implement your scroll pane code here
            });
            $(this).append(textarea);
            return(textarea);
        },
        plugin : function(settings, original) {
            // applies the growfield effect to the in-place edit field
            $('textarea', this).growfield(settings.growfield);
        }
    });
    
    /* jeditable initialization */
    $(function() {
        $('.editable_textarea').editable('postto.html', {
            type: "growfield", // tells jeditable to use your growfield input type from above
            submit: 'OK', // this and below are optional
            tooltip: "Click to edit...",
            onblur: "ignore",
            growfield: { } // use this to pass options to the growfield that gets created
        });
    })