数据表,使用下拉值导出为excel [英] Datatables,Export to excel with dropdown values
问题描述
试图导出,但在excel文件中获取所有选项.有什么方法可以只获取选定的项目,而不是所有下拉选项?
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
}
}
}
}
这使用JavaScript来操纵 body
函数提供的 node
.
This uses JavaScript to manipulate the node
provided by the body
function.
在这种情况下,它返回与所选下拉选项关联的 value .因此,例如,如果您有以下内容:
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>
然后,电子表格将包含"some_value"而不是某些价值".
then the spreadsheet will contain "some_value" and not "Some Value".
如果要显示的文本(某些值"),请改用上面代码中的注释行.但是在那种情况下,您将需要一些额外的逻辑来替换占位符文本"Select Report".带有空白字符串.
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.
由于某种原因,我无法在此处使用jQuery选择器.我想使用 option:selected
,但是我无法使它正常工作-不知道为什么.但是纯JavaScript方法(上面)确实有效.
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.
这篇关于数据表,使用下拉值导出为excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!