Exporting DataTable to excel - number - javascript

$(document).ready(function () {
$.fn.dataTable.moment(lang == "en" ? 'DD/MM/YYYY' : 'DD.MM.YYYY');
categories_table = $('.datatable').DataTable({
order: [[1, 'desc']],
language: datatable_lang,
dom: 'Bfrtip',
buttons: [
{
extend: 'collection',
autoClose: 'true',
text: '<i class="download icon">',
tag: 'span',
buttons: ['csv', 'print', 'excel']
}
]
});
});
This gives nice looking format for price, like "8,00" as example. But when I export that to excel, I get "800" as value. And if number is "18,00", excel will show it as "1,800".
Is there any way to sort this out and have same values in excel as well? I try a lot thing but I didnt do it.
Sorry for my English
Thanks

Related

Datatable export to Excel Format Problems with random columns

i am trying to export data from a datatable to an excel file.
But everytime i use the excel button it changes the format of my numbers.
For example a 0,023 is copied to excel as a 23.
Is there a way to use the excel export button so that excel isnt changing the format?
The other things is that i use the datatable on different data from a DB. So the column isnt always the same which has to be formated. Even the User can select which column should be exported. So the Format sould be used.
So possible Datatable could look like:
A
B
C
abc
1
1,2
A
B
C
abc
1,2
dcf
Thx in advance.
<script type="text/javascript" language="javascript">
$(document).ready(function() {
var table = $('#tbl').DataTable( {
dom: "lfBptrip",
select: {
style: 'multi'
}
,
columnDefs: [
{
targets: 0,
className: 'noVis'
}
],
language: {
searchBuilder: {
button: 'Filter',
}
},
buttons:[
'searchBuilder',
{
extend: 'colvis',
columns: ':gt(0)',
collectionLayout: 'fixed columns',
text: 'Spaltenauswahl',
postfixButtons: [{
extend: 'colvisRestore',
text: 'Show All',
}, {
extend: 'colvisGroup',
text: 'Hide All',
hide: ':visible'
},
]
},
{
extend: 'copy',
text: 'Zwischenablage',
exportOptions: {
columns: ':visible'
}
},
{
extend: 'excelHtml5',
text: 'Excel',
exportOptions: {
columns: ':visible'
}
},
{
extend: 'pdf',
pageSize: 'LEGAL',
orientation: 'landscape',
text: 'PDF',
exportOptions: {
columns: ':visible'
}
}
],
colReorder: true,
scrollX: true,
});
});
</script>

Jquery datatable exporting to csv problem with other language

I have a Japanese characters in my table. If I use excel button for exporting, it works fine but when I do for CSV. The data or the texts is not correct anymore. Do anyone know how to fix this? Thanks.
Here is my table
And for exporting to CSV and excel
I just used the simple way of creating the buttons for export
buttons: [
{
extend: 'excel',
text: 'Export to Excel',
className: 'btn btn-default',
exportOptions: {
columns: ':not(.notexport)'
},},
{
extend: 'csv',
text: 'Export to CSV',
className: 'btn btn-default',
exportOptions: {
columns: ':not(.notexport)'
},}
],

DataTable limited rows

I have data in my SQLite table and it has 985 rows, so I used DataTable to organise (searching and export to Pdf, CSV...), the problem is that the DataTable is limited rows and just shows the first 200 rows!.
my code is
<script>
$(document).ready( function () {
$('#table_id').DataTable({
dom: 'B<"clear">lfrtip',
buttons: {
name: 'primary',
buttons: [ 'copy', 'csv', 'excel', 'pdf' ]
}
});
});
</script
the data is stored in table Sqlite
and in the HTML page:
so what's the solution with?
There is more than one option for this:
You can use the paging option and set it to false, or you can use the pageLength option to choose how many results you want to show on one page.
paging:
<script>
$(document).ready( function () {
$('#table_id').DataTable({
dom: 'B<"clear">lfrtip',
paging: false,
buttons: {
name: 'primary',
buttons: [ 'copy', 'csv', 'excel', 'pdf' ]
}
});
});
</script
pageLength:
<script>
$(document).ready( function () {
$('#table_id').DataTable({
"pageLength": 1000,
dom: 'B<"clear">lfrtip',
buttons: {
name: 'primary',
buttons: [ 'copy', 'csv', 'excel', 'pdf' ]
}
});
});
</script

Datatable rowCallback function after initialize

I have the below working code for data table to set cell color based on condition.
$(document).ready(function() {
// DataTable
var table = $('#example').DataTable({
/*
dom: 'Bfrtip',
buttons: ['excel',{
extend: 'pdfHtml5',
orientation: 'landscape',
pageSize: 'LEGAL' }],
"ordering": false,
language: {
search: "_INPUT_",
searchPlaceholder: "Search All Data"
} */
rowCallback: function(row, data, index) {
console.log(row)
if (data[12].split(';')[1] == "In Progress") {
$(row).find('td:eq(11)').addClass('color')
}
}
});
});
The above code works fine but if I remove the comments section which is to add export function, it doesn't work.
I tried reproducing the same issue you're facing. I've created a jsfiddle example, which is working nice and fine. The only thing I changed in your code is that I added a comma (,) before rowCallback, this way :
$('#example').DataTable({
dom: 'Bfrtip',
buttons: ['excel',{
extend: 'pdfHtml5',
orientation: 'landscape',
pageSize: 'LEGAL' }],
"ordering": false,
language: {
search: "_INPUT_",
searchPlaceholder: "Search All Data"
},
rowCallback: function(row, data, index) {
console.log(row)
if (data[12].split(';')[0] == "In Progress") {
$(row).find('td:eq(11)').addClass('color')
}
}
});
and Plus, do recheck that the files related to jQuery datatable are same as the files I've added in my jsfiddle , there is a possibility that you've added a file twice or you might be using an older version of Jquery. If the issue still persists, edit your question and add your html + the files related to jquery that you've initialized.

Change file export title in jquery datatables after page loads

I'm trying to dynamically change the export title for datatable file export. I found a solution including AngularJS which I'm not going to be able to use. Is there a solution just using jquery?
Here is where I initialize the buttons:
var oTable = $("#standards").DataTable({
dom: 'lBfrtip',
buttons: [
{
extend: 'csvHtml5',
title: titleFinal,
exportOptions: {
columns: ':visible'
}
},
{
extend: 'excelHtml5',
title: titleFinal,
exportOptions: {
columns: ':visible'
}
}
],
I have a function makeTitle that returns a string built from the input string. Here is where I try to change the title after clicking a link.
$('ul').on('click', 'a', function () {
titleFinal = makeTitle('XYZ Standards Due for Testing, ');
oTable.buttons = [
{
extend: 'csvHtml5',
title: titleFinal
},
{
extend: 'excelHtml5',
title: titleFinal
}
];
oTable
.search('XYZ')
.draw();

Categories

Resources