Dynamic columns and user-defined data-source
Opened this issue · 3 comments
Greetings.
To warm up to easygrid, with the help of the instructions on this page (https://github.com/tudor-malene/Easygrid) and details from this issue (#53): I was able to successfully make use of easygrid with jqgrid that reads rows from dataSourceType of 'list'.
Now I want to take it one step further. My requirements is that: the data is going to come from a non SQL source (i.e. an API) and the count and names of columns will be dynamic (i.e. vary on user request).
(1) Can this be handled via dataSourceType of 'custom'?
(1.1) If yes, then how will I configure/handle the dynamic columns?
(2) Do I need to create my own data-source for it (i.e. a Service class that implements the methods - generateDynamicColumns, verifyGridConstraints, list, countRows, etc)?
(2.1) If yes, can you provide any hint/example for this i.e. Service class that implements these methods?
(2.2) When using a custom data-source, In the controller, how do I configure the grid details? e.g.
def dummyGrid = {
dataSourceType '???' --> Will this be 'custom' or name of my service class or ???
context '???'
columns { ???}
...
I have looked at this example (https://github.com/tsuyo/grails-easygrid-sample/tree/master/grails-app) which makes use of a custom dataSourceType but has fixed known columns.
Any help/hint will be highly appreciated.
Hi,
Before I can make a recommendation, here is a very important question:
-
How dynamic is the actual data coming from the APIs? ( are there x total fields , and each user will see a different subset of them? or is the data radically different and unpredictable each time?)
If the former, then you can just create a grid with all the columns for each API and only show the proper columns at runtime.Regarding the datasource, it will depend on the question, but it's very likely you will have to create your own Datasource service class, which is quite easy.
( you can check out ListDatasourceService or GormDatasourceService. For minimal functionality, you will have to implement the "list" - which returns a list of Objects(or Maps) - and "countRows" methods )
If you do this, you would basically translate the calls from easygrid to the API you are exposing .
Hi, thanks for your prompt response. To answer your question, the data will be different and unpredictable each time along with the number of columns. I have started to look at the the list/gorm data source services as well as initialization process in general. Just can't help wondering - if the "custom" data source supported one more closure called 'dataColumns' - which would allow the user to return a map/list of columns, that would just solve my problem.
Well, easygrid is mainly designed for a static number of columns.
There were indeed multiple requests for dynamic columns.
Here is the reason why the 'dataColumns' cannot be in the datasource ( at least in the current architecture) . There are 2 stages for rendering a grid:
- generating the actual javascript & html code
- responding to the requests from that javascript code with the actual grid data ( that must be in sync with the generated code from 1)
The datasource is responsible only for the second stage.
So, there is no nice way in the current version to generate a grid on the fly based on a datasource.
What you could do though, if you decide to use the current version of easygrid, is create a main grid with let's say 10 columns of each of the types you know you will support. ( you can do this programmatically using the "beforeApplyingColumnRules" closure of easygrid , where you can add the columns), and based on the API you want to expose, you can configure at runtime which columns to show .
OR,
There is another way of doing this( which is not documented ).
You can register ad-hoc grids ( check out https://github.com/tudor-malene/Easygrid/blob/master/grails-app/taglib/org/codehaus/groovy/grails/plugins/easygrid/EasygridTagLib.groovy#L44 ).
Basically, in your scenario you would:
- when the user visits the page with the grid, you would read the API, generate the GridConfig object, register it via EasygridInitService.initializeGrid using a unique name that you would use when you generate the taglib.
Hope this helps