Search code examples
jquerygrailspostbackgspclonenode

two cloned select box not loaded if i remove alert


enter image description here

I have couple of issues- the first constraint on select is loaded when reference table value selected.

ISSUE 1 - The last two select box name -Constraint on should have cloned option from first constraint on.but should not have the selected option in previous select box. How to do it?

ISSUE 2 - My select box is loaded on javascript call but i have to add a alert for this,why? Code for population two select -

function LoadSelects()
{
 $("#columns2 option").each(function() {
        $(this).remove();
    });
 $("#columns3 option").each(function() {
        $(this).remove();
    });
   alert("i m called");  ///it does not load the selects if i remove this****why?
 $("#columns option").clone().appendTo("#columns2");
  $("#columns option").clone().appendTo("#columns3");
 }  
}

Controller code for population first select-

 def getColumns = {
    def columns = GGWSchemaXref.executeQuery("select distinct p.columnname     from GGWSchemaXref p where p.tablename=:table and p.dbname = 'IVR_GUARDIAN'",[table:params.tableCombo])
    render(template:"selectTemplate", model: [ columnList: columns ])

}

GSP page code here-

  <tr> <td>Reference Table:</td>
   <td><g:select name="tableCombo"
        noSelection="${['':message(code:'Select Table')]}"
        from="${result}" value="${tableName }"        onchange="${remoteFunction(action:'getColumns', update:'columns', params:'\'tableCombo=\'    + this.value', OnComplete = 'LoadSelects();')}"/> </td></tr>
 <tr id ="cons"><td nowrap>Constraint On:</td>
    <td nowrap><g:select name="columns" from="[]" /></td>
    <td nowrap>Constraint Value:</td>
    <td nowrap><g:textField name="columnValue" value="${enterVal }" />  
</tr>
 <tr id ="cons2"><td nowrap>Constraint On:</td>
    <td nowrap><g:select name="columns2" from="[]" /></td>
    <td nowrap>Constraint Value:</td>
    <td nowrap><g:textField name="columnValue2" value="${enterVal2 }" />  
</tr> 
 <tr id ="cons3"><td nowrap>Constraint On:</td>
    <td nowrap><g:select name="columns3" from="[]" /></td>
    <td nowrap>Constraint Value:</td>
    <td nowrap><g:textField name="columnValue3" value="${enterVal3 }" />  
</tr>     

ISSUE 3- I need to keep the selected and entered values after search (post back).

Please help how to do it?

thanks


Solution

  • The first "A" in AJAX is for "asynchronous". You need to execute LoadSelects() in the success callback of your ajax

    You can simplify the function to:

    function LoadSelects(){
    
        $("#columns2, #columns3").html( $("#columns").html()).val('') ;
    
     }
    

    This will copy the options from #columns to both of the other selects and unselect both which I believe is what you are asking.

    Using $('select').val('') as in code above will remove selected property from options

    Edit If the each you are using for your selects is due to have more than one with same ID you have a problem since ID's must be unique. If this is the case, code will need to be refactored to a traverse within a row

    EDIT #2 AJAX Success

    jQuery.ajax({
        type: 'POST',
        data: 'tableCombo=' + this.value,
        url: '/GryphonMonitor/load/getColumns',
    
        success: function(data, textStatus) {‌
    
            /*  this is callback when all data has been received*/      
    
            jQuery('#columns').html(data);
             LoadSelects();
        },
        error: function(XMLHttpRequest, textStatus, ‌errorThrown) {}
    })