csv upload issue
csv upload issue

Hi Alan,
I tried to upload the file for you to check using debugger but it kept showing me uploading so I will just paste my code here.
when I tried to upload csv like this:
serial_number,date_purchased,
23938,2019-08-13,
23939,2019-08-14,
23940,2019-08-15,
23941,2019-08-16,
datatable editors can show me how many rows in the csv file correctly(in the several pop-up windows), however, after I finally confirm to upload, each existing record in datatables will be doubled. nothing in the csv has been uploaded to datatable editor.
Please take a look and let me know how to fix the problem.(how to upload what's in the csv instead of doubling the current data) and which part I can put code to submit the csv data to backend via ajax.
Thank you in advance for your help.
html code:
_<table id="example" class="table bg-white table-hover dt-responsive display" style="width:100%">
<thead>
<tr>
<th>SERIAL NUMBER</th>
<th>SERIAL CODE</th>
<th>STATUS</th>
<th>DATE PURCHASED</th>
</tr>
</thead>
</table>_
js part code:
_<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.6.3/papaparse.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="./js/dataTables.editor.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/select/1.3.0/js/dataTables.select.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/plug-ins/1.10.19/sorting/datetime-moment.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script>
// Use a global for the submit and return data rendering in the examples.
// Don't do this outside of the Editor examples!
var editor;
// Display an Editor form that allows the user to pick the CSV data to apply to each column
function selectColumns ( editor, csv, 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: header,
def: header[i]
} );
}
selectEditor.create({
title: 'Map CSV fields',
buttons: 'Import '+csv.length+' records',
message: 'Select the CSV column you want to use the data from for each field.'
});
selectEditor.on('submitComplete', function (e, json, data, action) {
// Use the host Editor instance to show a multi-row create form allowing the user to submit the data.
editor.create( csv.length, {
title: 'Confirm import',
buttons: 'Submit',
message: 'Click the <i>Submit</i> button to confirm the import of '+csv.length+' rows of data. Optionally, override the value for a field to set a common value by clicking on the field below.'
} );
for ( var i=0 ; i<fields.length ; 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] );
console.log(csv[j][mapped]);
}
}
} );
}
$(document).ready(function() {
// Regular editor for the table
editor = new $.fn.dataTable.Editor( {
ajax: "url",
table: "#example",
fields: [ {
label: "Serial Code:",
name: "serial_number"
}, {
label: "Purchase Date:",
name: "date_purchased"
},
]
} );
// Upload Editor - triggered from the import button. Used only for uploading a file to the browser
var uploadEditor = new $.fn.dataTable.Editor( {
fields: [ {
label: 'Drag and Drop',
name: 'csv',
type: 'upload',
ajax: function ( files ) {
// Ajax override of the upload so we can handle the file locally. Here we use Papa
// to parse the CSV.
Papa.parse(files[0], {
header: true,
skipEmptyLines: true,
complete: function (results) {
if ( results.errors.length ) {
console.log( results );
uploadEditor.field('csv').error( 'CSV parsing error: '+ results.errors[0].message );
}
else {
uploadEditor.close();
selectColumns( editor, results.data, results.meta.fields );
}
}
});
}
} ]
} );
var table =$('#example').DataTable( {
dom: 'Bfrtip',
ajax: "url",
columns: [
{ data: 'serial_number' },
{ data: 'serial_code' },
{ data: "status" },
// { data: "office" },
{ data: "date_purchased" }
// { data: "salary", render: $.fn.dataTable.render.number( ',', '.', 0, '$' ) }
],
select: true,
buttons: [
// { extend: 'create', editor: editor },
// { extend: 'edit', editor: editor },
{ extend: 'remove', editor: editor },
// {
// extend: 'csv',
// text: 'Export CSV',
// className: 'btn-space',
// exportOptions: {
// orthogonal: null
// }
// },
{
text: 'Import CSV',
action: function () {
uploadEditor.create( {
title: 'CSV Import New Inventory Items'
} );
}
},
// {
// extend: 'selectAll',
// className: 'btn-space'
// },
// 'selectNone',
]
} );
$('#filter-expr').on('keyup',function(){
table.search($(this).val()).draw();
});
} );
</script>_
Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Answers
I don't see anything immediately wrong. Could you link to a page showing the issue so we can attempt to debug it please.
Allan
Hello Allan:
you can use this link to debug.
there are 32 records in the datatable and when I upload a csv with 200 records, it shows 64 records in total after I upload.
http://id.cast-soft.com/Protofrontend/admin-inventory-debug.html
Also please let me know which part I should write ajax to sync data with backend.
Thank you,
Thanks for the link! I've just tried it and get to the
Submit
form - It makes an Ajax request to the server (/moa/api/jsonExample
) with the data that I would expect it to submit.However, the server is returning nothing at that point (not even a status code as far as I can see - the connection just terminates) and Editor shows its
A system error has occurred
message.Reloading the table shows the table still has 32 entries.
Have I done something wrong, or is the server in a "funny" state at the moment? Perhaps something is shown in its error logs?
Allan
Hi Allan,
Thank you for your help.
I think I have figured it out. the problem happens when I try to merge the php code with the framework I am using currently.
but it works now. Thank you again