Tried to export but getting all options in excel file. is there any way to get only selected items instead of all dropdown options?
$('#example').DataTable( {
dom: 'Bfrtip',
columns: [
{ data: 'name' },
{ data: 'surname' },
{ data: 'position' },
{ data: 'office' },
{ data: 'salary' }
],
buttons: [
{
extend: 'excelHtml5',
exportOptions: { orthogonal: 'export' }
}
]
} );
You can use the following export options:
exportOptions: {
format: {
body: function ( inner, rowidx, colidx, node ) {
if ($(node).children("select").length > 0) {
// we are in a cell containing a "select" drop-down - so, get it:
var selectNode = node.firstElementChild;
var txt = selectNode.options[selectNode.selectedIndex].value;
//var txt = selectNode.options[selectNode.selectedIndex].text;
return txt;
} else {
return inner; // the standard cell contents
}
}
}
}
This uses JavaScript to manipulate the node
provided by the body
function.
In this specific case, it returns the value associated with the selected drop-down option. So, for example, if you have this:
<option value="some_value">Some Value</option>
then the spreadsheet will contain "some_value" and not "Some Value".
If you want the displayed text ("Some Value"), then use the commented-out line in the above code instead. But in that case you would need some extra logic to replace the placeholder text "Select Report" with a blank string.
For some reason, I was not able to use a jQuery selector here. I wanted to use option:selected
but I was not able to get that to work - not sure why. But the pure JavaScript approach (above) did work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.