Import file button
Import file button
Looking for a way to insert it I found this and I adapted it for me, now if I click on the button the window opens I select the file and import it but nothing matters, I didn't understand where to map the file to be able to associate it with my table, and import the data, I would like to make sure if it is possible to do it, as I did before, I import the file but first check the data and then import, is it possible to do it? I wanted to import excel files but if you can't even csv are fine, can someone help me figure out how to do it?
Editor::make('upload')
->fields([
File::make('csv')
->label('Carica File:')
->ajax("function(files) {
Papa.parse(files[0], {
header: true,
skipEmptyLines: true,
complete: function (results) {
if ( results.errors.length ) {
LaravelDataTables['tickets-table-upload'].field('csv').error( 'CSV parsing error: '+ results.errors[0].message );
}
else {
LaravelDataTables['tickets-table-upload'].close();
$.fn.uploadCallback(LaravelDataTables['tickets-table-editor'], results.data, results.meta.fields)
}
}
});
}"),
]),
])
Button::raw()
->editor('upload')
->text('Importa')
->action("LaravelDataTables['tickets-table-upload'].create({title: 'Carica File'});"),
@push('scripts')
$.fn.uploadCallback = function (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: 'Importa',
buttons: 'Importa ' + csv.length + ' records',
message: 'Seleziona la colonna di cui desideri utilizzare i dati per ciascun campo.'
});
selectEditor.on('submitComplete', function (e, json, data, action) {
editor.create(csv.length, {
title: 'Conferma',
buttons: 'Salva',
message: 'Clicca il Salva pulsante per confermare l\'importazione di ' + csv.length + ' righe di dati. Facoltativamente, sovrascrivere il valore di un campo per impostare un valore comune facendo clic sul campo sottostanterighe di dati. Facoltativamente, sovrascrivere il valore di un campo per impostare un valore comune facendo clic sul campo sottostante.'
});
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]);
}
}
});
}
{{ $dataTable->scripts() }}
@endpush
This question has accepted answers - jump to:
Answers
I'm not sure I really understand your question I'm afraid - however:
Are you saying that CSV import is fine, but you now what to do Excel import?
That can be done, but you'd need to use an external library such as SheetJS to read the Excel file. I'm afraid we don't have a tutorial on how to use SheetJS - you'd need to refer to their documentation.
Allan
the popup to load the csv file works but it doesn't load anything, it doesn't import anything, currently it opens the window to select the csv file but when I select it nothing is imported
See if you get errors in the browser’s console.
Kevin
No unfortunately no error, the popup window opens to select the file I select it and it disappears and nothing happens I don't see any kind of message or error
We'd need a link to a page showing the issue so I can diagnose it. As you can see this example is working okay.
Allan