import CSV provide a blank choice for the field match

import CSV provide a blank choice for the field match

bfarkasbfarkas Posts: 127Questions: 33Answers: 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: 10,638Questions: 25Answers: 2,428
    Answer ✓

    This thread might help you get started.

    Kevin

  • bfarkasbfarkas Posts: 127Questions: 33Answers: 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: 10,638Questions: 25Answers: 2,428

    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 16

    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
                });
            }
    
Sign In or Register to comment.