Search code examples
zk

how to create communication with dynamic text box


How to communicate between dynamic textbox that without id .

In normal case we can have some of the event name onchange and etc . but in dynamic component. We don't have that key (id) to link it. Like onchange$id then do something "".

so in dynamic component case , how to get value from dynamic component? Please provide idea.


Solution

  • since your question isn't very precise (about how you create those components dynamicall - java or zul templates) here the 2 basic methods:

    from java code:

    after creating a new component just add an event listener programmatically, no need for any convention based method names.

    //create dynamically anywhere
    Textbox someInput = new Textbox();
    someInput.setParent(someParent);
    someInput.addEventListener(Events.ON_CHANGE, this::onChangeSomeInput);
    //someInput.addEventListener(Events.ON_CHANGE, (InputEvent event) -> {...});
    ...
    
    public void onChangeSomeInput(InputEvent event) {
       Textbox someInput = event.getTarget();
       someInput.getValue();
    }
    

    from zul code:

    in this case event forwarding is the tool of choice, it lets you delegate the event to a parent component with an id

    <grid id="mygrid">
      <template name="model">
        <row>
          <textbox forward="onChange=mygrid.onChangeSomeInput(${each})"/>
        </row>
      </template>
    </grid>
    

    then you can implement a composer method like this

    public void onChangeSomeInput$mygrid(ForwardEvent forwardEvent) {
        InputEvent event = (InputEvent)forwardEvent.getOrigin();
        Object each = forwardEvent.getData();
    }
    

    (I wrote this code our of my head so please excuse typos/errors, I'll be happy to provide running examples in case you specify your question)