CSV Import - Include "none" or "not applicable" in column select dropdown

CSV Import - Include "none" or "not applicable" in column select dropdown

ahaighahaigh Posts: 6Questions: 2Answers: 1

Hi,

I'm wondering if it is possible to include a "none" or "not applicable" in the CSV column select dropdown when importing a CSV file. I've gone over the examples and documentation both for Editor and papa parse and I don't think it is possible right now.

If I use the example provided, this would be as though one were uploading names, position, etc. but without salary data to be entered in manually, later, or not at all. It currently forces the user to select a column header from the csv file, and one can then set those values to zero on the next popup. In the instance where there isn't a corresponding column it would be great to have the ability to select "none" and then have the column value default to null or zero on the next popup. See picture below.

Thanks again!

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    That is something you will need to add to the code. The minimum you will need to add whatever option(s) you want to the options assigned in line 17 of the example. I did this:

    function selectColumns ( editor, csv, header ) {
        options = [''].concat(header)
    
        var selectEditor = new $.fn.dataTable.Editor();
        var fields = editor.order();
    
        for ( var i=0 ; i<fields.length ; i++ ) {
            var field = editor.field( fields[i] );
    
            selectEditor.add( {
                label: field.label(),
                name: field.name(),
                type: 'select',
                options: options,  //header,
                def: header[i]
            } );
    

    I believe that is all I did to allow for a blank option to be selected. Then the user can choose a default value. I think the field.multiSet( j, csv[j][mapped] ); in line 41 will come back as undefined in the case of not selecting a column. You may or may not need to handle this. In my case it works fine without doing anything extra.

    Kevin

  • ahaighahaigh Posts: 6Questions: 2Answers: 1
    edited August 2019

    Hi Kevin. Thanks for the prompt response. Much appreciated.

    Following your logic I've added a "None" option, but my syntax was slightly different than above.

    header = header.concat('None');

    When I don't select a column, it comes back with the value of 5, which I assume is the index number of the selected option. To your point, when I log

    field.multiSet(j, csv[j][mapped]);

    the parameter multiValues is equal to 5 so provided that can be set to zero I should be all set. I will keep at it and post if/when I get it sorted out.

  • ahaighahaigh Posts: 6Questions: 2Answers: 1
    Answer ✓

    Here's what I ended up doing:

            for ( var i=0 ; i<fields.length - less ; i++ ) { 
                var field = editor.field( fields[i] );
                var mapped = data[ field.name() ];
    
                for ( var j=0 ; j<csv.length ; j++ ) {
                    field.multiSet(j, csv[j][mapped]);
                }
    
            }
    

    where "less" is a variable that counts out the number of fields that are set to "none". Not perfect as it assumes that only the later fields in the list will be set to "none", but in my case this should be true most of the time. If there is a way to specify which fields get dropped or to set the defaults to zero (or null) for all set to none, that would be great, but this works for now.

    Thanks again.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Good idea for improving the import. I should have thought of that when originally writing that example. I've added it to my list of things to do :).

    Allan

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    @ahaigh thanks for sharing your solution.

    Kevin

  • ahaighahaigh Posts: 6Questions: 2Answers: 1

    Thanks guys!

  • bfarkasbfarkas Posts: 181Questions: 48Answers: 0

    I am trying to follow this, but all the fields that get set to none/blank pass a 3 or null to the form when submitted. Does anyone have a solution so that when none/blank option is selected noting is passed to the fields and the standard defaults for the fields apply?

  • bfarkasbfarkas Posts: 181Questions: 48Answers: 0

    Also, how did you set your less variable?

This discussion has been closed.