如何在Grid中设置默认值?

时间:2018-07-04 14:19:24

标签: c# acumatica

我有一个选择器控件,我想在用户单击“ +”按钮时设置一个默认值,我尝试了字段默认事件CacheAttached和RowInserted,但它们都不起作用。

下面是参考代码。

//Cache Attached
[PXDBString(4, IsFixed = true, IsKey = true, InputMask = "####")]
[PXDefault(typeof(Search<AMShiftMst.shiftID, Where<AMShiftMst.shiftID, Equal<AMShiftID>>>), PersistingCheck = PXPersistingCheck.Nothing)]
[PXSelector(typeof(Search<AMShiftMst.shiftID>))]
[PXUIField(DisplayName = "Shift")]
protected virtual void AMShift_ShiftID_CacheAttached(PXCache sender)
{

}

//RowInserted
protected virtual void AMShift_RowInserted(PXCache sender, PXRowInsertedEventArgs e, PXRowInserted InvokeBaseHandler)
{
    if (InvokeBaseHandler != null)
        InvokeBaseHandler(sender, e);

    AMShift row = e.Row as AMShift;

    if (row == null)
        return;

    row.ShiftID = "1";            
}

//Defaulting Event
protected virtual void AMShift_ShiftID_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
{
    AMShift doc = e.Row as AMShift;

    if (doc != null)
    {
        AMShiftMst AMShiftMstData = PXSelectReadonly<AMShiftMst, Where<AMShiftMst.shiftID, Equal<Required<AMShiftMst.shiftID>>>>.Select(Base, "1");

        if (AMShiftMstData != null)
        {
            e.NewValue = AMShiftMstData.ShiftID;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

使用它们的默认方式,但是它们要求用户首先输入新行。在此图中,它要求用户至少填写一个字段值才能获取默认值以触发。

我从上一个答案中更新了代码,以包括该行的自动插入,然后使用您的默认值将自动设置值。请注意,也可以如下所示将全局SetVisible调用移至Initialize。

public class MyGraphExtensionTest : PXGraphExtension<WCMaint>
{
    public override void Initialize()
    {
        base.Initialize();

        PXUIFieldAttribute.SetVisible<AMShift.crewSize>(Base.WCShifts.Cache, null, false);
        PXUIFieldAttribute.SetVisible<AMShift.machNbr>(Base.WCShifts.Cache, null, false);
        PXUIFieldAttribute.SetVisible<AMShift.shftEff>(Base.WCShifts.Cache, null, false);
    }

    protected virtual void AMWC_RowInserted(PXCache sender, PXRowInsertedEventArgs e, PXRowInserted del)
    {
        if (del != null)
        {
            del(sender, e);
        }

        var row = (AMWC) e.Row;
        if (string.IsNullOrWhiteSpace(row?.WcID) || Base.IsImport || Base.IsContractBasedAPI || Base.WCShifts.Cache.Inserted.Any_())
        {
            return;
        }

        Base.WCShifts.Insert(new AMShift
        {
            WcID = row.WcID
        });
    }

    protected virtual void AMShift_ShiftID_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e, PXFieldDefaulting del)
    {
        if (del != null)
        {
            del(sender, e);
        }

        AMShift doc = e.Row as AMShift;

        if (doc != null)
        {
            AMShiftMst AMShiftMstData = PXSelect<AMShiftMst, Where<AMShiftMst.shiftID, Equal<Required<AMShiftMst.shiftID>>>>.Select(Base, "1");

            if (AMShiftMstData != null)
            {
                e.NewValue = AMShiftMstData.ShiftID;
            }
        }
    }

    protected virtual void AMShift_CalendarID_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e, PXFieldDefaulting del)
    {
        if (del != null)
        {
            del(sender, e);
        }

        AMShift doc = e.Row as AMShift;

        if (doc != null)
        {
            CSCalendar CSCalendarData = PXSelect<CSCalendar, Where<CSCalendar.calendarID, Equal<Required<CSCalendar.calendarID>>>>.Select(Base, "OFFICE");

            if (CSCalendarData != null)
            {
                e.NewValue = CSCalendarData.CalendarID;
            }
        }
    }
    protected virtual void AMShift_LaborCodeID_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e, PXFieldDefaulting del)
    {
        if (del != null)
        {
            del(sender, e);
        }

        AMShift doc = e.Row as AMShift;

        if (doc != null)
        {
            AMLaborCode AMLaborCodeData = PXSelect<AMLaborCode, Where<AMLaborCode.laborCodeID, Equal<Required<AMLaborCode.laborCodeID>>>>.Select(Base, "DIRECT");

            if (AMLaborCodeData != null)
            {
                e.NewValue = AMLaborCodeData.LaborCodeID;
            }
        }
    }
}