Hello andriikravetskyi35,
Just to recap : my aim was to avoid having a new record when navigating in a grid. This occurs in my case when I click "next" on last record or "previous" on first record.
The solutions I have writen thanks to zfebert56 and you is the following :
For button "next" :
- Firstly, I have created an overloaded method for my main view. In this method, I get and memorize first and last record identifiers (in my case, RefNbr fits).
here is the code :
#region Attributes
private string firstRecordID;
private string lastRecordID;
#endregion
...
public SelectFrom<EAPInvoice>
.LeftJoin<Vendor>.On<Vendor.bAccountID.IsEqual<EAPRegister.vendorID>>
.OrderBy<Asc<EAPInvoice.refNbr>>
.View DocumentView;
protected virtual IEnumerable documentView()
{
IEnumerable<EAPInvoice> result = SelectFrom<EAPInvoice>
.LeftJoin<Vendor>.On<Vendor.bAccountID.IsEqual<EAPRegister.vendorID>>
.OrderBy<Asc<EAPInvoice.refNbr>>.View.Select(this).RowCast<EAPInvoice>().ToList();
firstRecordID = result.FirstOrDefault()?.RefNbr;
lastRecordID = result.LastOrDefault()?.RefNbr;
return result;
}
- Secondly, I have created a new class that inherits from PXPrevNextBase. In this class, the method "Handler(PXAdapter adapter)" is overloaded to behave differently if we detect that current record is the last one.
public new PXNextRestricted<EAPInvoice> Next;
public class PXNextRestricted<TNode> : PXPrevNextBase<TNode> where TNode : class, IBqlTable, new()
{
protected override int DefaultStartRow => 0;
public PXNextRestricted(PXGraph graph, string name) : base(graph, name) { }
public PXNextRestricted(PXGraph graph, Delegate handler) : base(graph, handler) { }
[PXUIField(DisplayName = "Next", MapEnableRights = PXCacheRights.Select)]
[PXNextButton]
protected override IEnumerable Handler(PXAdapter adapter)
{
EAPInvoiceEntry eapInvoiceEntryGraph = base.Graph as EAPInvoiceEntry;
EAPInvoice currentEapInvoice = eapInvoiceEntryGraph.DocumentView.Current;
adapter.StartRow += adapter.MaximumRows;
IEnumerable result = base.Handler(adapter);
bool isLastRecord = eapInvoiceEntryGraph.lastRecordID == null || (currentEapInvoice != null && currentEapInvoice.RefNbr != null && currentEapInvoice.RefNbr.Equals(eapInvoiceEntryGraph.lastRecordID));
if (isLastRecord)
{
adapter.StartRow -= adapter.MaximumRows; // back
result = base.Handler(adapter);
SetEnabled(false);
}
else
{
SetEnabled(true);
}
return result;
}
}
For button previous, it is the save idea. This code can be improved I think but I didn't have time any more to get deeper. Anyway, It works as it is.
Hope It will be helpfull for you.