locked
HTML - formatting decimal places in data entry fields RRS feed

  • Question

  • Going crazy and spending a lot of time trying to get input fields to display numbers with trailing zeros.

    I want 3 decimal places to be shown when presenting data from the database, or after someone edits the field.

    So, if someone enters "2.34", I want "2.340" to display (after they tab out of field or save)

    I've tried the postRender "toFixed" suggestions in other posts such as: https://msdn.microsoft.com/en-us/library/jj733572.aspx#Format

    I've also converted text to number or floats and back again.

    It's not working. Something is taking away the trailing zeroes.

    I did find that if I used the exact same logic and changed the "displayName" of the page (just because it was easy to see when testing), that the number shows up correctly, yet it doesn't show up in the form area correctly.

    This works:

    var textValue = parseFloat(value).toFixed(3);
    contentItem.screen.details.displayName = ">" + textValue + "<";

    This doesn't:

    contentItem.screen.myScreen.TestValue = parseFloat(value).toFixed(3);

    And neither does this:

    var textValue = parseFloat(value).toFixed(3);
    contentItem.screen.myScreen.TestValue = textValue;

    And neither does this (in a postRender function):

      $(element).text(value.toFixed(3));

    Anyone have a solution for this? This is a basic user interface thing that shouldn't be this hard to do.

    Thanks,

    Dave

    Tuesday, August 23, 2016 9:53 PM

All replies

  • Try wrapping the changing line inside a setTimeout({},0) call whenever you're trying to manually adjust any display properties. That ensures that it doesn't execute until all of the other postRender code has made changes to the screen that might affect the field you're trying to alter, even if you think nothing else is related to it. It usually works just fine after that.

    Regards,

    RT Watkins

    • Proposed as answer by Xpert360 Friday, September 2, 2016 7:35 PM
    Friday, August 26, 2016 7:23 PM
  • It looks to me like msls-xxx.js is reformatting the string after the postRender code runs.

    I've also tried creating $(element).change functions to do it, but the msls scripts seem to attach after my "change" functions, so they overwrite it.

    It does this for decimal data types. msls seems to look at the datatype, and then runs it's own formatting code which drops trailing zeroes.

    I'm considering creating shadow local data items of property type "string" to make this work. It's a pain to do, and requires extra data handling that seems excessive for such a common task. I have dozens of values on dozens of screens that need to be formatted this way.

    Do you think the timeout method would delay until after the msls scripts run, or do you think the msls .change functions would get triggered anyway?

    Thanks,

    Dave

    Friday, September 2, 2016 8:23 PM
  • If I use firebug console, and run $("input.ui-input-text").change(function () { setDecimal(this); });

    it does work.

    Friday, September 2, 2016 9:48 PM