如何使用PXTimeSpan中的时间格式设置输入掩码

时间:2016-06-06 02:44:16

标签: c# erp acumatica

我有StartTime字段,我想用时间格式hh:mm:ss填充字段,但是当我将其应用于PXFormView时,它只显示小时和分钟。如何以timespan格式显示seconds?我用以下属性覆盖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>

screenshoot result

1 个答案:

答案 0 :(得分:0)

正如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" />

SecTimeSpanField