22
Views
4
Comments
Solved
Start Index & Navigation/Pagination - How can I get the start index?
Application Type
Reactive

Hi buddies,


I hope you are fine.


I have one question regarding the Start Index and Navigation/Pagination widget.

I want to use the Navigation/Pagination widget to refresh my table records. I use the GetTransactions to fetch data and inside this action, I have the Aggregate GetTotalTransactions and an assign where I assign the data to the output TransactionList and Count. In the properties of aggregate GetTotalTransactions doesn't exist the Start Index property, only the Max Records property. 

Usually, I use an Aggregate directly inside of the screen/block to fetch data and the aggregate has the property of Start Index and Max Records. In this situation, fetch data from other sources, the aggregate inside doesn't have the property of Start Index.

My question is: How can I get/calculate the start index and assign it to the table records?




I send my oml file attached.

Thanks in advance.


Kind regards,

FC

MyFinancee.oml

Rank: #86
Solution

Hello Fábio,

Hope you're doing well.


Checking on your code, you have GetTotalTransactions Aggregate inside GetTransactions Data Action. Why don't you simply execute the Aggregate (Fetch Data from Database) directly instead of using a Data Action (Fetch Data from Other Sources)?

If you do this, you will have access to both Start Index and Max. Records properties from the Aggregate and you can implement a Pagination like described in here. You had implemented like this in your TransactionCategories screen.


If you want to use a Data Action (Fetch Data from Other Sources) and implement a Pagination, you will still be able to do that, but you have to implement an additional logic.

  • Create a Local Variable of the same type of the List resulting from your Data Action (let's call it TransactionListLocal).
  • Create a new Screen Action and bind it to On After Fetch event of the Data Action (let's call it OnAfterFetch_GetTransactions). Basically, this event will run after the Data Action is executed.In this Screen Action, you'll need to iterate over the Data Action output GetTransactions.TransactionList (total records) and append to the Local Variable just the records that you want to show in the screen for the first time - Start Index = 0 / Maximum Iterations = MaxRecords.Since MaxRecords default value is 5, you will have 5 records in TransactionListLocal variable after this, corresponding to the first 5 records (first page of the list).

  • Bind the Local Variable to the table instead of the Data Action output.
  • In your OnNavigate_Pagination Screen Action, you'll need to clear your Local Variable, get the next records (for the next page) from the Data Action output (based on NewStartIndex) and append them to the Local Variable (similarly to what you did before) - Start Index = NewStartIndex / Maximum Iterations = MaxRecords.You will also need to assign StartIndex variable to NewStartIndex, so your Navigation\Pagination Block is updated.

  • Back to the screen, all of your table's expressions should rely on TransactionListLocal variable (the one bound to the table) and not on GetTransactions.TransactionList.


Please refer to attached OML file with these changes.


Hope that this helps you!


Kind regards,

Rui Barradas

MyFinance.oml

Rank: #86
Solution

Hello again Fábio,

In terms of performance, I would suggest for you to use Aggregates whenever is possible. The OutSystems Platform has optimizations for Aggregates, such as optimizations for the number of fields to be retrieved (based on the fields that you are actually using).

If you do this Aggregate inside a Data Action, then the Platform can't optimize it. So I suggest to use an Aggregate in this scenario.


Hope that this helps you!


Kind regards,

Rui Barradas

Rank: #86
Solution

Hello Fábio,

Hope you're doing well.


Checking on your code, you have GetTotalTransactions Aggregate inside GetTransactions Data Action. Why don't you simply execute the Aggregate (Fetch Data from Database) directly instead of using a Data Action (Fetch Data from Other Sources)?

If you do this, you will have access to both Start Index and Max. Records properties from the Aggregate and you can implement a Pagination like described in here. You had implemented like this in your TransactionCategories screen.


If you want to use a Data Action (Fetch Data from Other Sources) and implement a Pagination, you will still be able to do that, but you have to implement an additional logic.

  • Create a Local Variable of the same type of the List resulting from your Data Action (let's call it TransactionListLocal).
  • Create a new Screen Action and bind it to On After Fetch event of the Data Action (let's call it OnAfterFetch_GetTransactions). Basically, this event will run after the Data Action is executed.In this Screen Action, you'll need to iterate over the Data Action output GetTransactions.TransactionList (total records) and append to the Local Variable just the records that you want to show in the screen for the first time - Start Index = 0 / Maximum Iterations = MaxRecords.Since MaxRecords default value is 5, you will have 5 records in TransactionListLocal variable after this, corresponding to the first 5 records (first page of the list).

  • Bind the Local Variable to the table instead of the Data Action output.
  • In your OnNavigate_Pagination Screen Action, you'll need to clear your Local Variable, get the next records (for the next page) from the Data Action output (based on NewStartIndex) and append them to the Local Variable (similarly to what you did before) - Start Index = NewStartIndex / Maximum Iterations = MaxRecords.You will also need to assign StartIndex variable to NewStartIndex, so your Navigation\Pagination Block is updated.

  • Back to the screen, all of your table's expressions should rely on TransactionListLocal variable (the one bound to the table) and not on GetTransactions.TransactionList.


Please refer to attached OML file with these changes.


Hope that this helps you!


Kind regards,

Rui Barradas

MyFinance.oml

Rui,


I hope you are fine.


Yeah, I understand your question :).

I know how can I use the Navigation/Pagination widget with the Aggregate (Fetch Data from Database) directly but I don't know how can I use the Navigation/Pagination widget with Data Action (Fetch Data from Other Sources). So I tried to do that and I couldn't do it.

What is the best solution in terms of performance?


Thanks for your excellent explanation.

I implemented what you did and works perfectly.


Best regards,

FC

Rank: #86
Solution

Hello again Fábio,

In terms of performance, I would suggest for you to use Aggregates whenever is possible. The OutSystems Platform has optimizations for Aggregates, such as optimizations for the number of fields to be retrieved (based on the fields that you are actually using).

If you do this Aggregate inside a Data Action, then the Platform can't optimize it. So I suggest to use an Aggregate in this scenario.


Hope that this helps you!


Kind regards,

Rui Barradas

Rui,


I understand what you are saying.

I'll do it.


Thank you so much.


Have a nice day!

FC