Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Use dataTables API on a dtInstance #824

Closed
jrdn91 opened this issue Jul 27, 2016 · 3 comments
Closed

Use dataTables API on a dtInstance #824

jrdn91 opened this issue Jul 27, 2016 · 3 comments

Comments

@jrdn91
Copy link

jrdn91 commented Jul 27, 2016

I can't seem to figure out how to do this.

I want to have a sort bar outside of the dataTables instance that will sort the columns in the dataTables instance when I click the button.

In the dataTables API I can do something like

var table = $('#example').DataTable();

table
    .column( '0:visible' )
    .order( 'asc' )
    .draw();

But with this directive I can't seem to figure out how I'm supposed to be able to do something like this. It seems when I add dt-instance="dtInstance" the dtInstance property in my controller is still an empty object so I can't call any dataTables methods on it.

@jrdn91
Copy link
Author

jrdn91 commented Jul 27, 2016

Okay so here's some more info. If I echo out the dtInstance variable to the page I see it fill up with the instance data, but trying to access it inside of a function I call via ng-click is just giving me the empty object.

$scope.dtInstance = {};
$scope.sortByTitle = function(){
    console.log($scope.dtOptions);
}

ng-click="sortByTitle()"

@jrdn91
Copy link
Author

jrdn91 commented Jul 28, 2016

Got it working with

$scope.dtInstance = {};
$scope.dtIntanceCallback = function (instance) {
    $scope.dtInstance = instance;
};

And in the template call it at the dt-instance directive like dt-instance="dtIntanceCallback"

@jrdn91 jrdn91 closed this as completed Jul 28, 2016
@vincilbishop
Copy link

@Jordan4jc How did you figure that out? You rock! Don't know how long I spent on this...and how many other people on this repo have given up and declared this was not possible.

I will make one note for anyone else that finds themselves here from Google...reference the dtInstance.DataTable without the (), e.g. dtInstance.DataTable.row() will work.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants