1

I have implemented Struts2-jQuery <sj:select/>.

This select box is populated fetching a JSON object coming from an AJAX call.

How can I access this JSON object before the loading of the SelectBox in javascript in order to do some processing on it?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Man
  • 67
  • 1
  • 11

1 Answers1

1

The Struts2-jQuery-plugin's SelectTag has an attribute for that:

onBeforeTopic : Topics that are published before a load

Example of usage:

<script type="text/javascript">
    $(function(){
        $.subscribe("myBeforeHandler", function(event, data) {
            alert("I'm raised before the loading of the SelectBox ! Data is : " + data);
        });
    });
</script>

<s:url var="myUrl" action="foobar" namespace="/" />

<sj:select onBeforeTopics = "myBeforeHandler"
                     href = "%{myUrl}"
                     list = "foo"
                     name = "bar" />
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thank you very much for your reply. 'myBeforeHandler' function is getting called before load. I have one doubt in it. Can I iterate through this data i.e. the json object which is used to fill combobox. I checked it. It is coming as HTMLOptionsElement. I was not able to use it then. And also I havent checked it too much as I was busy in other stuff. Can you please suggest me some way to use it. Thanks – Man Feb 26 '15 at 10:06
  • I'm pretty sure you can iterate that data (try looking [here](http://stackoverflow.com/a/2950163/1654265)); BTW since it is a different problem, to get a better help it should be asked as a new question, including the details of the data returned. Also @Man please consider upvoting the answer (by pressing the arrow up) if you liked it. Thank you – Andrea Ligios Feb 26 '15 at 10:15
  • @Man No you can't, because the data is not available yet. – Roman C Feb 26 '15 at 14:19
  • @Man, if onBeforeTopics is fired before of the AJAX call, instead that before the population (both can be defined *load*), the use onCompleteTopics, that calls your handlers after the AJAX data is returned. – Andrea Ligios Feb 26 '15 at 15:49