0
我需要上傳excel文件並從數組創建數據表。怎麼做?我在使postExcel返回時遇到問題。從laravel excel數組創建datatable
IM使用http://www.maatwebsite.nl/laravel-excel/docs和 http://datatables.net/
這裏是我的代碼:
public function postExcel(Request $request)
{
if($request->ajax()) {
if ($request->hasFile('file'))
{
$file = $request->file('file');
$filename = $file->getClientOriginalName();
//$file->getRealPath();
$path = $file->move(public_path(), $filename);
\Excel::load($path, function($reader){
$results = $reader->toArray();
});
print_r($results);die();
$json = array(
'status' => 'ok',
'array' => $results
);
}
return Response::json($json);
}
}
,這是視圖,JS那裏我需要生成表:
$(document).on("change", "#file-input", function() {
var formData = new FormData($('form.upload-excel')[0]);
$.ajax({
type: 'POST',
data: formData,
url: '/Excel/public/upload-excel',
cache: false,
contentType: false,
processData: false,
success: function(data){
if(data.status === 'ok') {
console.log(data.array);
$('#table').DataTable({
colReorder: true,
});
}
}
});
});
從'ajax'返回的數據看起來像什麼? – jonmrich