0

I'd like to save in some variable (menuSize) the size of my <p:panelMenu> which is surrounded by an <p:layoutUnit> element. I tried the following :

In my view (xhtml) :

<p:layoutUnit id="myMenuLayout" position="west" size="#{loginBean.menuSize}" resizable="true" header="Menu">
  <p:panelMenu id="myPanelMenu" widgetVar="myMenu" model="#{menuModelGenerator.menuModel}" />
</p:layoutUnit>

<p:resizable for="myMenuLayout" onResize="#{loginBean.keepMenuSize(event)}" />

In my controller :

public void keepMenuSize(ResizeEvent event) {
  if (event != null) {
  menuSize = event.getWidth();
} else {
  logger.info("event is null !!");
}

But my keepMenuSize method is not called when I resize the menu, and the value of the event still null when I refresh the page into the browser

What am I missing ?


EDIT :

I've succeed now to make keepMenuSizemethod called using the following :

<p:layoutUnit id="boToolMenuLayout" position="west" size="#{loginBean.menuSize}" resizable="true" header="Menu">
    <p:panelMenu id="boToolPanelMenu" widgetVar="boToolMenu" model="#{menuModelGenerator.menuModel}" />
</p:layoutUnit>
<p:ajax event="resize" listener="#{loginBean.keepMenuSize}" global="false" />

But the problem now is that my method is called twice, for example :

When I resize my menu into the UI, the logger I've implemented into keepMenuSize method says that the size value is 475px, then immediately after that says that the size value is 711px.

Then if I resize again, it says 864px then immediately 322px...etc.

Does anyone knows why ?

Thank you

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Sinda MOKADDEM
  • 796
  • 2
  • 12
  • 35
  • doesn't layout have its own resize ajax handler? – Kukeltje Mar 18 '16 at 10:04
  • @Kukeltje yes, actually, I tried to user the `onResize` attribute of `p:layoutUnit ` but it still doesn't work – Sinda MOKADDEM Mar 18 '16 at 10:07
  • "still doesn't work" is not smart. It is an end-user description, not a developer one. Do some more debugging... – Kukeltje Mar 18 '16 at 14:29
  • @Kukeltje "still doesn't work" mean that I already tried the solution you proposed before posting my question and my problem (aka the method is not called) persisted ! If you've read my question well you wouldn't have said that I'm not smart ! Thank you to contribute with constructive comments. – Sinda MOKADDEM Mar 18 '16 at 14:43
  • I'm not saying you are not smart;-) Sorry for the misunderstanding I meant it in the context of https://en.wikipedia.org/wiki/SMART_criteria. And 'method not called' is already 'smarter' but there are many related posts in StackOverflow about 'methods not being called' like http://stackoverflow.com/questions/2118656/commandlink-commandbutton-ajax-backing-bean-action-listener-method-not-invoked – Kukeltje Mar 18 '16 at 14:49
  • @Kukeltje Ok, but I already browsed stackoverflow for related questions/answers and none of them solved my issue. That's why I've decided to post a new question. this said, I've succeed to run the method using `` but something else is going wrong this time, because the method is now called twice. For example, if I resize my menu to 475px, it's first resized to 475px then to 711px automatically. I will edit my answer to include details... – Sinda MOKADDEM Mar 18 '16 at 14:59
  • See [ask] about the searching AND keeping track and mentioning in new questions... – Kukeltje Mar 18 '16 at 15:06
  • 1
    Sure very logical (I think)... Try adding up both numbers... Notice something (1186?) ? The eventhandler is on the layout, not a layout unit. So it receives one event of something getting smaller AND one event of the other part getting bigger... – Kukeltje Mar 18 '16 at 15:13
  • @Kukeltje, thank you for the remark. So, all what I have to do is to make the eventhandler only consider my layout unit, right ? maybe using `process` attribute ? – Sinda MOKADDEM Mar 18 '16 at 15:18
  • No, take the event in the eventhandler and from the event look up the component. You can then 'just' react on the relevant one... – Kukeltje Mar 18 '16 at 15:28
  • @Kukeltje All I can do using my `event` object and to fit your suggestion is to test for its class : `if (event.getSource() instanceof LayoutUnit)`, but in my case I have the other LayoutUnit elements so this control is not relevant.. – Sinda MOKADDEM Mar 18 '16 at 15:52
  • 1
    No... You can give the layout units an id and get that from the event and/or component to. – Kukeltje Mar 18 '16 at 16:38
  • OK, I finally succeeded using the following code in my bean : `LayoutUnit layoutUnit = (LayoutUnit) event.getComponent();` then `if ("west".equals(layoutUnit.getPosition())) {`. Thank you @Kukeltje – Sinda MOKADDEM Mar 18 '16 at 16:50
  • And you can do `((LayoutUnit)event.getComponent()).getPosition()` (see [PrimeFaces Showcase source](https://github.com/primefaces/showcase/blob/master/src/main/java/org/primefaces/showcase/view/panel/LayoutView.java)) – Kukeltje Mar 18 '16 at 16:51
  • I'll write up an answer about this... or do you want to create a full nice one yourself?. Using the source helps... the PF source and the showcase source. – Kukeltje Mar 18 '16 at 16:52
  • @Kukeltje Yes, both codes are approximately the same in my opinion :) For the answer, it's as you prefer ... If you write an answer exposing this solution, I'll validate it, otherwise I'll write my own, no problem :) Thank you very much for your time today – Sinda MOKADDEM Mar 18 '16 at 17:12

0 Answers0