How can I get the CSV import to convert the user-provided label into the ID value?

How can I get the CSV import to convert the user-provided label into the ID value?

tim-hitchins-ekkosensetim-hitchins-ekkosense Posts: 5Questions: 2Answers: 0

Very similar to https://datatables.net/forums/discussion/67056/import-csv-convert-code-to-id-using-table-ajax-json

I have a CSV with the following structure:

Name,Organisation
IME,Organisation 1

The value Organisation is a select field, with a number of options provided in the initial JSON load, with the options key:

    ....
    "options": {
        "modbusManufacturers.organisation": [
            {
                "value": 1,
                "label": "Organisation 1"
            },
            {
                "value": 10,
                "label": "Organisation 10"
            },
            {
                "value": 100,
                "label": "Organisation 100"
            },
            ...

But when I submit the CSV it is setting the field value to Organisation 1 and the field does not seem to convert it to an integer ID, it silently discards it because it's not a valid value.

How can I get the ID from the user-provided label using the already-populated options in the field?

Thanks,
Tim

This question has an accepted answers - jump to answer

Answers

  • tim-hitchins-ekkosensetim-hitchins-ekkosense Posts: 5Questions: 2Answers: 0

    The best I've done is to get it from the node element select options but that really doesn't feel right:

    for (let j = 0; j < csv.length; j++) {
        let userValue = csv[j][mapped];
        if ('select' == field.s.opts.type) {
            option = $(field.node()).find('option').filter(( function () {
                return $(this).text() == userValue;
            }));
            userValue = option.val();
        }
        field.multiSet(j, userValue);
    }
    
  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    Answer ✓

    The problem is that "Organisation 1" is not a value in the list of options - it is a label. Editor can't select values based on the value - it does it on the value, so I think your workaround is probably correct at the moment.

    Allan

  • tim-hitchins-ekkosensetim-hitchins-ekkosense Posts: 5Questions: 2Answers: 0
    edited April 2023

    Thanks.

    I've gone with the slightly more elegant solution of storing the initial load data and using that

    let loadData = {};
    table.on('init', (e, settings, json) => {
        loadData = json;
    });
    
                for (let j = 0; j < csv.length; j++) {
                    let userValue = csv[j][mapped];
                    if ('select' == field.s.opts.type) {
                        options = loadData.options[field.name()];
                        const match = options.find((element) => {
                            return element.label === userValue;
                        })
                        userValue = match.value;
                    }
                    field.multiSet(j, userValue);
                }
    

    I think the best solution would be for Editor to select values by label in this situation - although I understand that has edge cases when it's a select with numbers + the IDs don't line up.
    The middle ground would be a field.options() method so I wouldn't have to get them from the initial load.

Sign In or Register to comment.