【问题标题】:How to get datatable visible columns Index for save states in database如何获取数据库中保存状态的数据表可见列索引
【发布时间】:2019-10-30 20:38:11
【问题描述】:

我想在单击按钮时获取总可见列名称以将状态保存在数据库中。

示例 - colName - Col1, Col2, Col3, Col4, Col8, Col9

在数据库中保存列名后,当用户返回该表时,他应该只能看到他保存在数据库中的那些列。

当用户返回时,显示列名的列 - Col1、Col2、Col3、Col4、Col8、Col9

我发现这些功能用于动态隐藏/显示列 -

显示列oTable.fnSetColumnVis(item, true,false);

隐藏列oTable.fnSetColumnVis(item, false,false);

结论

  1. 如何获取数据表中可见列名称的总列表?

  2. 当用户重新登录时,他应该只能看到他上次保存的那些列吗?

任何意见都会有所帮助,谢谢。

【问题讨论】:

    标签: jquery asp.net asp.net-mvc datatable datatables


    【解决方案1】:

    如何获取数据表中可见列的总列表?

    扩展@Aswin Kumar 的答案

    let dataTable = $('#example').DataTable({
      "columnDefs": [
        { "visible": false, "targets": [1] },
        { "visible": false, "targets": [4] },
        { "visible": false, "targets": [5] },
      ]
    });
    
    let result = dataTable.columns().visible().reduce((a, v, i) => v ? [...a, i] : a, [])
    
    console.log(result) // Gives you index list of all the visible columns
    

    当用户重新登录时,他应该只能获得那些索引 他上次保存的那个?

    这可能有点棘手。

    您需要将每个列值保存在数据库中,并在加载表时获取这些值并使用它们来显示列

    类似的东西

    table.columns( [ 0, 1, 2, 3 ] ).visible( false, false );
    

    更多Detail here

    编辑 1:

    获取所有可见的列名和索引。

    function get_visible_columns() {
        //console.log(table);
       var all_columns = table.settings().init().columns;
        console.log('all_columns', all_columns);
        var visible_columns = [];
        for (var i in all_columns) {
            if (table.column(all_columns[i].name + ':name').visible()) {
                visible_columns.push(all_columns[i].name);
                console.log("index: "+i);
            }
        }
    
        alert(visible_columns.join(', '));
    }
    

    索引将用于恢复所有可见列

    table.columns( [ 0, 1, 2, 3 ] ).visible( false, false );
    

    Working Fiddle

    【讨论】:

    • 嗨@MyTwoCents,我还在同一个表上实现了列重新排序,以便我想在数据库中保存列名期望列索引。您能否向我建议我们如何获得可见的列名数组以及如何在用户返回时恢复它?
    • 添加详细信息以获取列名
    【解决方案2】:

    使用DataTable.columns().visible()

    更新

    使用columns().every( fn )

    let dataTable = $('#example').DataTable({
      "colReorder": true,
      "columnDefs": [
        { "visible": false, "targets": [1] },
        { "visible": false, "targets": [4] },
        { "visible": false, "targets": [5] },
      ]
    });
    
    // let result = dataTable.columns().visible().reduce((a, v, i) => v ? [...a, i] : a, [])
    
    // console.log(result)
    
    dataTable.on('column-reorder', function() {
      console.clear();
      dataTable.columns().every(function(i) {
        this.visible() && console.log(i, this.header().innerHTML)
      });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    <link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet" />
    <link href="https://cdn.datatables.net/colreorder/1.5.1/css/colReorder.dataTables.min.css" rel="stylesheet" />
    <script src="https://cdn.datatables.net/colreorder/1.5.1/js/dataTables.colReorder.min.js"></script>
    
    
    <table id="example" class="display" style="width:100%">
      <thead>
        <tr>
          <th>Name</th>
          <th>Position</th>
          <th>Office</th>
          <th>Numero</th>
          <th>Start date</th>
          <th>Salary</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Tiger Nixon</td>
          <td>System Architect</td>
          <td>Edinburgh</td>
          <td>155555</td>
          <td>2011/04/25</td>
          <td>$320,800</td>
        </tr>
        <tr>
          <td>Garrett Winters</td>
          <td>Accountant</td>
          <td>Tokyo</td>
          <td>63</td>
          <td>2011/07/25</td>
          <td>$170,750</td>
        </tr>
        <tr>
          <td>Ashton Cox</td>
          <td>Junior Technical Author</td>
          <td>San Francisco</td>
          <td>1</td>
          <td>2009/01/12</td>
          <td>$86,000</td>
        </tr>
      </tbody>
    </table>

    【讨论】:

    • 我更新了这个问题,你能看看这个吗?我想要列名,而不是列索引,因为我已经在同一个表上实现了列重新排序功能,所以当用户重新排序列时,这会影响显示/隐藏列,所以我想要列名数组。
    猜你喜欢
    • 2021-02-10
    • 2011-01-03
    • 2015-05-27
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-05
    • 2013-03-22
    相关资源
    最近更新 更多