Error when Importing CSV Data

Error when Importing CSV Data

gh2mgh2m Posts: 63Questions: 16Answers: 0

Link to test case: http://jsfiddle.net/gen8zo5h/2/
Debugger code (debug.datatables.net):
Error messages shown: DataTables warning: table id=example - Requested unknown parameter 'Description' for row 5, column 1. For more information about this error, please see http://datatables.net/tn/4
Description of problem: I am trying to import a CSV file data into the dataTables but getting the above error. I looks correct after clicking the OK button couple of time. How do I stop getting that error? Also showing entry as one more that the data. I guess it counts the first (title) row as data.

Ultimately, what I want to do is to import the csv data into an existing dataTable (columns pre-defined) starting with the second row of the csv file. Is that possible with my code?

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    That's nice! I tried it with a CSV exported from this page - and it worked fine.

    What steps do I need to do to reproduce the error?

    Colin

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    You have this column config:

                            "columns": results.meta.fields.map(c => ({
                                "title": c,
                                "data": c,
                                "visible": c.toLowerCase() !== "id",
                                "default": ""
                            }
    

    Requested unknown parameter 'Description' for row 5, column 1

    Sounds like row 5 doesn't have all of the columns which would cause the above error. You can use defaultContent to handle the missing data. Change "default": "" to "defaultContent": "". If this doesn't help then, as Colin asked, provide the steps and CSV file needed to reproduce the error.

    Kevin

  • gh2mgh2m Posts: 63Questions: 16Answers: 0

    I did the same thing as Colin and it works. However after open and re-save that exported CSV file, without any changes, it causes the same issue as I have before.

  • gh2mgh2m Posts: 63Questions: 16Answers: 0

    Kevin, you suggestion of defaultConent did the trick. Thanks! Now back to the second part of the question: Showing 1 to x of x entries has one more than number of data rows. It seems to include the column title row as a data row. Anything I can do to get that corrected?

    Also, I would like to import/load a file into an existing dataTable instead using the first row as the title. How do I define the dataTable in that case?

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    Showing 1 to x of x entries has one more than number of data rows. It seems to include the column title row as a data row. Anything I can do to get that corrected?

    I don't see that. Using Colin's step when exporting the CSV from that example there is a title row but I don't see it in the table. You are using Papa Parse to import the CSV. Look at the docs if you want a different import behavior of the import. You have this:

    Papa.parse(file, {
                    "download": true,
                    "header": true,
    ....
    

    The header option docs state this:

    If true, the first row of parsed data will be interpreted as field names

    I would like to import/load a file into an existing dataTable instead using the first row as the title. How do I define the dataTable in that case?

    The columns.title option will set the Header title. You have this:

    "columns": results.meta.fields.map(c => ({
        "title": c,
    ....
    

    You will need to change this to whatever you want the title to be.

    Kevin

  • gh2mgh2m Posts: 63Questions: 16Answers: 0

    If you re-save the exported CSV file and try to import/load, you will see the show entry count has 1 more than the data row, which I think it counts the title row. I tried "header": false in Papa.parse, the function stops working because of following error:
    Uncaught TypeError: Cannot read property 'map' of undefined

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    If you re-save the exported CSV file and try to import/load, you will see the show entry count has 1 more than the data row,

    I opened the CSV and re-saved it, making no changes, and the table still show 57 rows. What are you opening the CSV file with? Have you looked at the saved CSV file in a test editor?

    Kevin

  • gh2mgh2m Posts: 63Questions: 16Answers: 0

    Wow. Maybe just me. I simply double clicked the exported file from Window Explorer. It opens in MS Excel. I then clicked File -> Save. It pops up a message "Some features in your workbook might be lost if you save it as CSV(Comma delimited). Do you want to keep using that format?", I just clicked Yes button. I am not sure which test editor are you referring to. I tried to open it with Notepad and resaved it, it works fine.

    So it has something to do with saving it as CSV in Excel. I never know that. My users are definitely going to use Excel to edit the file before loading/importing. Is there a way in Excel that will not cause that issue?

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770
    edited November 2020 Answer ✓

    I saved the file using Excel as a CSV and it imported as expected. You will need to investigate the differences between the two files. Use a text file or a file compare tool (your OS may have one builtin). Maybe there is something in Papa Parse that can help. Papa Parse is the source of the data for Datatables.

    Kevin

  • gh2mgh2m Posts: 63Questions: 16Answers: 0

    Thanks for your help Kevin!

  • gh2mgh2m Posts: 63Questions: 16Answers: 0

    BTW, what's the best way to write this imported rows into a SQL table? The back-end. May be I can just process the CSV file directly. Any direction would be appreciated.

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    Editor can be used for that - it also has this import functionality - see here. Otherwise, yep, you'd need some other server-side script to do that.

    Colin

This discussion has been closed.