2

I am implementing datatable this table contain all row with one API hit. And I want to put condition in java script code. Data table creating through java script. I am sharing my code sample.

$scope.standardOptions = DTOptionsBuilder
   .fromFnPromise(R.all('----api call--').getList())
   .withDOM("<'dt-toolbar'<'col-xs-12 col-sm-6'f><'col-sm-6 col-xs-12 hidden-xs'l>r>" +
   "t" +
   "<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>")
   .withBootstrap();

$scope.standardColumns = [               
   DTColumnBuilder.newColumn('flightNo').withOption('defaultContent', '-'),
   DTColumnBuilder.newColumn('eta').renderWith(dataRendererETA).withOption('defaultContent', '-'),
   DTColumnBuilder.newColumn('etd').renderWith(dataRendererETA).withOption('defaultContent', '-'),        
];

API Call DATA

    {
    "_id": "101-87450458_2016_SEP",
    "flightNo": "087",
    "eta": {
          "$date": 1511868720000
          },
    "etd": {
         "$date": 1511875800000
        },
    }

I want to put if condition in second and third DtColumnBuilder. If either eta should print or etd.

I am new in datatables. How can I put condition.

DTColumnBuilder.newColumn('eta').renderWith(dataRendererETA).withOption('defaultContent', '-'), DTColumnBuilder.newColumn('etd').renderWith(dataRendererETA).withOption('defaultContent', '-'),

I want to display one at a time.

enter image description here

anatol
  • 791
  • 9
  • 16
Varun Sharma
  • 4,632
  • 13
  • 49
  • 103
  • What's wrong with an if statement? You can use unshift to push elements into the front of the array if you need to or push to put them on the end of the array and use an if to conditionally do that... not clear what the issue is really. – shaunhusain Nov 29 '17 at 09:08
  • What is the `if`?, I mean what is the condition exactly? Is it a dynamic scope value that changes somehow during runtime? – davidkonrad Nov 29 '17 at 12:09

1 Answers1

1

You can do it upon initialization :

$scope.standardColumns = [               
   DTColumnBuilder.newColumn('flightNo').withOption('defaultContent', '-')
]
if (condition) {
   $scope.standardColumns.push( DTColumnBuilder.newColumn('eta').renderWith(dataRendererETA).withOption('defaultContent', '-') )
} else {
   $scope.standardColumns.push( DTColumnBuilder.newColumn('etd').renderWith(dataRendererETA).withOption('defaultContent', '-') )
}

or you can create both and hide / show one of the columns using dtInstance based on a condition, for example in a $watch :

$scope.$watch(‘condition’, function(newVal, oldVal) {
  if (newVal) {
    $scope.dtInstance.DataTable.column(1).visible(true)
    $scope.dtInstance.DataTable.column(2).visible(false)
  } else {
    $scope.dtInstance.DataTable.column(1).visible(false)
    $scope.dtInstance.DataTable.column(2).visible(true)
  }
});
davidkonrad
  • 83,997
  • 17
  • 205
  • 265
  • Thanks for the answer. This data-table only showing 1000 record. If in my api have more than 1000 record then it will showing now data. How can I achieve more than 1000 record in table. – Varun Sharma Nov 30 '17 at 07:16
  • ??? There is no limit in DataTables for the number of rows, perhaps you have some limitation serverside or in your `R` service. I really cant help with that. – davidkonrad Nov 30 '17 at 07:21
  • I am using in single page application. mongodb using. So what can I do? – Varun Sharma Nov 30 '17 at 07:22
  • could you check once.. https://stackoverflow.com/questions/47568013/get-more-than-1000-row-in-the-data-table-in-angular-js – Varun Sharma Nov 30 '17 at 07:28