数据表-导出字段输入内外的值以及选择字段的值 [英] Datatables - Export values inside and outside the field input and value of the select field

查看:108
本文介绍了数据表-导出字段输入内外的值以及选择字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们看看谁能帮助我解决这个问题.

let's see who can help me solve this problem.

我有几个带有JS datatables插件的表( https://datatables.net/)

I have several tables with the JS datatables plugin (https://datatables.net/)

我的问题是导出PDF和Excel中的数据.

My problem is in exporting the data in PDF and Excel.

我无法将输入或选择字段内的值导出到PDF或Excel(仅所选值)

I can not export to PDF or Excel the values that are inside the input or select fields (only the selected value)

我有几个表,其中有作为输入的列,另一列选择和另一列简单文本.如果您可以使用此插件,我想知道如何将所有这些值导出到Excel或PDF.到目前为止,我还没有得到它.

I have several tables where there are columns that are inputs, another column selects and another column simple text. I would like to know how I can do to export all these values to Excel or PDF, if you can with this plugin. So far I have not been able to get it.

以下是我的代码摘录,用于构建数据表:

Here a extract of my code to build the datatable:

var tabla_table = $('#table').DataTable({
    dom: 'Blfrtip',
    buttons: [{
            extend: 'collection',
            text: "<i class='fa fa-cog'></i>",
            buttons: [
              {
                  extend: 'pdfHtml5',
                  orientation: 'landscape',
                  customize: function ( doc ) {
                    doc.defaultStyle.fontSize = 10;  
                  },
                  exportOptions: {
                    columns: ':visible',
                    columns: ':not(.no-print)',
                  /*  format: {
                    body: function ( data, row, column, node, sValue, nTr, type ) {
                      //
                      //check if type is input using jquery
                    //  console.log('data val: ' + $(data).val() );
                      console.log('data: ' + data );
                      console.log('row: ' + row );
                      console.log('nTr: ' + nTr );
                      console.log('node: ' + node );
                      console.log('type: ' + type );
                      /*if( $(data).is("input") ){
                        return data;     
                      }else{
                        return $(data).val();  

                      }

                      }
                    }*/
                    //columns: [4, 8, 9, 10, 11, 12, 13, 14]
                  }
              },{
                  extend: 'excel',
                  orientation: 'landscape',
                  exportOptions: {
                    columns: ':visible',
                    columns: ':not(.no-print)',
                    format: {
                    body: function ( data, row, column, node ) {
                      //
                      //check if type is input using jquery
                      //console.log('PRUEBA: ' + $(data).val() );
                      if( $(data).is("input") ){
                        return data;     
                      }else{
                        return $(data).val();  

                      }

                      }
                    }
                    //columns: [4, 8, 9, 10, 11, 12, 13, 14]
                  }
                },{
                  text: 'Imprimir',
                  extend: 'print',
                  orientation: 'landscape',

                  exportOptions: {
                    columns: ':visible',
                    columns: ':not(.no-print)'
                  }
                },
               /* 'colvis'*/

            ]
        }

    ],.....

以PDF格式输出:

我希望我能提供足够的信息来解决此问题,如果可以解决的话.并且,如果需要更多信息,请随时告诉我.

I hope I could have provided enough information to resolve this, if it can be resolved. And if more information is needed, do not hesitate to tell me.

非常感谢您

推荐答案

var buttonCommon = {
    exportOptions: {
        format: {
            body: function(data, column, row, node) {
                if (row == 1) {
                    return $(data).is("div") ? $(data).text() : data
                }
                else  if (row == 4) {
                    return $(data).is("select") ? $(data).val() : data
                }
                else {
                    return $(data).is("input") ? $(data).val() : data
                }
            }
        }
    }
};
$(document).ready(function() {
    $('#Goal_tables').DataTable({
        dom: 'Bfrtip',
        "paging": !1,
        buttons: [$.extend(!0, {}, buttonCommon, {
            extend: "excel"
        })]
    })
});

这篇关于数据表-导出字段输入内外的值以及选择字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆