Sep 15, 2015, 7:22 AM
[CLOSED] Formatting 'Number' Field
Hi
I have an application which allows the user to check a bank statement. As part of that I want to have a 'number' field in the TopBar into which the user enters the starting balance, a decimal figure. I need the starting balance to be displayed as a UK currency. I've got a simplified version working, see below, but it seems a very 'cack handed' way of doing it. There must be a better way?
I have an application which allows the user to check a bank statement. As part of that I want to have a 'number' field in the TopBar into which the user enters the starting balance, a decimal figure. I need the starting balance to be displayed as a UK currency. I've got a simplified version working, see below, but it seems a very 'cack handed' way of doing it. There must be a better way?
<!DOCTYPE html>
<html>
<head>
<title></title>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
@section scripts
{
<script>
var startingBalanceChange = function (numField) {
Ext.net.DirectMethod.request({
url: '@(Url.Action("StartingBalanceChanged"))',
params: { balance: numField.value },
success: function () {
if (numField.value.substring(0, 1) != "£") {
numField.setValue(Ext.util.Format.number(numField.getValue(), '£#,###.00'));
}
}
})
};
</script>
}
</head>
<body>
@section mainBody
{
@(Html.X().GridPanel().Width(200)
.Title("Users").ID("GridPanel").Border(true)
.Store(Html.X().Store()
.ID("Store")
.Model(Html.X().Model().IDProperty("ID")
.Fields(
new ModelField("ID", ModelFieldType.Int),
new ModelField("Value",ModelFieldType.Float)
)
)
.Proxy(Html.X().AjaxProxy().Url(Url.Action("Read")).Reader(Html.X().JsonReader().RootProperty("data"))))
.ColumnModel(
Html.X().NumberColumn().Text("Value").DataIndex("Value").Align(Alignment.Right).Format("£#,###.00")
)
.TopBar(
Html.X().Toolbar()
.Items(
Html.X().TextField().ID("nfStartingBalance").FieldLabel("Starting Balance:").Width(220).LabelWidth(120)
.Listeners(lst => { lst.Change.Handler = "startingBalanceChange(this)"; })
)
)
)
}
</body>
</html>
using Ext.Net;
using Ext.Net.MVC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace ExtSandpit.Controllers
{
public class HomeController : Controller
{
private static decimal startingBalance;
public ActionResult Index()
{
return View();
}
public DirectResult StartingBalanceChanged(string balance)
{
decimal parsedBalance;
if (balance.Length > 0)
{
if (decimal.TryParse(balance.Substring(1, balance.Length - 1), out parsedBalance))
{
startingBalance = parsedBalance;
}
X.GetCmp<Store>("Store").Reload();
}
return this.Direct();
}
public ActionResult Read()
{
List<Cost> lstValues = new List<Cost>();
if (startingBalance < 1.07M)
{
lstValues.Add(new Cost(1000, 1.06M));
}
lstValues.Add(new Cost(1001, 500.92M));
lstValues.Add(new Cost(1002, 5234.92M));
return this.Store(lstValues);
}
}
public class Cost
{
public Cost()
{
}
public Cost(int id, decimal value)
{
ID = id;
Value = value;
}
public int ID { get; set; }
public decimal Value { get; set; }
}
}
Last edited by Daniil; Sep 22, 2015 at 11:14 AM.
Reason: [CLOSED]