简体   繁体   中英

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

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?

DataTables do actually have a columns.visible option (which also apply to columnDefs ). You can set the initial visibility in dtColumnDefs by (example) :

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

There is a notVisible() "shorthand". You can change the visibility at any time through a dtInstance (which you not seem to be using). Add a dt-instance attribute to the table:

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

Implement dtInstanceCallback this way:

$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

I have found out a simple solution

first append a field for example 'visibility' = true or false in the $scope.TableColumns array.

$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());
                }          
            });
       }
   });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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