arrays - Using JQuery to create dataTable. What am I doing wrong? -


i have tried multiple ways work.

this import:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> </script>  <script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/jquery.datatables.min.js" ></script> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/jquery.datatables.min.css" /> 

this code

var myurl = "test.php"; $(document).ready(function() {     $('#ajax-content').datatable({         "ajax": myurl,         "columns": [             { "data": "id"},             { "data": "navn"},             { "data": "timestamp"},             { "data": "logged_in"},             { "data": "display_status"},             { "data": "logo"}         ]     }); }); 

and json

{     "2": {         "navn": "henry",         "timestamp": "1505222637",         "logged_in": "not online",         "display_status": "doesn't work",         "logo": "someurl"     },     "5": {         "navn": "diana",         "timestamp": "1505224159",         "logged_in": "online",         "display_status": "works",         "logo": "anotherurl"     } } 

its fine if can done without datatable. suspect problem when execute first segment of code. when try doing jquery.

i appreciate if can me come solution :(

updated: forgot post picture of error gave me

https://i.imgur.com/tqjpeqg.png

the jquery datatables plugin expects array, or array of objects, when populating table data. providing objects in json, format not expect—thereby causing plugin break.

there several options fix this:

  • update api endpoint return correct format.
  • manually map object array (underscore/lodash have useful utility functions, if ever need parse object manually)

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -