.data().toArray() returning empty array

.data().toArray() returning empty array

tokrumtokrum Posts: 6Questions: 1Answers: 0

I have successfully populated my table with data, but now I need to extract it back out again to do calculations and produce charts. Code:

function display_table(obj) {
var params = {
ajax: {
url: 'http://127.0.0.1:5000/api/v_0_0/json/8',
dataSrc: ''
},
columnDefs: obj,
dom: 'Bfipt',
buttons: ['copy', 'print', 'csv']
};

        //This is the DataTables.js call that initializes and displays the table
        var table = $('#example').DataTable(params);

        var o = table.column(0).data().toArray();

...etc.
When I alert(0), I get an empty array '[]' and o.length shows length of zero. My table is already displayed on my web page with the data in it, so I know it's in there!
What am I missing?

This question has an accepted answers - jump to answer

Answers

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49
    Answer ✓

    Have you tried it without '.toArray()'?

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    Also don't use alert! Use console.log.

    Allan

  • tokrumtokrum Posts: 6Questions: 1Answers: 0

    OK, using console.log, here is what I get for table.column(0).data():

    {"context":[{"oFeatures":{"bAutoWidth":true,"bDeferRender":false,"bFilter":true,"bInfo":true,"bLengthChange":true,"bPaginate":true,"bProcessing":false,"bServerSide":false,"bSort":true,"bSortMulti":true,"bSortClasses":true,"bStateSave":null},"oScroll":{"bCollapse":false,"iBarWidth":15,"sX":"","sXInner":"","sY":""},"oLanguage":{"fnInfoCallback":null,"oAria":{"sSortAscending":": activate to sort column ascending","sSortDescending":": activate to sort column descending","hungarianMap":{"sortAscending":"sSortAscending","sortDescending":"sSortDescending"}},"oPaginate":{"sFirst":"First","sLast":"Last","sNext":"Next","sPrevious":"Previous","_hungarianMap":{"first":"sFirst","last":"sLast","next":"sNext","previous":"sPrevious"}},"sEmptyTable":"No data available in table","sInfo":"Showing _START to END of TOTAL entries","sInfoEmpty":"Showing 0 to 0 of 0 entries","sInfoFiltered":"(filtered from MAX total entries)","sInfoPostFix":"","sDecimal":"","sThousands":",","sLengthMenu":"Show MENU entries","sLoadingRecords":"Loading...","sProcessing":"Processing...","sSearch":"Search:","sSearchPlaceholder":"","sUrl":"","sZeroRecords":"No matching records found","_hungarianMap":{"aria":"oAria","paginate":"oPaginate","emptyTable":"sEmptyTable","info":"sInfo","infoEmpty":"sInfoEmpty","infoFiltered":"sInfoFiltered","infoPostFix":"sInfoPostFix","decimal":"sDecimal","thousands":"sThousands","lengthMenu":"sLengthMenu","loadingRecords":"sLoadingRecords","processing":"sProcessing","search":"sSearch","searchPlaceholder":"sSearchPlaceholder","url":"sUrl","zeroRecords":"sZeroRecords"}},"oBrowser":{"bScrollOversize":false,"bScrollbarLeft":false,"bBounding":true,"barWidth":15},"ajax":{"url":"http://127.0.0.1:5000/api/v_0_0/json/8","dataSrc":""},"aanFeatures":[],"aoData":[],"aiDisplay":[],"aiDisplayMaster":[],"aIds":{},"aoColumns":[{"idx":0,"aDataSort":[0],"asSorting":["asc","desc"],"bSearchable":true,"bSortable":true,"bVisible":true,"_sManualType":null,"_bAttrSrc":false,"fnCreatedCell":null,"mData":"keyword","mRender":null,"nTh":{"jQuery1120014248317612098427":31},"nTf":null,"sClass":"","sContentPadding":"","sDefaultContent":null,"sName":"","sSortDataType":"std","sSortingClass":"sorting","sSortingClassJUI":"","sTitle":"Keyword","sType":"string","sWidth":"63px","sWidthOrig":null,"iDataSort":-1,"sCellType":"td","_hungarianMap":{"dataSort":"iDataSort","sorting":"asSorting","searchable":"bSearchable","sortable":"bSortable","visible":"bVisible","createdCell":"fnCreatedCell","data":"mData","render":"mRender","cellType":"sCellType","class":"sClass","contentPadding":"sContentPadding","defaultContent":"sDefaultContent","name":"sName","sortDataType":"sSortDataType","title":"sTitle","type":"sType","width":"sWidth"},"_setter":null,"targets":0,"title":"Keyword","data":"keyword"},{"idx":1,"aDataSort":[1],"bSearchable":true,"bSortable":true,"bVisible":true,"_sManualType":null,"_bAttrSrc":false,"fnCreatedCell":null,"mData":"datestring","mRender":null,"nTh":{"jQuery1120014248317612098427":36},"nTf":null,"sClass":"","sContentPadding":"","sDefaultContent":null,"sName":"","sSortDataType":"std","sSortingClass":"sorting","sSortingClassJUI":"","sTitle":"Datestring","sType":"string","sWidth":"72px","sWidthOrig":null,"iDataSort":-1,"sCellType":"td","_setter":null,"targets":1,"title":"Datestring","data":"datestring"},{"idx":2,"aDataSort":[2],"bSearchable":true,"bSortable":true,"bVisible":true,"_sManualType":null,"_bAttrSrc":false,"fnCreatedCell":null,"mData":"geo","mRender":null,"nTh":{"jQuery1120014248317612098427":41},"nTf":null,"sClass":"","sContentPadding":"","sDefaultContent":null,"sName":"","sSortDataType":"std","sSortingClass":"sorting","sSortingClassJUI":"","sTitle":"Geo","sType":"string","sWidth":"28px","sWidthOrig":null,"iDataSort":-1,"sCellType":"td","_setter":null,"targets":2,"title":"Geo","data":"geo"},{"idx":3,"aDataSort":[3],"bSearchable":true,"bSortable":true,"bVisible":true,"_sManualType":null,"_bAttrSrc":false,"fnCreatedCell":null,"mData":"kwcat","mRender":null,"nTh":{"jQuery1120014248317612098427":46},"nTf":null,"sClass":"","sContentPadding":"","sDefaultContent":null,"sName":"","sSortDataType":"std","sSortingClass":"sorting","sSortingClassJUI":"","sTitle":"Kwcat","sType":"string","sWidth":"44px","sWidthOrig":null,"iDataSort":-1,"sCellType":"td","_setter":null,"targets":3,"title":"Kwcat","data":"kwcat"},{"idx":4,"aDataSort":[4],"bSearchable":true,"bSortable":true,"bVisible":true,"_sManualType":null,"_bAttrSrc":false,"fnCreatedCell":null,"mData":"engine","mRender":null,"nTh":{"jQuery1120014248317612098427":51},"nTf":null,"sClass":"","sContentPadding":"","sDefaultContent":null,"sName":"","sSortDataType":"std","sSortingClass":"sorting","sSortingClassJUI":"","sTitle":"Engine","sType":"string","sWidth":"48px","sWidthOrig":null,"iDataSort":-1,"sCellType":"td","_setter":null,"targets":4,"title":"Engine","data":"engine"},{"idx":5,"aDataSort":[5],"bSearchable":true,"bSortable":true,"bVisible":true,"_sManualType":null,"_bAttrSrc":false,"fnCreatedCell":null,"mData":"matchtype","mRender":null,"nTh":{"jQuery1120014248317612098427":56},"nTf":null,"sClass":"","sContentPadding":"","sDefaultContent":null,"sName":"","sSortDataType":"std","sSortingClass":"sorting","sSortingClassJUI":"","sTitle":"Matchtype","sType":"string","sWidth":"74px","sWidthOrig":null,"iDataSort":-1,"sCellType":"td","_setter":null,"targets":5,"title":"Matchtype","data":"matchtype"},{"idx":6,"aDataSort":[6],"bSearchable":true,"bSortable":true,"bVisible":true,"_sManualType":null,"_bAttrSrc":false,"fnCreatedCell":null,"mData":"ppc_qualityscore","mRender":null,"nTh":{"jQuery1120014248317612098427":61},"nTf":null,"sClass":"","sContentPadding":"","sDefaultContent":null,"sName":"","sSortDataType":"std","sSortingClass":"sorting","sSortingClassJUI":"","sTitle":"Ppc_Qualityscore","sType":"string","sWidth":"120px","sWidthOrig":null,"iDataSort":-1,"sCellType":"td","_setter":null,"targets":6,"title":"Ppc_Qualityscore","data":"ppc_qualityscore"},{"idx":7,"aDataSort":[7],"bSearchable":true,"bSortable":true,"bVisible":true,"_sManualType":null,"_bAttrSrc":false,"fnCreatedCell":null,"mData":"ppc_avgpos","mRender":null,"nTh":{"jQuery1120014248317612098427":66},"nTf":null,"sClass":"","sContentPadding":"","sDefaultContent":null,"sName":"","sSortDataType":"std","sSortingClass":"sorting","sSortingClassJUI":"","sTitle":"Ppc_Avgpos","sType":"string","sWidth":"83px","sWidthOrig":null,"iDataSort":-1,"sCellType":"td","_setter":null,"targets":7,"title":"Ppc_Avgpos","data":"ppc_avgpos"},{"idx":8,"aDataSort":[8],"bSearchable":true,"bSortable":true,"bVisible":true,"_sManualType":null,"_bAttrSrc":false,"fnCreatedCell":null,"mData":"ppc_impressions","mRender":null,"nTh":{"jQuery1120014248317612098427":71},"nTf":null,"sClass":"","sContentPadding":"","sDefaultContent":null,"sName":"","sSortDataType":"std","sSortingClass":"sorting","sSortingClassJUI":"","sTitle":"Ppc_Impressions","sType":"string","sWidth":"116px","sWidthOrig":null,"iDataSort":-

  • tokrumtokrum Posts: 6Questions: 1Answers: 0

    and here's the rest:

    1,"sCellType":"td","_setter":null,"targets":8,"title":"Ppc_Impressions","data":"ppc_impressions"},{"idx":9,"aDataSort":[9],"bSearchable":true,"bSortable":true,"bVisible":true,"_sManualType":null,"_bAttrSrc":false,"fnCreatedCell":null,"mData":"ppc_clicks","mRender":null,"nTh":{"jQuery1120014248317612098427":76},"nTf":null,"sClass":"","sContentPadding":"","sDefaultContent":null,"sName":"","sSortDataType":"std","sSortingClass":"sorting","sSortingClassJUI":"","sTitle":"Ppc_Clicks","sType":"string","sWidth":"76px","sWidthOrig":null,"iDataSort":-1,"sCellType":"td","_setter":null,"targets":9,"title":"Ppc_Clicks","data":"ppc_clicks"},{"idx":10,"aDataSort":[10],"bSearchable":true,"bSortable":true,"bVisible":true,"_sManualType":null,"_bAttrSrc":false,"fnCreatedCell":null,"mData":"ppc_avgcpc","mRender":null,"nTh":{"jQuery1120014248317612098427":81},"nTf":null,"sClass":"","sContentPadding":"","sDefaultContent":null,"sName":"","sSortDataType":"std","sSortingClass":"sorting","sSortingClassJUI":"","sTitle":"Ppc_Avgcpc","sType":"string","sWidth":"83px","sWidthOrig":null,"iDataSort":-1,"sCellType":"td","_setter":null,"targets":10,"title":"Ppc_Avgcpc","data":"ppc_avgcpc"},{"idx":11,"aDataSort":[11],"bSearchable":true,"bSortable":true,"bVisible":true,"_sManualType":null,"_bAttrSrc":false,"fnCreatedCell":null,"mData":"ppc_spend","mRender":null,"nTh":{"jQuery1120014248317612098427":86},"nTf":null,"sClass":"","sContentPadding":"","sDefaultContent":null,"sName":"","sSortDataType":"std","sSortingClass":"sorting","sSortingClassJUI":"","sTitle":"Ppc_Spend","sType":"string","sWidth":"76px","sWidthOrig":null,"iDataSort":-1,"sCellType":"td","_setter":null,"targets":11,"title":"Ppc_Spend","data":"ppc_spend"},{"idx":12,"aDataSort":[12],"bSearchable":true,"bSortable":true,"bVisible":true,"_sManualType":null,"_bAttrSrc":false,"fnCreatedCell":null,"mData":"ppc_enrolled","mRender":null,"nTh":{"jQuery1120014248317612098427":91},"nTf":null,"sClass":"","sContentPadding":"","sDefaultContent":null,"sName":"","sSortDataType":"std","sSortingClass":"sorting","sSortingClassJUI":"","sTitle":"Ppc_Enrolled","sType":"string","sWidth":"93px","sWidthOrig":null,"iDataSort":-1,"sCellType":"td","_setter":null,"targets":12,"title":"Ppc_Enrolled","data":"ppc_enrolled"}],"aoHeader":[[{"unique":true},{"unique":true},{"unique":true},{"unique":true},{"unique":true},{"unique":true},{"unique":true},{"unique":true},{"unique":true},{"unique":true},{"unique":true},{"unique":true},{"unique":true}]],"aoFooter":[],"oPreviousSearch":{"bCaseInsensitive":true,"sSearch":"","bRegex":false,"bSmart":true,"_hungarianMap":{"caseInsensitive":"bCaseInsensitive","search":"sSearch","regex":"bRegex","smart":"bSmart"}},"aoPreSearchCols":

  • tokrumtokrum Posts: 6Questions: 1Answers: 0

    and the last of it:

    [{"bCaseInsensitive":true,"sSearch":"","bRegex":false,"bSmart":true},{"bCaseInsensitive":true,"sSearch":"","bRegex":false,"bSmart":true},{"bCaseInsensitive":true,"sSearch":"","bRegex":false,"bSmart":true},{"bCaseInsensitive":true,"sSearch":"","bRegex":false,"bSmart":true},{"bCaseInsensitive":true,"sSearch":"","bRegex":false,"bSmart":true},{"bCaseInsensitive":true,"sSearch":"","bRegex":false,"bSmart":true},{"bCaseInsensitive":true,"sSearch":"","bRegex":false,"bSmart":true},{"bCaseInsensitive":true,"sSearch":"","bRegex":false,"bSmart":true},{"bCaseInsensitive":true,"sSearch":"","bRegex":false,"bSmart":true},{"bCaseInsensitive":true,"sSearch":"","bRegex":false,"bSmart":true},{"bCaseInsensitive":true,"sSearch":"","bRegex":false,"bSmart":true},{"bCaseInsensitive":true,"sSearch":"","bRegex":false,"bSmart":true},{"bCaseInsensitive":true,"sSearch":"","bRegex":false,"bSmart":true}],"aaSorting":[[0,"asc"]],"aaSortingFixed":[],"asStripeClasses":["odd","even"],"asDestroyStripes":[],"sDestroyWidth":"","aoRowCallback":[],"aoHeaderCallback":[],"aoFooterCallback":[],"aoDrawCallback":[{},{"sName":"sc"},{"sName":"information"},{"sName":"pagination"}],"aoRowCreatedCallback":[],"aoPreDrawCallback":[],"aoInitComplete":[],"aoStateSaveParams":[],"aoStateLoadParams":[],"aoStateLoaded":[],"sTableId":"example","nTable":{"jQuery1120014248317612098427":14},"nTHead":{},"nTFoot":null,"nTBody":{},"nTableWrapper":{},"bDeferLoading":false,"bInitialised":true,"aoOpenRows":[],"sDom":"Bfipt","searchDelay":null,"sPaginationType":"simple_numbers","iStateDuration":7200,"aoStateSave":[],"aoStateLoad":[],"oSavedState":null,"oLoadedState":null,"sAjaxSource":null,"sAjaxDataProp":"data","bAjaxDataGet":true,"jqXHR":{"readyState":1},"fnServerData":null,"aoServerParams":[],"sServerMethod":"GET","aLengthMenu":[10,25,50,100],"iDraw":1,"bDrawing":false,"iDrawError":-1,"iDisplayLength":10,"_iDisplayStart":0,"_iRecordsTotal":0,"_iRecordsDisplay":0,"bJUI":false,"oClasses":{"sTable":"dataTable","sNoFooter":"no-footer","sPageButton":"paginate_button","sPageButtonActive":"current","sPageButtonDisabled":"disabled","sStripeOdd":"odd","sStripeEven":"even","sRowEmpty":"dataTables_empty","sWrapper":"dataTables_wrapper","sFilter":"dataTables_filter","sInfo":"dataTables_info","sPaging":"dataTables_paginate paging","sLength":"dataTables_length","sProcessing":"dataTables_processing","sSortAsc":"sorting_asc","sSortDesc":"sorting_desc","sSortable":"sorting","sSortableAsc":"sorting_asc_disabled","sSortableDesc":"sorting_desc_disabled","sSortableNone":"sorting_disabled","sSortColumn":"sorting_","sFilterInput":"","sLengthSelect":"","sScrollWrapper":"dataTables_scroll","sScrollHead":"dataTables_scrollHead","sScrollHeadInner":"dataTables_scrollHeadInner","sScrollBody":"dataTables_scrollBody","sScrollFoot":"dataTables_scrollFoot","sScrollFootInner":"dataTables_scrollFootInner","sHeaderTH":"","sFooterTH":"","sSortJUIAsc":"","sSortJUIDesc":"","sSortJUI":"","sSortJUIAscAllowed":"","sSortJUIDescAllowed":"","sSortJUIWrapper":"","sSortIcon":"","sJUIHeader":"","sJUIFooter":"","_hungarianMap":{}},"bFiltered":false,"bSorted":false,"bSortCellsTop":false,"oInit":{"ajax":{"url":"http://127.0.0.1:5000/api/v_0_0/json/8","dataSrc":""},"columnDefs":[{"data":"keyword","targets":0,"mData":"keyword"},{"targets":0,"title":"Keyword","sTitle":"Keyword"},{"data":"datestring","targets":1,"mData":"datestring"},{"targets":1,"title":"Datestring","sTitle":"Datestring"},{"data":"geo","targets":2,"mData":"geo"},{"targets":2,"title":"Geo","sTitle":"Geo"},{"data":"kwcat","targets":3,"mData":"kwcat"},{"targets":3,"title":"Kwcat","sTitle":"Kwcat"},{"data":"engine","targets":4,"mData":"engine"},{"targets":4,"title":"Engine","sTitle":"Engine"},{"data":"matchtype","targets":5,"mData":"matchtype"},{"targets":5,"title":"Matchtype","sTitle":"Matchtype"},{"data":"ppc_qualityscore","targets":6,"mData":"ppc_qualityscore"},{"targets":6,"title":"Ppc_Qualityscore","sTitle":"Ppc_Qualityscore"},{"data":"ppc_avgpos","targets":7,"mData":"ppc_avgpos"},{"targets":7,"title":"Ppc_Avgpos","sTitle":"Ppc_Avgpos"},{"data":"ppc_impressions","targets":8,"mData":"ppc_impressions"},{"targets":8,"title":"Ppc_Impressions","sTitle":"Ppc_Impressions"},{"data":"ppc_clicks","targets":9,"mData":"ppc_clicks"},{"targets":9,"title":"Ppc_Clicks","sTitle":"Ppc_Clicks"},{"data":"ppc_avgcpc","targets":10,"mData":"ppc_avgcpc"},{"targets":10,"title":"Ppc_Avgcpc","sTitle":"Ppc_Avgcpc"},{"data":"ppc_spend","targets":11,"mData":"ppc_spend"},{"targets":11,"title":"Ppc_Spend","sTitle":"Ppc_Spend"},{"data":"ppc_enrolled","targets":12,"mData":"ppc_enrolled"},{"targets":12,"title":"Ppc_Enrolled","sTitle":"Ppc_Enrolled"}],"dom":"Bfipt","buttons":["copy","print","csv"],"sDom":"Bfipt"},"aoDestroyCallback":[],"oInstance":{"selector":"#example","context":{"location":{"href":"http://127.0.0.1:5000/api/v_0_0/start?product=Thrive&report=Name1","origin":"http://127.0.0.1:5000","protocol":"http:","host":"127.0.0.1:5000","hostname":"127.0.0.1","port":"5000","pathname":"/api/v_0_0/start","search":"?product=Thrive&report=Name1","hash":""},"jQuery1120014248317612098427":1},"length":1,"internal":{}},"sInstance":"example","iTabIndex":0,"nScrollHead":null,"nScrollFoot":null,"aLastSort":[{"src":0,"col":0,"dir":"asc","index":0,"type":"string"}],"oPlugins":{},"rowId":"DT_RowId","renderer":null,"iInitDisplayStart":-1,"_rowReadObject":true,"nHolding":null,"nTableReinsertBefore":{},"_buttons":[{"inst":{"c":{"name":"main","tabIndex":0,"dom":{"container":{"tag":"div","className":"dt-buttons"},"collection":{"tag":"div","className":"dt-button-collection"},"button":{"tag":"a","className":"dt-button","active":"active","disabled":"disabled"},"buttonLiner":{"tag":"span","className":""}}},"s":{"dt":{"context":[null],"selector":{"rows":null,"cols":null,"opts":null},"ajax":{"__dt_wrapper":true}},"buttons":[{"conf":{"className":"buttons-copy buttons-html5","exportOptions":{},"fieldSeparator":"\t","fieldBoundary":"","header":true,"footer":false,"namespace":".dt-button-0"},"node":{"jQuery1120014248317612098427":15},"inserter":{"length":1},"buttons":[],"inCollection":false,"collection":null},{"conf":{"className":"buttons-print","title":"*","message":"","exportOptions":{},"header":true,"footer":false,"autoPrint":true,"customize":null,"namespace":".dt-button-1"},"node":{"jQuery1120014248317612098427":18},"inserter":{"length":1},"buttons":[],"inCollection":false,"collection":null},{"conf":{"className":"buttons-csv buttons-html5","filename":"*","extension":".csv","exportOptions":{},"fieldSeparator":",","fieldBoundary":"\"","escapeChar":"\"","charset":null,"header":true,"footer":false,"namespace":".dt-button-2"},"node":{"jQuery1120014248317612098427":21},"inserter":{"length":1},"buttons":[],"inCollection":false,"collection":null}],"listenKeys":"","namespace":"dtb0"},"dom":{"container":{"0":{},"length":1}}},"name":"main"}],"_drawHold":false,"oAjaxData":{}}],"length":0,"selector":{"rows":null,"cols":null,"opts":null},"ajax":{"__dt_wrapper":true}}

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49

    That is what I would expect to see when viewing the DataTable variable as all of that is configuration. Not really sure why you would be getting that for columns().data()

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49

    Now I know you will get all of that when messing around in DataTables Live

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    Yeah - the console.log there is showing the entire contents of the API instance.

    @tokrum - Your table appears to have no data in it, hence why the API is getting you an empty array.

    If you link to the page showing the issue I'd be happy to take a look and offer some help.

    Allan

  • tokrumtokrum Posts: 6Questions: 1Answers: 0

    Thanks @allan! It's not a public site, but I have attached the entire code of the page. As you will see, the table really does have data in it, because the data gets displayed on the screen. Could there be an issue with the ajax attribute of the configuration retrieving the data asynchronously, while the rest of the code, including my call to .data() continues to execute? I didn't think so, because I got the same result when I inserted a long delay between the configuration and the .data() call. Also there was nothing in the DT documentation about needing a callback function after invoking $(#example).DataTable(params).

    The ajax call to my /getcols endpoint returns this:
    [{'data': 'keyword', 'targets': 0},
    {'targets': 0, 'title': 'Keyword'},
    {'data': 'datestring', 'targets': 1},
    {'targets': 1, 'title': 'Datestring'},
    {'data': 'geo', 'targets': 2},
    {'targets': 2, 'title': 'Geo'},
    {'data': 'kwcat', 'targets': 3},
    {'targets': 3, 'title': 'Kwcat'},
    {'data': 'engine', 'targets': 4},
    {'targets': 4, 'title': 'Engine'},
    {'data': 'matchtype', 'targets': 5},
    {'targets': 5, 'title': 'Matchtype'},
    {'data': 'ppc_qualityscore', 'targets': 6},
    {'targets': 6, 'title': 'Ppc_Qualityscore'},
    {'data': 'ppc_avgpos', 'targets': 7},
    {'targets': 7, 'title': 'Ppc_Avgpos'},
    {'data': 'ppc_impressions', 'targets': 8},
    {'targets': 8, 'title': 'Ppc_Impressions'},
    {'data': 'ppc_clicks', 'targets': 9},
    {'targets': 9, 'title': 'Ppc_Clicks'},
    {'data': 'ppc_avgcpc', 'targets': 10},
    {'targets': 10, 'title': 'Ppc_Avgcpc'},
    {'data': 'ppc_spend', 'targets': 11},
    {'targets': 11, 'title': 'Ppc_Spend'},
    {'data': 'ppc_enrolled', 'targets': 12},
    {'targets': 12, 'title': 'Ppc_Enrolled'},
    {'data': 'date', 'targets': 13},
    {'targets': 13, 'title': 'Date'}]

    The call to my /json/8 endpoint returns this:
    [{"keyword":"kaiser medicare","datestring":"7\/26\/15","geo":"Northern California","kwcat":"Brand Product Known","engine":"Google","matchtype":"Exact","ppc_qualityscore":10,"ppc_avgpos":1,"ppc_impressions":34,"ppc_clicks":8,"ppc_avgcpc":2.06,"ppc_spend":16.48,"ppc_enrolled":0},{"keyword":"kaiser medicare","datestring":"8\/2\/15","geo":"Northern California","kwcat":"Brand Product Known","engine":"Google","matchtype":"Exact","ppc_qualityscore":10,"ppc_avgpos":1,"ppc_impressions":32,"ppc_clicks":3,"ppc_avgcpc":1.54,"ppc_spend":4.62,"ppc_enrolled":0},{"keyword":"kaiser medicare","datestring":"8\/9\/15","geo":"Northern California","kwcat":"Brand Product Known","engine":"Google","matchtype":"Exact","ppc_qualityscore":10,"ppc_avgpos":1,"ppc_impressions":48,"ppc_clicks":13,"ppc_avgcpc":1.51,"ppc_spend":19.63,"ppc_enrolled":1},{"keyword":"kaiser medicare","datestring":"8\/16\/15","geo":"Northern California","kwcat":"Brand Product Known","engine":"Google","matchtype":"Exact","ppc_qualityscore":10,"ppc_avgpos":1,"ppc_impressions":65,"ppc_clicks":20,"ppc_avgcpc":1.2,"ppc_spend":24.09,"ppc_enrolled":1},{"keyword":"kaiser medicare","datestring":"8\/23\/15","geo":"Northern California","kwcat":"Brand Product Known","engine":"Google","matchtype":"Exact","ppc_qualityscore":10,"ppc_avgpos":1,"ppc_impressions":61,"ppc_clicks":17,"ppc_avgcpc":1.56,"ppc_spend":26.6,"ppc_enrolled":1},{"keyword":"kaiser medicare","datestring":"8\/30\/15","geo":"Northern California","kwcat":"Brand Product Known","engine":"Google","matchtype":"Exact","ppc_qualityscore":10,"ppc_avgpos":1,"ppc_impressions":43,"ppc_clicks":6,"ppc_avgcpc":3.18,"ppc_spend":19.06,"ppc_enrolled":2},{"keyword":"kaiser medicare","datestring":"9\/6\/15","geo":"Northern California","kwcat":"Brand Product Known","engine":"Google","matchtype":"Exact","ppc_qualityscore":10,"ppc_avgpos":1,"ppc_impressions":47,"ppc_clicks":11,"ppc_avgcpc":1.1,"ppc_spend":12.07,"ppc_enrolled":0},{"keyword":"kaiser medicare","datestring":"9\/13\/15","geo":"Northern California","kwcat":"Brand Product Known","engine":"Google","matchtype":"Exact","ppc_qualityscore":10,"ppc_avgpos":1,"ppc_impressions":51,"ppc_clicks":13,"ppc_avgcpc":1.17,"ppc_spend":15.16,"ppc_enrolled":1}]

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    $('#example').html(data);

    This is not a valid thing to do with DataTables. You are directly changing the HTML and DataTables doesn't (and can't) know that that has happened. You would need to initialise the DataTable after this point, or (I would suggest) use the API to add the rows.

    See this FAQ.

    Allan

  • tokrumtokrum Posts: 6Questions: 1Answers: 0
    edited June 2016

    @allan,
    OK, I understand that and will change it. However, that's not where I'm having the problem that is the subject of this question. The problem is occurring when the page first loads and the DT is initialized:

    function display_table(obj) {
                var params = {
                        ajax: {
                        url: 'http://127.0.0.1:5000/api/v_0_0/json/8',
                        dataSrc: ''
                    },
                    columnDefs: obj,
                    dom: 'Bfipt',
                    buttons: ['copy', 'print', 'csv']
                };
                //console.log(params);
                //This is the DataTables.js call that initializes and displays the table
                var table = $('#example').DataTable(params);
                
                var o = table.column(0).data();
    

    The weird results I originally asked about happen before the invalid code you pointed out is ever invoked... unless you are suggesting that the mere existence of this code is responsible?

    Please give this another look and help if you can. Many thanks!

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    Thanks for the code. You are ajax loading the data, which is the "issue". Keep in mind what the A in ajax stands for - asynchronous. The data hasn't finished loading when your var o = ... line executes! Hence there is no data in the table.

    Use initComplete to wait for the data to load.

    Allan

This discussion has been closed.