views:

196

answers:

1

Hi everyone,

I recently upgraded jQuery to 1.4.2 and jQuery-UI to 1.8.2. Mistake or not, I started pulling my hair when I tried combining the new ui-autocomplete (not the old one from bassistance.de) with jeditable.

When I edit a field in my page (usually a span with a unique ID), user starts typing the name of someone and I manage to display the label name+last name in the input field generated by jeditable. My problem is that some contacts in my DB have same name+last name. So sending the result "name+last name" will not allow me to get the right contact. Instead I need to send the ID associated with that contact.

Has anyone managed to create a new custom input involving the latest ui-autocomplete where the ID would be stored in a hidden input then posted to the processing script while the label is displayed in the visible input field ?

Additional question: can anyone confirm that it is absolutely not possible to use ui-autocomplete where the label is displayed in the input field and the id stored in the value of the same input ? A hidden input is always necessary right ?

Many thanks in advance

+1  A: 

After several days of trying (my javascript skills being quite bad), I managed to get something to work using Pete Freitag's code (see comment above).

The code is probably ugly (that's why I submit it to you guys, any constructive comment is welcome).

 $.editable.addInputType('autocompleteCON', {
        element : $.editable.types.text.element,
        plugin : function(settings, original) {
            $('input', this).each(function() {
                /* change name of original input */
                $(this).attr('name', 'cellvalue_autocomplete_label');
                $(this).attr('id','parentInput');
                /* create new hidden input with name of orig input */
                $(this).after('<input type="hidden" name="cellvalue" id="cellvalue_autocomplete_hidden" />');
                $(this).autocomplete({
                    source: "tab_autocomplete.php?cat=CON",
                    change: function(event, ui) {
                        if (!ui.item) {
                            $(this).val('');
                            $('#cellvalue_autocomplete_hidden').val('');
                            return false;
                        }
                    },
                    select: function(event, ui) {
                        $('#cellvalue_autocomplete_hidden').val(ui.item.value);
                        $('#parentInput').val(ui.item.label);
                        return false;
                    }
                });
            //  alert(result);
            }).data( "autocomplete" )._renderItem = function( ul, item ) {
                    return $( "<li></li>" )
                        .data( "item.autocomplete", item )
                        .append( "<a>" + item.label + " <span style='font-size:0.8em;font-weight:normal;max-width:650px;'>(" + item.desc + ")</span></a>" )
                        .appendTo( ul );
            };
        },
        submit: function (settings, original) {
            var value = $('#cellvalue_autocomplete_hidden').val();
            $('input', this).val(value);
        }
}); 

$(".autocompleteCON_con").editable("update.php", {
    id          : 'cellid',
    name        : 'cellvalue',
    event       : 'dblclick',
    submitdata  : {id: "<?php echo $id_contact; ?>"},
    type        : 'autocompleteCON',
    indicator   : 'Saving...',
    select      : true,
    tooltip     : 'Double click to edit...',
    placeholder : '<b style="color:#AAA">Edit</b>',
    style       : 'display: inline',
    height      : '25px',
    width       : '150px',
    onblur      : 'ignore',
    submit      : 'Save',
    cancel      : 'Cancel',
    callback    : function(value, settings) {
        // some function
            }   
});
Jonas