Map class domain - grails

I have my class domain
class domain Factura {
String name
BigDecimal value
}
in my html:
<g:form>
<input type="text" name="name" />
<input type="number" step="any" name="value" />
<inpu type="submit" value="save" />
</g:form>
in my controller
def save(Factura factura) {
//Suppose in the value I have put : 152.36
println "valor: " + factura.value => return 15236.0
//Suppose in the value I have put : 152,36
println "valor: " + factura.value => return 15236.0
}
How can I return as a decimal? I need 152.36

You can use text input and cast it to BigDecimal
Change this
<input type="number" step="any" name="value" />
to
<input type="text" name="value" />
In your save method
try{
params.value.toBigDecimal()
catch(e){
// error
}

Related

how to write onclick event in reactjs?

here is my code
this is simple form. I am trying to call on click event on button click
render: function () {
return (
<form className="commentForm">
<input
type="text"
placeholder="Your name"
value={this.state.author}
onChange={this.handleAuthorChange}
/>
<input
type="text"
placeholder="Say something..."
value={this.state.text}
onChange={this.handleTextChange}
/>
<input type="submit" value="Post" />
</form>
);
You forgot to pass the onSubmit event for form
render: function () {
return (
<form className="commentForm" onSubmit={this.submit}>
<input
type="text"
placeholder="Your name"
value={this.state.author}
onChange={this.handleAuthorChange}
/>
<input
type="text"
placeholder="Say something..."
value={this.state.text}
onChange={this.handleTextChange}
/>
<input type="submit" value="Post" />
</form>
);
submit: function() {
// do your stuff
}
try this:
<form className="commentForm" onSubmit={this.handleSubmit}>
<input
type="text"
placeholder="Your name"
value={this.state.author}
onChange={this.handleAuthorChange}
/>
<input
type="text"
placeholder="Say something..."
value={this.state.text}
onChange={this.handleTextChange}
/>
<input type="submit" value="Post" />
</form>
Can you try like this
handleTextChange(e) {
const { name, value } = e.target;
this.setState({
[name]: value
});
}
_handleSubmit(e) {
e.preventDefault();
let { author, text} = this.state;
// dispatch the submit function
}
render: function () {
return (
<form className="commentForm" onSubmit={this._handleSubmit}>
<input
type="text"
placeholder="Your name"
name="author"
value={this.state.author}
onChange={this.handleAuthorChange}
/>
<input
type="text"
placeholder="Say something..."
name="text"
value={this.state.text}
onChange={this.handleTextChange}
/>
<input type="submit" value="Post" />
</form>
);
u want onclick method here it is onsubmit same work of onclick.
handleSubmit(){
console.log(this.state.author);// u can see the value of autor and say somthing input vale in here it is coming right value or not
console.log(this.state.text);
axios.post(route,{
name:this.state.author, //these name and say are the variable whice are use to take the values to back end
Say :this.state.text
}).then({response}); //here u cn give some thing to disply after data add to database.
}
<form className="commentForm" onsubmit={this.handleSubmit.bind(this)}>
<input
type="text"
placeholder="Your name"
value={this.state.author}
onChange={this.handleAuthorChange}
/>
<input
type="text"
placeholder="Say something..."
value={this.state.text}
onChange={this.handleTextChange}
/>
<input type="submit" value="Submit" />
</form>
You need not have separate onChange for two fields unless you do something different in each of them.
Render:
<form className="commentForm" onSubmit={this.handleSubmit}>
<input
type="text"
placeholder="Your name"
name="author"
onChange={this.handleChange}
/>
<input
type="text"
placeholder="Say something..."
name="text"
onChange={this.handleChange}
/>
<button type="submit" >submit</button>
</form>
When submit is clicked :
handleSubmit = (e) => {
e.preventDefault();
console.log(this.state.author) //whatever you want to process the data with...
}
You can have single handleChange to set the state
handleChange = (e) => {
const { value, name } = e.target;
this.setState({ [name]: e.target.value })
}

Breeze SaveChanges

I am new to BreezeJS, and have recently created a project that is up and running. I am able to use Breeze to query data from my SQL server without a problem. However, whenever I try to save changes, the changes do not save. I have verified, in VS 2012, that the JavaScript save function is actually called, however the HttpPost method on the server side is never called.
Here is the Breeze Controller that I have set up:
using System.Linq;
using System.Web.Http;
using Newtonsoft.Json.Linq;
using Breeze.WebApi;
using FitnessTracker.Models;
namespace FitnessTracker.Controllers
{
[BreezeController]
public class BreezeController : ApiController
{
private readonly EFContextProvider<FitnessTrackerContext> _fitnessContext =
new EFContextProvider<FitnessTrackerContext>();
//
// GET: /Breeze/
[HttpGet]
public string Metadata()
{
return _fitnessContext.Metadata();
}
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
return _fitnessContext.SaveChanges(saveBundle);
}
[HttpGet]
public IQueryable<FitnessEquipment> FitnessEquipments()
{
return _fitnessContext.Context.FitnessEquipments;
}
}
}
Here is the save function that I am using:
function saveChanges() {
return manager.saveChanges()
.fail(saveFailed);
}
Here is the HTML:
<div class="box-body form" data-bind="foreach: exercises">
<input type="text" class="txt-m g2" data-bind="text: EquipmentName" />
<input type="text" class="txt-m g1" data-bind="text: EquipmentLevel" />
<input type="text" class="txt-m g2" data-bind="text: DurationSeconds" />
<input type="text" class="txt-m g2" data-bind="text: Weight" />
<input type="text" class="txt-m g4" data-bind="text: DateTime" />
<br class="clear" />
</div>
Save Changes
Thanks in advance for your help!!
Edit 9/12/2013:
I have fixed it thanks to Jay's help!!
The problem is that I was binding on the text instead of the value. I changed the html to:
<div class="box-body form" data-bind="foreach: exercises">
<input type="text" class="txt-m g2" data-bind="value: EquipmentName" />
<input type="text" class="txt-m g1" data-bind="value: EquipmentLevel" />
<input type="text" class="txt-m g2" data-bind="value: DurationSeconds" />
<input type="text" class="txt-m g2" data-bind="value: Weight" />
<input type="text" class="txt-m g4" data-bind="value: DateTime" />
<br class="clear" />
</div>
It is now saving without a problem. Thanks Jay for your help!
Have you confirmed that you are actually changing/or adding an entity within the EntityManager on the client? You can call the EntityManager's hasChanges method to confirm. Breeze does not attempt to call the server if there are no changes to save.
So try something like
if (manager.hasChanges()) {
manager.saveChanges()
.fail(saveFailed);
} else {
// my guess is that you will get here.
}
My guess is that you either have a binding issue or have not queried or created an entity that requires saving.

Hide/Disable submit button from controller

How can I hide a webform button from controller action? or Do I do it in webform itself?
There is a condition to hide/disable the button:
if (StudentType != "Senior")
{
Hide Button
}
Display Button
View:
<form method="post" action="/Student/Dispatch/">
<label for="id">Student Number: </label>
<input type="text" name="id" value="" /> <br /><br />
<input type="submit" value="Get Student(xls)" name="xls" /> &nbsp
<input type="submit" value="Get Student(pdf)" name="pdf" />
</form>
Controller:
[HttpPost]
public ActionResult Dispatch(string pdf, string id) {
if (!string.IsNullOrEmpty(pdf)) {
// GetPdf submit button was clicked
return StudentPdf(id);
}
// GetXls submit button was clicked
return StudentExcel(id);
}
You can use ViewData dictionary .
Controller:
if (StudentType != "Senior")
{
ViewData["isHideButton"] =true;
}
View:
<form method="post" action="/Student/Dispatch/">
<label for="id">Student Number: </label>
<input type="text" name="id" value="" /> <br /><br />
<% bool hideButton= false;
bool.TryParse(ViewData["isHideButton"],hideButton)%>
<%if(!hideButton)
{%>
<input type="submit" value="Get Student(xls)" name="xls" />
<%}%>
&nbsp <input type="submit" value="Get Student(pdf)" name="pdf" />
</form>

how to get an collection of ids int[] in post action (selected checkboxes)

I need to get in an action method the values from the selected checkboxes on a form
how would you do that ? (or probably get them all and see who was selected)
public ActionResult (int[] ids)
...
<input type="checkbox" value = "1" />
<input type="checkbox" value = "2" />
<input type="checkbox" value = "3" />
<input type="checkbox" value = "4" />
<%=Html.Submit(); %>
You could try giving the checkboxes a name (ids):
<input type="checkbox" name="ids" value="1" />
<input type="checkbox" name="ids" value="2" />
<input type="checkbox" name="ids" value="3" />
<input type="checkbox" name="ids" value="4" />
Should work with:
public ActionResult Index(int[] ids) { ... }

ASP.NET MVC 2 strongly typed htmlhelper, indexes

public class Foo { public bool Checked {get;set;}}
View:
<viewdata model="Foo[] fooList" />
<for each="var f in fooList">
${Html.CheckBoxFor(x=>x[fIndex].Checked)}
</for>
Will output:
<input id="Checked" name="Checked" type="checkbox" value="true" />
<input name="Checked" type="hidden" value="false" />
<input id="Checked" name="Checked" type="checkbox" value="true" />
<input name="Checked" type="hidden" value="false" />
<input id="Checked" name="Checked" type="checkbox" value="true" />
<input name="Checked" type="hidden" value="false" />
Problem is that System.Web.Mvc.ExpressionHelper.GetExpressionText does not include index in id/name.
That leads to problems in case I want to add a label for every checkbox (because all id`s are the same).
Any ideas how to handle this properly?
From the MVC source=>
while (part != null) {
if (part.NodeType == System.Linq.Expressions.ExpressionType.MemberAccess) {
MemberExpression memberExpressionPart = (MemberExpression)part;
nameParts.Push(memberExpressionPart.Member.Name);
part = memberExpressionPart.Expression;
}
else {
//arghhhh... [index] != MemberAccess :(
break;
}
}
The ability of the expression-based helpers to understand indexes isn't in the product yet. It will be in the next preview release (whatever comes after MVC 2 RC). See http://aspnet.codeplex.com/WorkItem/View.aspx?WorkItemId=4970.
Use the CheckBoxFor overload that allows you to specify html attributes:
CheckBoxFor(TModel)(HtmlHelper(TModel), Expression(Func(TModel, Boolean)), IDictionary(String, Object))
For example,
${Html.CheckBoxFor(x => x[fIndex].Checked, new { id = "foo" + fIndex) })}

Resources