import CSV provide a blank choice for the field match

import CSV provide a blank choice for the field match

bfarkasbfarkas Posts: 181Questions: 48Answers: 0

I am working through the import process for my table using the example here:
https://editor.datatables.net/examples/extensions/import.html

I have this up and working as is, but am looking to add a blank selection to the select dropdowns that are used to match the import fields.
The goal is that if the user is bringing a doc in that has extra fields or no data for some of the fields of the table, they can select the blank choice and on import the field would just remain empty for each record imported.

Bonus points to be able to auto-match fields if there is an option that matches perfectly...any ideas for either?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,461Questions: 26Answers: 4,975
    Answer ✓

    This thread might help you get started.

    Kevin

  • bfarkasbfarkas Posts: 181Questions: 48Answers: 0

    Thanks Kevin! That did the trick, I had searched a couple of times and never came across that.

    Now onto trying to see if i can auto select if names match.
    Thanks again!

  • kthorngrenkthorngren Posts: 21,461Questions: 26Answers: 4,975

    I had searched a couple of times and never came across that.

    I knew that thread was there and it took a few tries for me to find :smile:

    auto select if names match.

    Please post your solution for this. Sounds like a good idea.

    Kevin

  • KhanBouphaKhanBoupha Posts: 1Questions: 0Answers: 0
    edited July 2020

    This is what I do to map csv fields to the editor fields. Also provide a means to default to blank if no match found. The match is case insensitive by way of toLowerCase function. Perhaps someone will have a more efficient approach. In the meantime, hope this helps.

        function selectColumns(editor3, csv, header) {
            var selectEditor = new $.fn.dataTable.Editor();
            var fields = editor3.order();
            var csvFields = [];
            //csv field names must be in same order as editor3
            csvFields[0] = "FirstName";
            csvFields[1] = "LastName";
            csvFields[2] = "Salary";
            csvFields[3] = "City";
    
            var hfields = [];  //convert headers to all lowercase
            for (var i = 0; i < header.length; i++) {
                hfields[i] = header[i].toLowerCase();
            }
    
            header.push("");  //provide default blank
            for (var i = 0; i < fields.length; i++) {
                var field = editor3.field(fields[i]);
    
                var c = "";
                var a = hfields.indexOf(field.name().toLowerCase());
                if (a == -1) a = hfields.indexOf(csvFields[i].toLowerCase());
                if (a >= 0) c = header[a];
    
                selectEditor.add({
                    label: field.label(),
                    name: field.name(),
                    type: 'select',
                    options: header,
                    def: c
                });
            }
    
  • bfarkasbfarkas Posts: 181Questions: 48Answers: 0

    Hi KhanBoupha,
    I'm giving a modifed verison of this a try and it seems to be working, but when I leave a field set to the added 'none', it enters data of a number, in my case 3, which I'm guessing is the value of the multiset/none choice.Are you seeing that same behavior or do you have a solution for this?

This discussion has been closed.