【问题标题】:How to set inputmask using the time format in PXTimeSpan如何使用 PXTimeSpan 中的时间格式设置输入掩码
【发布时间】:2016-06-06 02:44:15
【问题描述】:

我有一个StartTime 字段,我想用时间格式hh:mm:ss 填充该字段,但是当我将它应用于PXFormView 时,它只显示小时和分钟。如何使它以seconds 格式显示timespan?我正在使用以下属性覆盖 DAC 对象:

protected int? _CapacityStart;
[PXDBInt()]
[PXDefault(0)]
[PXUIField(DisplayName = "Start")]

下面的aspx代码,

<px:PXTimeSpan TimeMode="True" ID="edCapacityStart" runat="server" DataField="CapacityStart" InputMask="hh:mm:ss" AllowNull="False" Size="S" ></px:PXTimeSpan>

【问题讨论】:

  • 我相信时间跨度的工作原理是将时间(以分钟为单位)保存到 INT 字段中。如果分钟是最低存储值,我看不出您将如何获得秒数。您可能需要考虑使用日期时间字段并仅显示时间部分。

标签: c# erp acumatica


【解决方案1】:

正如 Brendan 所说,PXTimeSpan 控件不允许您添加秒数。但您可以使用 DateTime 字段。

这是一个自定义属性的代码,它允许您在数据库中保存秒而不是分钟。

using System;
using PX.Data;

namespace PX.Data
{

    #region PXDBSecTimeSpanAttribute

    /// <summary>Maps a DAC field of <tt>int?</tt> type to the <tt>int</tt>
    /// database column. The field value represents a date as a number of
    /// seconds passed from 01/01/1900.</summary>
    /// <remarks>
    /// <para>The attribute is added to the value declaration of a DAC field.
    /// The field becomes bound to the database column with the same
    /// name.</para>
    /// <para>The field value stores a date as a number of seconds. In the UI,
    /// the string is typically represented by a control allowing a selection
    /// from the list of time values with half-hour interval.</para>
    /// </remarks>
    /// <example>
    /// <code>
    /// [PXDBSecTimeSpan]
    /// [PXUIField(DisplayName = "Run Time")]
    /// public virtual int? RunTime { get; set; ]
    /// </code>
    /// </example>
    public class PXDBSecTimeSpanAttribute : PXDBTimeSpanAttribute
    {
        #region State

        /// <summary>
        /// The "00:00:00" constant.
        /// </summary>
        public new const string Zero = "00:00:00";

        /// <summary>
        /// The BQL constant representing string "00:00:00".
        /// </summary>
        public new sealed class zero : Constant<string> { public zero() : base(Zero) { } }

        #endregion State

        #region Ctor

        /// <summary>
        /// Initializes a new instance with default parameters.
        /// </summary>
        public PXDBSecTimeSpanAttribute() : base()
        {
            _InputMask = "HH:mm:ss";
            _DisplayMask = "HH:mm:ss";
        }

        #endregion Ctor

        #region Implementation

        /// <exclude/>
        public override void FieldSelecting(PXCache sender, PXFieldSelectingEventArgs e)
        {
            if (_AttributeLevel == PXAttributeLevel.Item || e.IsAltered)
            {
                e.ReturnState = PXDateState.CreateInstance(e.ReturnState, _FieldName, _IsKey, null, _InputMask, _DisplayMask, _MinValue, _MaxValue);
            }

            if (e.ReturnValue != null && (e.ReturnValue is int || e.ReturnValue is int?))
            {
                TimeSpan span = new TimeSpan(0, 0, 0, (int)e.ReturnValue);
                e.ReturnValue = new DateTime(1900, 1, 1).Add(span);
            }
        }

        /// <exclude/>
        public override void FieldUpdating(PXCache sender, PXFieldUpdatingEventArgs e)
        {
            if (e.NewValue == null || e.NewValue is int)
            {
            }
            else if (e.NewValue is string)
            {
                DateTime val;
                if (DateTime.TryParse((string)e.NewValue, sender.Graph.Culture, System.Globalization.DateTimeStyles.None, out val))
                {
                    TimeSpan span = new TimeSpan(val.Hour, val.Minute, val.Second);
                    e.NewValue = (int)span.TotalSeconds;
                }
                else
                {
                    e.NewValue = null;
                }
            }
            else if (e.NewValue is DateTime)
            {
                DateTime val = (DateTime)e.NewValue;
                TimeSpan span = new TimeSpan(val.Hour, val.Minute, val.Second);
                e.NewValue = (int)span.TotalSeconds;
            }
        }

        #endregion Implementation

        /// <summary>Returns the date obtained by adding the specified number of
        /// seconds to 01/01/1900.</summary>
        /// <param name="seconds">The seconds to add to the default date.</param>
        public static DateTime FromSeconds(int seconds)
        {
            TimeSpan span = new TimeSpan(0, 0, 0, seconds);
            return new DateTime(1900, 1, 1).Add(span);
        }
    }

    #endregion PXDBSecTimeSpanAttribute 
}

您可以将其添加到您的自定义项目的代码部分,然后在自定义字段中使用它或替换现有 PXDBInt 时间跨度字段的属性,您还希望在其中节省秒数,如下所示:

[PXDBSecTimeSpan]
[PXUIField(DisplayName="Start")]
public virtual int? CapacityStart { get; set; }

并使用启用了 TimeMode 的 PXDateTimeEdit 控件来编辑值:

<px:PXDateTimeEdit runat="server" ID="edUsrSpanStart" DataField="UsrSpanStart" TimeMode="True" />

【讨论】:

    猜你喜欢
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多