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

2
  • 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. Commented Nov 29, 2017 at 9:08
  • What is the if?, I mean what is the condition exactly? Is it a dynamic scope value that changes somehow during runtime? Commented Nov 29, 2017 at 12:09

1 Answer 1

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)
  }
});
Sign up to request clarification or add additional context in comments.

4 Comments

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.
??? 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.
I am using in single page application. mongodb using. So what can I do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.