views:

456

answers:

2

Hi all, Let's say I have sj:autocompleter like this :

<sj:autocompleter  id="idDistributor" 
name="idDistributor" 
list="%{idDistributorList}"  label="ID
Distributor"  indicator="indicator" />

when the value is changed, I'd like to pass the detail value to other text field like this :

<sj:textfield label="Nama Distributor" id="namaDistributor" name="namaDistributor" required="true"/>

where its value will be retrieved from struts back bean (FormAction).

How I could do this?

Really thanks in advance.

Mr.K

A: 

The following will send the autocompleter value to your action and set the value of #namaDistributor with the returned string:

$('#idDistributor').change(function(){
    // Make the ajax request
    $.ajax({
        url: 'path/to/back-bean.action',
        data: "autocompleterValue=" + $(this).val(),
        dataType: "json",
        success: function(data) {    
            // Set the inputs from the json object            
            $('#namaDistributor').val(data.distributor);
            $('#namaPrice').val(data.price);
        }
    });
});

You can read more about the $.ajax() method here. The above json example assumes that your Action is creating a json object with the following format:

{"price": "123.40", "distributor": "XYZ Inc."} 

You can read more about using json with Struts2 in this article.

Pat
Hi Pat, I'd like to pass the result value from back bean to #namaDistributor not directly from idDistributor value, how can I do this?Thanks
Mr.K
Not a problem. I've updated the code to show how you'd update `#namaDistributor` from the result of the ajax call.
Pat
Awesome, it works...with little bit modification on back bean for returning the StreamResult not String. Pat, sorry for further question, if I want to modify multiple textfield after autocompleter onchange, how can I do this?, on the other word I need to return two value for two different field.Thanks.
Mr.K
View my updates (again :) to see how you would do this using json.
Pat
awesome..awesome it works using json!!!, actually I'm quite new on JQuery, really thanks for your support.Mr.K
Mr.K
Glad to hear you got it sorted out. Once you get past the initial learning curve, jQuery is pretty easy to use.
Pat
+1  A: 

you can do this with onSelectTopics.

Subscribe a topic like this:

<script>
$.subscribe('autocompleteSelect', function(event, data) {
  var ui = event.originalEvent.ui;
  $('#namaDistributor').val(ui.item.value);
});
</script>

and at it to your autocompleter

<sj:autocompleter  id="idDistributor" 
name="idDistributor" onSelectTopics="autocompleteSelect"
list="%{idDistributorList}"  label="ID
Distributor"  indicator="indicator" />

you can find an example for this in the Struts2 jQuery Plugin Showcase

Mark
thanks for the insight.
Mr.K