简体   繁体   English

jQuery DataTable - 隐藏列问题

[英]jQuery DataTable - Hide Column Issue

I am working on a project that is using jQuery DataTables empowered theme (Smart Admin). 我正在开发一个使用jQuery DataTables授权主题(Smart Admin)的项目。 I find the tool a great client side solution specially to filter results without coming back to the server. 我发现该工具是一个很好的客户端解决方案,专门用于过滤结果而无需返回服务器。

The table fields we show are only part of which there is in the database due to the fact there is not enough space. 由于没有足够的空间,我们显示的表字段只是数据库中的一部分。 The issue is that the export feature then only includes these limited columns. 问题是导出功能只包含这些有限的列。 I red that if we have a hidden column that too will get exported (ex: csv, xls, pdf) etc. 如果我们有一个隐藏的列也会被导出(例如:csv,xls,pdf等)

I tried to hie a field and that didn't work out for me. 我试图打过一个场地,这对我来说没有用。 I be obliged if you could point out to what I am doing wrong. 如果你能指出我做错了什么,我有责任。

I referenced the following article: Data Table Example - Hidden Columns 我引用了以下文章: 数据表示例 - 隐藏列

There were no console errors and the rest of the functionality (filtering, exporting works well). 没有控制台错误和其他功能(过滤,导出工作正常)。

How ever my concern is that we might be having an bug, didn't update to latest. 我的担心是我们可能有一个bug,没有更新到最新版本。 I think we have v1.10. 我想我们有v1.10。

  # @fn           setup: -> {{{
  # @brief        Sets up DataTable plugin with the help of the @$_tableId provided.
  setupDataTables: ->
    self = @

    @setupCheckboxes() if @$_bulkControls.length > 0

    @$_dataTables = $(@$_tables).dataTable
      "columnDefs": [
        {
          "targets": [1, 2, 3, 4],
          "visible": false
        }
      ],
      "sDom": "<'dt-toolbar'<'col-xs-12 col-sm-6'f><'col-sm-6 col-xs-6 hidden-xs'T>r>t<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-sm-6 col-xs-12'p>>"
      "oTableTools": {
        "aButtons": [
          "copy",
          {
            "sExtends": "csv",
            "sTitle": @generateDocumentName()
          },
          {
            "sExtends": "xls",
            "sTitle": @generateDocumentName()
          },
          {
            "sExtends": "pdf",
            "sTitle": @generateDocumentName(),
            "sPdfMessage": "SmartAdmin PDF Export",
            "sPdfSize": "letter"
          },
          {
            "sExtends": "print",
            "sMessage": "Generated by SmartAdmin <i>(press Esc to close)</i>"
          }
        ],
        "sSwfPath": "<%= asset_path('copy_csv_xls_pdf.swf') %>"
      },
      "autoWidth" : true,
      "preDrawCallback" : ->
        # Initialize the responsive datatables helper once.
        if !self.$_responsiveHelperDatatableTabletools
          self.$_responsiveHelperDatatableTabletools = new ResponsiveDatatablesHelper($('#datatable_tabletools'), self.$_breakpointDefinition)
      ,
      "rowCallback" : (nRow) ->
        self.$_responsiveHelperDatatableTabletools.createExpandIcon(nRow)
      ,
      "drawCallback" : (oSettings) =>
        @$_responsiveHelperDatatableTabletools.respond()

        # Setup bulk control containers if requiredkCo
        @setupBulkControls() if @$_bulkControls.length > 0

    $("#datatable_tabletools thead th input[type=text]").on 'keyup change', ->
      self.$_dataTables
        .column( $(this).parent().index()+':visible' )
        .search( this.value )
        .draw()


  # end of setup: -> }}}

Thanks in advance and wish you guys a wonderful week end. 在此先感谢,祝你们周末愉快。

So what you want is that the extra columns cannot be shown in the UI (because of real estate limitations) 所以你想要的是额外的列不能在UI中显示(因为房地产限制)
However, you want these extra columns to be present in the exported csv. 但是,您希望这些额外的列出现在导出的csv中。

What datatable does is that the hidden columns are present in the table HTML but are marked display:none. 数据表的作用是隐藏列存在于表HTML中,但标记为display:none。 This is because the idea of hidden columns is that they do not show up anywhere but they are available so that it can be searched. 这是因为隐藏列的想法是它们不会显示在任何地方,但它们是可用的,以便可以搜索它。

If you want to achieve this, you need to write a custom jQuery which will pick out all the data in the columns and then flush it into csv. 如果你想实现这一点,你需要编写一个自定义jQuery,它将挑选出列中的所有数据,然后将其刷新到csv中。

That being said, i think it would be advisable if you create a new response type for your controller method to handle csv and seperate the export columns from the display columns. 话虽这么说,我认为如果为控制器方法创建一个新的响应类型来处理csv并从显示列中分离导出列是明智的。 In other words 换一种说法

respond_to |format| do 
html: <something> 
json: <something> 
csv:@model.to_csv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM