[CLOSED] Formatting 'Number' Field

  1. #1

    [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?

    <!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]
  2. #2
    Hi Jeff,

    Unfortunately, a NumberField doesn't support formatting still. Your approach with a TextField and handling its Change event appears to be the best solution.

Similar Threads

  1. [CLOSED] Formatting Number Decimal & thousand seperator
    By FpNetWorth in forum 1.x Legacy Premium Help
    Replies: 14
    Last Post: Mar 30, 2015, 1:06 PM
  2. Formatting number for NumberField
    By fac in forum 1.x Help
    Replies: 1
    Last Post: Jan 30, 2012, 4:02 PM
  3. [CLOSED] Number Field
    By Vasudhaika in forum 1.x Legacy Premium Help
    Replies: 19
    Last Post: Jul 19, 2011, 8:59 AM
  4. [CLOSED] number formatting
    By majestic in forum 1.x Legacy Premium Help
    Replies: 6
    Last Post: Jul 28, 2010, 10:56 AM
  5. Number Field
    By fabiomarcos in forum 1.x Help
    Replies: 2
    Last Post: Jan 20, 2009, 11:29 AM

Tags for this Thread

Posting Permissions