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?
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?
Share Improve this question asked Feb 16, 2018 at 6:15 Nithin MohanNithin Mohan 77017 silver badges36 bronze badges2 Answers
Reset to default 5DataTables 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());
}
});
}
});
}