How to change dynamically csv datatype of fields

Answered
Emmanuel asked on February 22, 2023

Hi,
When I load a csv file (with the "Connect / to local CSV" option), one of the field is detected as numeric (probably because on the first lines, this field contains a numerical value). But some values of the field are alphanumeric so it shouldn't be considered as a measure (but is should be considered as string).
How can we change dynamically (after the file has been loaded) the type of a specific field (in the attached example, the field "MODELE_ETUDE" is considered as numeric, I would like to be able to change that to string type). This change should be possible throught the UI (not programmatically).
The problem of that behavior is when you put MARQUE on row, MODELE_ETUDE on columns and sum of VOLUME on VALUES, the values of MODEL_ETUDE are correctly shown on columns (I20, I30, 147, 159). But when you click on cell HYUNDAI/I20, the detail shows wrong column value for MODEL_ETUDE (20 instead of I20).
Regards

Attachments:
sample_immat.csv

8 answers

Public
Maksym Diachenko Maksym Diachenko Flexmonster February 24, 2023

Hello, Emmanuel!

Thank you for reaching out to us.

Flexmonster has the mapping feature for manually defining the type for a particular field. This can be done inside the MappingObject, a property of the DataSourceObject. By default, Flexmonster does not have a UI control for setting the types. Still, it is possible to implement a custom UI control for setting the types, which would require some development effort.

Based on your description, we suggest that it would be easier to programmatically set the types for certain fields:

dataSource: {
mapping: {
MODELE_ETUDE: {
type: "string"
}
}
}

It is possible to customize the Toolbar to include the mapping while connecting to local CSV. The new "Connect / To local CSV" handler can be implemented with Flexmonster's connectTo() API call, which clears the report and connects to a new data source. You are welcome to check the JSFiddle example: https://jsfiddle.net/flexmonster/xe4taL9z/
Also, check out or page about the Toolbar customization: https://www.flexmonster.com/doc/customizing-toolbar/ 

Please let us know if this approach would work for you.

Best Regards,
Maksym

Public
Maksym Diachenko Maksym Diachenko Flexmonster March 7, 2023

Hello, Emmanuel!

Hope you are doing well.
Our team would like to know if you had time to check our previous reply. Did the proposed solution to customize the "Connect / To local CSV" handler work for you?
Looking forward to hearing from you.

Best Regards,
Maksym

Public
Maksym Diachenko Maksym Diachenko Flexmonster March 14, 2023

Hi, Emmanuel!

We are wondering if you checked our reply. Our team is ready to assist you in implementing a mapping UI control if the mentioned approach with modifying the "Connect / To local CSV" handler does not fit your use case.
Looking forward to hearing your feedback.

Best Regards,
Maksym

Public
Emmanuel March 14, 2023

Hello Maksym,
I checked the answer but indeed, it is necessary to develop a mapping UI (as the CSV file content can change, it's not a fixed formated columns file).
Have you an example about the creation of this UI control ? As I can se, it would first get columns/types of the input file and propose a control to change the type of any of these columns then import definitely the file with the correct mapping.
Regards
Emmanuel

Public
Maksym Diachenko Maksym Diachenko Flexmonster March 16, 2023

Hello, Emmanuel!

Thank you for your reply.

Our team prepared an example illustrating the custom mapping UI control: https://jsfiddle.net/flexmonster/gyd42z6a/
For better understanding, we have described the most important parts of the code.

Retrieving all hierarchies

Firstly, you should get all available hierarchies to populate the view responsible for selecting types. This can be done with the getAllHierarchies() method, which should be wrapped into the reportcomplete event to ensure the data is already loaded.

Adding mapping inputs
After retrieving the hierarchies, the UI controls for mapping can be created. In our example, only string and number types can be set. However, you can add more types to your implementation and customize its appearance. 

Applying the mapping
For this purpose, we suggest resetting the report with modified mapping using the getReport and setReport. However, if you were using the local file, the report from getReport would contain the browseForFile: true property in the data source. With this property, the file dialog will be opened each time report is set. To avoid this behavior, you could use the workaround with the save method (see lines 55-64 in the example), which will convert the data from a file into the JSON array.

Feel free to contact us if any questions arise.

Best Regards,
Maksym

Public
Maksym Diachenko Maksym Diachenko Flexmonster March 23, 2023

Hello, Emmanuel!

Hope you are doing well.
Please let us know if you had time to check the provided example of a custom mapping UI control.
We are looking forward to hearing your feedback.

Best Regards,
Maksym

Public
Emmanuel March 24, 2023

Hi Maksym,
thank you very much for your help.
With the provided code example, we should be able to customize our GUI to enable this feature.
Best regards
Emmanuel

Public
Maksym Diachenko Maksym Diachenko Flexmonster March 24, 2023

Hi, Emmanuel!

Thank you for your reply.
We are glad to hear that the provided example was helpful.
Feel free to contact us if any other questions arise.

Best Regards,
Maksym

Please login or Register to Submit Answer