Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I would like to do this :

testdata = [{"id":"58",...}]; // local object

$('#test').dataTable({
    "aaData": testdata,
    "aoColumns": [
        { "mDataProp": "id" }
    ] });

with the angular-datatables module. I have tried this :

Controller

$scope.dtOptions = DTOptionsBuilder.fromSource('[{"id": 1}]')
        .withDataProp('data')
        .withBootstrap()
        .withPaginationType('full_numbers');
$scope.dtColumns = [
        DTColumnBuilder.newColumn('id').withTitle('ID')
];

View

<table datatable="" dt-options="dtOptions" dt-columns="dtColumns" class="table table-striped table-bordered"></table>

But it doesn't work at all, and I get this error message :

DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see http://datatables.net/tn/7

Also, I can't use any Ajax options to get json data from an URL, because my angularjs project uses Express and the Rest API, so I get 401 not authorized error while trying to get my data with a GET/POST URL like "/api/data/getJsonData".

Any ideas ? Thanks.

share|improve this question
up vote 7 down vote accepted

You can't use .fromSource as it always will do an ajaxUrl request. But you can use .fromFnPromise() instead. Put your JSON into a function which returns an deferred.promise.

share|improve this answer

I had a similar problem to the OP and Mica's answer was very helpful in pointing me in the right direction. The solution I used was as follows:

var getTableData = function() {
    var deferred = $q.defer();
    deferred.resolve(myTableDataObject); 
    return deferred.promise;
};

Then use that in the DTOptionsBuilder:

$scope.dtOptions = DTOptionsBuilder.fromFnPromise(getTableData)
    .withPaginationType('full_numbers');

I'm fairly new to Angular, and indeed JavaScript, so there may well be a more elegant/correct way of doing this, but it worked for me.

share|improve this answer
    
HEllo. exactly what do you mean with myTableDataObject . Is the javascript object? – jasmo2 Sep 3 '16 at 3:58
    
@jasmo2 - yes, the data object you're trying to load into the table. So in the OP it would be testdata. This is from an old project, so I had to look it up again to remind myself and it looks like I made a mistake and should pass the function itself instead of calling it. I'll edit the answer accordingly, but feel free to modify it based on your experience. – danj1974 Sep 4 '16 at 20:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.