Export Dynamic or User Defined Filtered Data From a K2 List View to EXCEL
K2 List View allows users to define custom filters to filter list data based on a user’s required filter criteria. The List View filter feature in K2 is called the User Filter and it is very useful as it provides the user the flexibility to choose their own filter criteria for list data. This is especially useful to create tabular-style reports in K2 where there is a requirement to filter the report by multiple criteria. The issue is that you sometimes want to do something in bulk with the filtered data.
This blog will show you how to perform operations on filtered data – for example exporting it to Excel. Other operations like bulk update or deletion can also be performed, but we focus on the Excel export.
We will use simple jQuery in the K2 SmartForm to select all the list items that are filtered by the User Filter (or quick search) with the intention to perform an action on the items using the K2 For-Each SmartForms loop on the “Selected” items.
Let’s look at an example
Consider the example order data below that is filtered based on a User Filter “Unit Price” and that is further refined by a quick text search. The latter makes this feature extremely useful, as it does not just rely on the User Filter.
To select the list items, which are a result of a filter or quick search, use the following jQuery script:
Transfer this script into a K2 datalabel which is marked as a literal to enable it to execute at runtime.
Now, perform any action on the selected items using the following “For- Each” condition; setting “Selected” on the condition selection.
Once the action is completed use the following JQuery script to deselect the list items:
This jQuery trick was used for Excel export and will only export the filtered list items. The clever bit is the ID fields of the “Selected” items are transferred into a data label and used as a filter in the Excel control, hence only exporting the filtered data!
Following is the Excel file export which contains data that are filtered based on the user filters –
This a useful feature to create a dynamic Excel export based on a user filter or data returned in a K2 List View based on a quick filter. We use this quite extensively in our solutions, as business users love to export data to Excel based on K2 filters to analyse it further. If you need any further assistance please get in touch.