ZK - Invoking ZScript function of an included sub page - function

Say, I have a zul page (page1.zul) like so:
<zk>
<textbox id="textbox1" ></textbox>
<button label="Display" onClick="display()" ></button>
<include id="include1" ></include>
<zscript>
display() {
include1.setSrc("page2.zul");
java.lang.Class[] argTypes = new java.lang.Class[]{String.class};
org.zkoss.xel.Function fn = include1.getChildPage().getZScriptFunction("doDisplay", argTypes);
fn.invoke(null, textbox1.value);
}
</zscript>
</zk>
But, I get the error - "Attempt to invoke method getZScriptFunction on null value". So, include1.getChildPage() is returning a null value i.e. I am not able to retrieve "page2" using getChildPage() and I am not sure how to go about it.
My second page is shown below:(page2.zul)
<zk>
<label id="label1" ></label>
<zscript>
doDisplay(String value) {
label1.setValue(value);
}
</zscript>
</zk>
If I enter something in the textbox and click the "Display" button, I want to set the value of label in a different page(i.e page2) to the value in the textbox. The idea is to pass value of a component from one page to a zscript function of another included page.

You can do this instead of Passing value.
in Page2.zul
<zk>
<label id="label1" ></label>
<zscript>
doDisplay(String value) {
Textbox textbox=(Textbox)((Include)label1.getSpaceOwner()).getSpaceOwner().getFellowIfAny("textbox1");
label1.setValue(textbox.getValue());
}
</zscript>
</zk>

You can change file1 as follows:
<zk>
<textbox id="textbox1" ></textbox>
<button label="Display" onClick="display()" ></button>
<!-- <include id="include1" ></include> -->
<div id="include"></div>
<zscript>
display() {
include.appendChild(Executions.createComponents("page2.zul", include, null));
}
</zscript>
</zk>

My suggestion is to use EventQueue instead to prevent the coupling of the two zul file.
More details, please reference to the sample code.
http://zkfiddle.org/sample/379s7ev/3-A-sample-for-using-Event-queue-to-talk-with-other-include

Related

Spring MVC - how to bind HTML checkbox value in a boolean variable

I am very new to spring mvc world. I am trying to send boolean value to from html form checkbox. When a user check the checkbox then it will send true, false otherwise.
<form class="attendanceBook" role="form" method="post" action="/attendances">
<div class="row">
<div class="form-group">
<div class="col-xs-4">
<label class="control-label">Check Here</label>
</div>
<div class="col-xs-4">
<input type="checkbox" name="i" id="i" value="true" />
</div>
<div class="col-xs-4">
<input type="submit" value="Click"/>
</div>
</div>
</div>
</form>
After some googilng I have found this so post, where it said standard behaviour is the value is only sent if the checkbox is checked. So what I have understand that is if the checkbox checked then the form will submit with the value of checkbox, otherwise it will not submit. When there is unchecked checkbox the initialization value in data class will be effective.
But in my case every time I am submitting the form it submitting true.
here is my rest controller for the bind html form submit.
#RestController
#RequestMapping("attendances")
class AttendanceRestController {
val logger = getLogger(AttendanceRestController::class.java)
#PostMapping
fun patchAttendance(#RequestBody attendanceJson: AttendanceJson): ResponseEntity<*> {
logger.info("attendanceJson {}", attendanceJson)
return responseOK(attendanceJson)
}
}
the data class(I am using kotlin)
data class AttendanceJson (
var i: Boolean = false,
var t: String = ""
)
So what will be the method to bind boolean data from a form submission with checkbox. I am also using Thymeleaf. Thanks in advance.
I'm working in Struts and don't know much about Spring. But I faced a similar situation.
What I did was I binded the checkbox with a boolean property in my From class. So for each checkbox, one boolean variable. And at the time of submitting in front end, I'll call a JS function code is below
function verifyCheckboxes() {
document.getElementById("researchPaper").value = document.getElementById("researchPaper").checked;
document.getElementById("researchPaperSeminarProceed").value = document.getElementById("researchPaperSeminarProceed").checked;
document.getElementById("extraActivities").value = document.getElementById("extraActivities").checked;
document.getElementById("studentAchivements").value = document.getElementById("studentAchivements").checked;
}
Here you can see I'm just assigning the value of checked property of that Checkbox just before submitting. It will be either true or false.
You should remove 'value' attribute from the input. If you want the checkbox checked when loading the page, add 'checked' attribute not 'value'.
Replace the input line with this:
<input type="checkbox" name="i" id="i" checked="checked"/>
This is the reason why you always get 'true' in code behind.
It's a bit of a hack, but if you change the type of the input tag from 'checkbox' to 'text' just before the form is posted, you will receive the value, whether it is checked or unchecked.
If you use jQuery:
$("input:checkbox").each(function(){this.type='text'})

Conditionally calling only some of the custom tags on a page when a form is submitted

I'm trying to make a page where a user can select a method to execute and input arguments for it.
<form action="thisPage.jsp" method="post">
<h2>Select method to test:</h2>
<select name = 'methods' onchange='this.form.submit()'>
<custom:inputs selectedMethod="<%=request.getParameter(\"methods\")%>" />
<input type="submit" name="go" value="OK" />
</form>
<br /> Output:
<br />
<custom:hello arg="<%=request.getParameter(\"generatedInput\")%>" />
Currently I have two custom tags. The first tag, inputs, dynamically creates inputs for the user to fill, depending on the method chosen. The second tag takes the values of those inputs as an argument and displays output.
The issue is that when the form is submitted, both tags run. The tag responsible for the output shouldn't be called when the user is changing the currently selected method, or at least it should be able to know that the call was caused by method selection so it can return without executing. How can I do this, without making a separate static page for each method?
Try JSTL conditional tag:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:if test="${not empty param.generatedInput}">
<custom:hello arg="${param.generatedInput}" />
</c:if>
Thus, the output of hello tag will be included into page only if generatedInput param is not null and not empty string.
In the end I resorted to AJAX, converting the tag responsible for generating the inputs into a servlet, and writing a function to request from the servlet without reloading the page:
var generateInputs = function() {
$.get("inputGeneratorServlet", {
method : $('#methods').val()
}, function(responseText) {
$("#inputsDiv").html(responseText);
});
};
$(document).ready(generateInputs());
Instead of generating the inputs from a tag, place a div and update the DOM under it with the response from the call to the generatorServlet.
<h2>Select method to test:</h2>
<form action="thisPage.jsp" method="post">
<h2>Select method to test:</h2>
<select name = 'methods' onchange='generateInputs()'>
// the select above is actually part of the things the servlet generates
// under the div below, but I included it for clarity
<div id="inputsDiv"></div>
<input type="submit" name="go" value="OK" />
</form>
This way, changing the selected method doesn't submit the form, and the form is only submitted if the user presses the OK button, signifying that he has filled out the generated inputs.

Issue related to inbuilt javascript function of Telerik controls

I have asp.net webapplication having some telerik controls.
i have a RadTextBox(txtSearch) and RadButton(btnSearch) on .aspx page.
i have written following validation for empty Textbox:
$('#btnSearch').click(function () {
if ($('#txtSearch_text').val() == '') {
$('#txtSearch_text').addClass('validation');
return false;
}
else {
$('#txtSearch_text').removeClass('validation');
}
});
in validation class i have set Border-left:2px solid red
now problem is that when i click on btnSearch it sets validation class to txtSearch textbox, but when i use mouseover on txtSearch textbox class name suddenly changed to someother from inbuilt javascript function of Telerik. in this Javascript function of telerik TextBox, it changes class name of textbox to another class.
and this execution of change class occurs after executing custom javascript function.
so i want to execute customer javascript function after executing inbuilt functions of telerik. how to do it?
Thanks
You can define invalid states for RadInputs. RadTextbox by itself cannot be invalid because you can put anything in a textbox (unlike a numeric textbox, for example), yet here is a starting point:
<telerik:RadTextBox ID="RadTextBox1" runat="server">
<InvalidStyle BackColor="Red" />
</telerik:RadTextBox>
<asp:RequiredFieldValidator ID="TextBoxRequiredFieldValidator" runat="server" Display="Dynamic"
ControlToValidate="RadTextBox1" ErrorMessage="The textbox can not be empty!">
</asp:RequiredFieldValidator>
<telerik:RadButton ID="RadButton1" runat="server" OnClientClicked="test" Text="submit"
AutoPostBack="false" />
<script type="text/javascript">
function test() {
var tb = $find('RadTextBox1');
if (tb.get_value() == "") {
$find('RadTextBox1')._invalidate();
$find('RadTextBox1').updateCssClass();
}
}
</script>
Tampering directly with the HTML of complex controls may get you nowhere because they will try to update/fix their state according to the logic they have.

Button value is not posted to Controller while Form Submit?

In my View, there are two <input type="image"> tags, and they are inside a Form.
In my Controller, the value of these two <input type="image"> always appears as 'null'. Earlier, with <input type="submit">, the values were posted to the controller.Now they're not.Can anyone tell me how to access those values in the Controller?
View:
#using (Html.BeginForm("Load", "Consent"))
{
//some code
<div id="button1" style="right:150px; width:90px; bottom :15px;position:absolute"><input type="image" src="../../img/ButtonBack.png" name="button" value="Previous" id="back" /></div>
<div id="button2" style="right:20px; width:90px; bottom :15px; position:absolute"><input type="image" src="../../img/ButtonNext.png" name="button" value="Acknowledge" id="ack" /></div>
</div>
}
The Form is getting posted to the controller, but the value of string button is 'null'
Controller:
[HttpPost]
public ActionResult Load(bool? chk_acknowledge, string button)
{
// some code
}
The browser is not required to post the value of the "value" attribute, so don't rely on it. Value is meant for radio and checkbox input fields: http://www.w3.org/TR/html401/interact/forms.html#edef-INPUT
However, browsers will post two other form fields: name.x and name.y. You could instead name your two image buttons "nextButton" and "prevButton". In your action, check if Request.Form contains "nextButton.x". If it exists, nextButton has been pressed. Likewise, if "prevButton.x" exists, prevButton has been pressed.
Only the value of the clicked image button would be posted (only the clicked submit button). While having the 2 image buttons inside a form, if there is another submit button and you click on that, the image buttons would not get posted.
Eg : You click on first image button inside div#button1 - in the post array, there would be [button] => Previous the other image button will not get posted.
Quote from the specification:
When a pointing device is used to click on the image, the form is
submitted and the click coordinates passed to the server. The x value
is measured in pixels from the left of the image, and the y value in
pixels from the top of the image. The submitted data includes
name.x=x-value and name.y=y-value where "name" is the value of the
name attribute, and x-value and y-value are the x and y coordinate
values, respectively.
So:
#using (Html.BeginForm("Load", "Consent"))
{
... some code
<input type="image" src="#Url.Content("~/img/ButtonBack.png")" name="Previous" />
<input type="image" src="#Url.Content("~/img/ButtonNext.png")" name="Acknowledge" />
}
and then:
[HttpPost]
public ActionResult Load(bool? chk_acknowledge)
{
if (!string.IsNullOrEmpty(Request["previous.x"]))
{
// the Previous image button was clicked
}
else if (!string.IsNullOrEmpty(Request["acknowledge.x"]))
{
// the Acknowledge image button was clicked
}
...
}

OnServerClick event handler not called if using onclick

I have a peculiar problem here and I can't by my life figure out what the solution is. Note that the following code is not dynamically created, but just immediately in my aspx file.
<button type="button" runat="server" id="btnSubmit"
OnServerClick="btnSubmit_Click" onclick="return confirm('Sure?');">
Submit
</button>
This works just fine as long as I don't have the onclick attribute there, i.e. the OnServerClick handler is fired as it should. But when I use the onclick attribute it is not, no matter whether I confirm or decline the confirmation dialog box.
What am I doing wrong?
If you look at the source code generated you will see the following:
onclick="return confirm('Sure?'); __doPostBack('btnSubmit','')"
so what is happening is the _doPostBack is never called. The hacky way to do what you're looking for is the following:
<button type="button" runat="server" id="btnSubmit"
OnServerClick="btnSubmit_Click" onclick="if (confirm('Sure?')) ">
The real correct way would be to use a Web Control:
<asp:Button runat="server"
OnClick="btnSubmit_Click" OnClientClick="return confirm('Sure?')" Text="Submit" />
I had more success with
<asp:Button ID="btnSubmit" runat="server" Text="Save" UseSubmitBehaviour="false"
OnClick="btnSubmit_Click" OnClientClick="if (!confirm('Sure?')) return" />
The accepted answer is not perfect. If you do not use type="button", the web page will do postback even you have clicked cancel. The correct and easiest way is to take advantage of short-circuit evaluation and do this hack: replace ; with && !, like below.
<button runat="server" id="btnSubmit"
OnServerClick="btnSubmit_Click" onclick="return confirm('Sure?') && !">
The output will look like this:
<button id="btnSubmit"
onclick="return confirm('Sure?') && ! __doPostBack('btnSubmit','')">
It gives correct return value because true && !undefined will return true and undefined will be evaluated and false && !undefined will return false and undefined will NOT be evaluated which is exactly what we want.
How about chaging button's type to submit, it works well :
<button type="submit" runat="server" id="btnSubmit"
OnServerClick="btnSubmit_Click" onclick="return confirm('Sure?');">
Submit
</button>
Try this:
<button type="button" runat="server" id="btnSubmit"
OnServerClick="btnSubmit_Click" onclick="if(!confirm('Sure?')) return;">
Submit
</button>
It sounds like the onclick event isn't bubbling through.
You should just be able to use
OnClientClick="return confirm('Sure?');
I don't think the other onClick should be necessary.
Edit:
This method would require you to hook your function to the OnClick event manually.
I am making up attributes this morning so in a futile effort to redeem myself-
Another method would be to insert javascript to catch the event. Something like..
$("form").submit(function() {
var resp = confirm("Save & Submit?");
if (resp) {
serializeStats();
return true;
}
else {
return false;
}
});
I do recall there being a better inline way to do this though.
Caffeine time now.
< button> is an HtmlButton control where OnServerClick is the only server-side click event. OnClick is unrecognized by ASP.NET so it gets passed to the client intact in case you want some client-side javascript code to execute before the postback.
< asp:button> is a WebControl which accomplishes the same thing with the OnClick server-side event and OnClientClick for client-side.
I think you're getting the behavior you see because your confirm('sure?') returns false which stops the postback mechanism. That would explain why it works when you don't have onclick in place.
Front Site
<button id="submit1" runat="server"
onclick="if(confirm('Sure?')) { } else{ return false} ;"
onserverclick="submit_ServerClick" >save</button>
Back Site
protected void submit_ServerClick(object sender, EventArgs e)
{
}