How to properly sort Tasks having multi-level Task

2020-04-21 03:54发布

问题:

Default Out-of-box Sorting:

Expected Sorting:

回答1:

Set Edit Mask to Unicode for Project Task Segmented Key (CS202000) in order to allow multi-level Task CD (allowing . in value)

To get the expected sorting,

We will create custom BQL function which will pad the numeric portion to the out-of-box TaskCD field.

public class HierarchySorting<StringField> : BqlFormulaEvaluator<StringField>, IBqlOperand
        where StringField : IBqlField
{
    public override object Evaluate(PXCache cache, object item, 
                                    Dictionary<Type, object> pars)
    {
        PXFieldState fState = cache.GetStateExt<StringField>(item) as PXFieldState;
        return GetSortOrderValueExt(Convert.ToString(fState.Value));
    }

    public string GetSortOrderValueExt(string taskCD)
    {
        return Regex.Replace(taskCD, "[0-9]+", MatchReplacer => MatchReplacer.Value.PadLeft(10, '0'));
    }
}

This custom BQL function will pad zeros for any number in out-of-box TaskCD value.

We will create a new un-bound user defined field in DAC Extension of PMTask and will decorate with PXFormula having custom BQL function

public class PMTaskPXExt : PXCacheExtension<PMTask>
{
    public abstract class usrSortingTaskCD : IBqlField { }

    [PXString(IsUnicode = true)]
    [PXUIField(DisplayName = "Usr Task")]
    [PXFormula(typeof(HierarchySorting<PMTask.taskCD>))]
    public virtual string UsrSortingTaskCD { get; set; }
}

We will replace the sorting for the data view using the OrderByNew method of PXSelectBase/PXView.

public class ProjectEntryPXDemoExt : PXGraphExtension<ProjectEntry>
{
    public override void Initialize()
    {
        Base.Tasks.OrderByNew<OrderBy<Asc<PMTaskPXExt.usrSortingTaskCD>>>();
    }
}