0

Open url inside a grid in a new browser window

asked 2011-04-29 14:06:33 +0800

Bera gravatar image Bera
121 1 3

Hi

I have a list of urls in a grid, And I need that when a user click in a url a new browser windows open with the same url

I read some threads but in my case I believe my situation is a little different. In my controller
I'm using the following code

UrlListCollection.generateListUrl();

dataGrid.setRowRenderer(new RowRenderer() {
public void render(Row row, Object data) throws Exception {
UrlObj url = (UrlObj) data;
row.getChildren().add(new Label("Some data"));
row.getChildren().add(new Toolbarbutton(url.getUrlApp())); // url.getUrlApp() will be return a link like http://www.google.com
}
});

In my view(zul) I have

<grid id="dataGrid" width="100%">
<columns>
<column label="Some Data" sort="auto(FIELD_NAME)" width="200px" />
<column label="URL LINK" sort="auto(URL)" width="630px" />
</columns>
</grid>

But the common way to set an event an component in java can be:

Toolbarbutton button = new Toolbarbutton(url.getUrlApp()));
button.addEventListener(Events.ON_CLICK, new EventListener() {
public void onEvent(evt) {

// what I put here to open a Link in another web browser window ????
// and I need to be able to open every object value retrieved by url.getUrlApp() ???
}
});


I don't now what is necessary to make my code works..for me the way to apply a Event to toolbar button inside a grid
that use RowRenderer method is strange. I can't see a solution by myself.

Thx for help me

Best

delete flag offensive retag edit

10 Replies

Sort by ยป oldest newest

answered 2011-04-29 14:22:21 +0800

mjablonski gravatar image mjablonski
1284 3 5
http://www.jease.org/

Hi,

you can use a Toolbarbutton (or any other button like the A-Tag) in the following way:

Toolbarbutton tb = new Toolbarbutton("Click me");
tb.setHref("http://www.zkoss.org/");
tb.setTarget("_blank");

HTH, Maik

link publish delete flag offensive edit

answered 2011-04-29 14:38:08 +0800

Bera gravatar image Bera
121 1 3

But in my example is not possible to use your code because I create the the toolbarbuttons inside a grid dinamically

I also tried to use the code bellow but doesn't work neither


java.lang.Boolean cannot be cast to org.zkoss.zk.ui.Component

dataGrid.setRowRenderer(new RowRenderer() {
public void render(Row row, Object data) throws Exception {
UrlObj url = (UrlObj) data;
row.getChildren().add(new Label("Some data"));
row.getChildren().add(new Toolbarbutton(url.getUrlApp())
.addEventListener(Events.ON_CLICK, new EventListener() {
public void onEvent(Event evt) {
Executions.getCurrent().sendRedirect(app.getEnvApp(), "_blank");
Executions.getCurrent().sendRedirect(self.getAttribute("label").toString(), "_blank");

}
})


);

I got a error message:


java.lang.Boolean cannot be cast to org.zkoss.zk.ui.Component

Some other way?

link publish delete flag offensive edit

answered 2011-04-29 14:51:55 +0800

mjablonski gravatar image mjablonski
1284 3 5
http://www.jease.org/

Hi,

why can't you use it in the following way?

public void render(Row row, Object data) throws Exception {
  UrlObj url = (UrlObj) data;

  Toolbarbutton tb = new Toolbarbutton(url.getUrlApp());
  tb.setHref(url.getUrlApp());
  tb.setTarget("_blank");

  row.getChildren().add(new Label("Some data"));
  row.getChildren().add(tb);
}
});

Cheers, Maik

link publish delete flag offensive edit

answered 2011-05-02 14:39:03 +0800

Bera gravatar image Bera
121 1 3

Thanks Maik!

Works fine form me now using your approach!

Best!

link publish delete flag offensive edit

answered 2018-10-31 00:15:37 +0800

Narayanan gravatar image Narayanan
1

Hi Maik,

Could you provide me the steps to open the window with external url. When i use the below onclick method it is opening in new tab, but i am expecting it to be open in a window like popup. onClick='execution.sendRedirect("http://zkoss.org", "_blank")'

link publish delete flag offensive edit

answered 2018-10-31 15:21:34 +0800

cor3000 gravatar image cor3000
6280 2 7

Opening a popup window requires some JS code so a client side widget-event listener w:onClick should do that (provided the user's browser(/settings) allow that):

<zk xmlns:w="client">
  ...
  <button label="open example.org in a popup"
          w:onClick="window.open('http://www.example.org/', '_blank', 'scrollbars');"/>
  ...
</zk>

The equivalent java code is:

button.setWidgetListener("onClick", "window.open('http://www.example.org/', '_blank', 'scrollbars');");

One might be tempted to trigger the JS from the server side directly via Clients.evalJavaScript(...) however browsers will consider this an untrusted event and block the popup under most conditions - Especially when external URLs are opened.

link publish delete flag offensive edit

answered 2018-11-11 04:06:12 +0800

mattyp gravatar image mattyp
1

I am not able to set the URL in . How should I make changes in the back-end ??

link publish delete flag offensive edit

answered 2018-11-12 09:34:01 +0800

cor3000 gravatar image cor3000
6280 2 7

updated 2018-11-12 09:35:23 +0800

It seems your first sentence was truncated somehow. Please post again and make sure nothing is missing.

link publish delete flag offensive edit

answered 2018-11-13 01:34:55 +0800

Narayanan gravatar image Narayanan
1

Hi Maik,

Am trying to call below javascript method from Clients.evalJavaScript() and its not executing. Could you please provide the steps to load the javascript and execute it by using the Clients. Is there any way get the return value.

function getData() { var url="";//external site url newwindow=window.open(url,'name','height=200,width=150'); if (window.focus) {newwindow.focus()} return false; }

link publish delete flag offensive edit

answered 2018-11-13 11:32:03 +0800

cor3000 gravatar image cor3000
6280 2 7

As I explained before, trying to open a POPUP from an AJAX response (which is a ZK server side listener) will run into the browsers built in popup blocker. Because a server side response is not a trusted event. (please read on un/trusted events) the whole advertisement branche broke the web with unsolicited popups which are now commonly blocked by browsers. However a direct listener to an onClick listener AT CLIENT side creates a trusted event which allows you to open a Popup.

Here a zkfiddle showing the difference between a client side and server side listener. In both cases the JS is executed. When getData() is called from the server side event listener the browser will prevent the Popup unless you change your popup settings. Chrome will just ignore the popup whild FF will inform you about the blocked popup.

http://zkfiddle.org/sample/323lscb/1-compare-popup-from-client-or-server

So you CAN NOT use Clients.evalJavaScript to open a popup, only a browser tab. You have to add custom client side event listener.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
2 followers

RSS

Stats

Asked: 2011-04-29 14:06:33 +0800

Seen: 895 times

Last updated: Nov 13 '18

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More