简体   繁体   English

如何在angular-datatable中实现动态列隐藏?

[英]How to achieve dynamic column hiding in angular-datatable?

I have a following datatable sturcture 我有以下数据表结构

<table id="display" datatable="ng" dt-options="dtOptions" dt-column-defs="dtColumnDefs" class="table table-striped table-bordered hover" cellspacing="0" width="100%">
<thead>
    <tr>
        <th>INPUT TYPE</th>
        <th>INPUT NAME</th>
        <th>VALUE</th>
        <th>UNIT</th>
        <th ng-repeat="n in TableColumns | orderBy:'Name'">{{n.Name}}</th>
        <th class="noExport" style="width:50px">VIEW</th>
    </tr>
</thead>
<tbody>
    <tr ng-repeat="x in DisplayData">
        <td>{{x.Type}}</td>
        <td>{{x.Name}}</td>
        <td>{{x.Value}}</td>
        <td>{{x.Unit}}</td>
        <td ng-repeat="n in x.OptionData | orderBy:'Name'">{{n.Value}}</td>
        <td>
            <a ng-click="gotoLink(x)"><span class="glyphicon glyphicon-eye-open"></span></a>
        </td>
</tbody>

and the following is the angular code 以下是角度代码

$scope.TableColumns = [];
$scope.getAllData = function (item) {
   var DashboardData = item;
   resultService.getDataById(fetchData).then(function (response) {
       if (response.data.length > 0) {
           $scope.DisplayData = response.data;
           $scope.TableColumns = response.data[0].OptionData;
       }
   });
}

and response.data[0].OptionData looks like 和response.data [0] .OptionData看起来像

var result = [{OptionsId: 1, Name: "Pressure", Value: 10}
              {OptionsId: 2, Name: "Temperature", Value: 20}
              {OptionsId: 3, Name: "Humidity",Value: 30},.....];

How can I hide dynamically the columns from the result set $scope.TableColumns ,consider it has about 50 results but I want to display 10 and all other should hide on initial loading? 如何动态隐藏结果集中的$ scope.TableColumns列,考虑它有大约50个结果,但我想显示10个结果,而其他所有结果都应在初始加载时隐藏?

DataTables do actually have a columns.visible option (which also apply to columnDefs ). DataTable实际上确实具有columns.visible选项(这也适用于columnDefs )。 You can set the initial visibility in dtColumnDefs by (example) : 您可以通过dtColumnDefs设置初始可见性(例如):

DTColumnDefBuilder
  .newColumnDef(0)
  .withOption('data', 'office')
  .withTitle('office')
  .withOption('visible', false)

There is a notVisible() "shorthand". 有一个notVisible() “速记”。 You can change the visibility at any time through a dtInstance (which you not seem to be using). 您可以随时通过dtInstance (您似乎没有使用)来更改可见性。 Add a dt-instance attribute to the table: 在表中添加dt-instance属性:

<table datatable="ng"
   ...
   dt-instance="dtInstanceCallback">

Implement dtInstanceCallback this way: dtInstanceCallback以下方式实现dtInstanceCallback

$scope.dtInstanceCallback = function(instance) {
   $scope.dtInstance = instance
}

Now you can change column visibility on the fly by 现在,您可以即时更改列的可见性

$scope.dtInstance.DataTable.column(0).visible(true) //or false

demo -> http://plnkr.co/edit/ZXHisTJSU8tHgCLUEGP4?p=preview 演示-> http://plnkr.co/edit/ZXHisTJSU8tHgCLUEGP4?p=preview

I have found out a simple solution 我找到了一个简单的解决方案

first append a field for example 'visibility' = true or false in the $scope.TableColumns array. 首先在$ scope.TableColumns数组中附加一个字段,例如'visibility'= true或false。

$scope.TableColumns = [{OptionsId: 1, Name: "Pressure", Value: 10, visibility: true}
          {OptionsId: 2, Name: "Temperature", Value: 20,visibility: false}
          {OptionsId: 3, Name: "Humidity",Value: 30,visibility: true},.....];

then change the following section 然后更改以下部分

$scope.dtColumnDefs = [];
$scope.TableColumns = [];
$scope.getAllData = function (item) {
   var DashboardData = item;
   resultService.getDataById(fetchData).then(function (response) {
       if (response.data.length > 0) {
          $scope.DisplayData = response.data;
          $scope.TableColumns = response.data[0].OptionData;
          angular.forEach($scope.SiteTableColumns, function (v, k) {
                var index = 4 + k; //0-3 index value of columns manually set in the table and k+1 for array index
                  if (v.visibility == true) {
                     $scope.dtColumnDefs.push(DTColumnDefBuilder.newColumnDef(index).withTitle(v.Name));
                } else {
                    $scope.dtColumnDefs.push(DTColumnDefBuilder.newColumnDef(index).withTitle(v.Name).notVisible());
                }          
            });
       }
   });
}

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

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