php - How to show pictures in a table using jQuery DataTables -
is possible can show url images in data-table? returning data in array shown here in code , returning url image with...
before knew datatables using default table put table in <img src="">
how can use here?
<?php class basket_report{ function show_basket_report(){ $connect_mysql= @mysql_connect($server,$username,$passwor) or die ("connection failed!"); $mysql_db=mysql_select_db("gp15",$connect_mysql) or die ("could not connect database"); $query = "select * reportbasket notifyemployee='1'"; $result=mysql_query($query) or die("query failed : ".mysql_error()); $objects= array(); while($rows=mysql_fetch_array($result)) { $objects[]= $rows; } $data_set = "["; $count_rows = count($objects); $count = 1; foreach($objects $object){ $data_set .= "['". $object['reportid'] ."', '". $object['email'] ."', '". $object['date'] ."', '". $object['time'] ."', '". $object['basketid'] ."', '". $object['notifyemployee'] ."','". $object['replyedphoto'] ."']"; if($count != $count_rows){ $data_set .= ","; $count++; } } $data_set .= "]"; return $data_set; } ?>
<html> <head> <script src="js/jquery-1.11.1.js"></script> <!-- datatables css --> <link rel="stylesheet" type="text/css" href="datatables-1.10.7/media/css/jquery.datatables.css"> <!-- jquery --> <script type="text/javascript" charset="utf8" src="datatables-1.10.7/media/js/jquery.js"></script> <!-- datatables --> <script type="text/javascript" charset="utf8" src="datatables-1.10.7/media/js/jquery.datatables.js"></script> <script> $(document).ready(function() { $('#table_id').datatable( { "data": dataset, "columns": [ { "title": "report_id" }, { "title": "email" }, { "title": "date" }, { "title": "time"}, { "title": "basket_id"}, { "title": "notify_employee"}, { "title": "replyed_photo"} ] } ); } ); </script> </head> <body> <table id="table_id" class="display"> </table> <?php include('class_report_basket.php'); $basket_report = new basket_report(); ?> <script> var dataset= <?php echo $basket_report->show_basket_report(); ?>; </script> </body> </html>
you can use columns.render option define callback function render cell content based on data.
$('#example').datatable({ "ajax": { url: "/test/0", }, "columns": [ { "data": 0 }, { "data": 1, "orderable": false, "sortable": false, "render": function (data, type, row, meta){ // if data displayed if(type === "display"){ return '<img src="' + data + '">'; // otherwise, if search/filtering performed } else { return data; } } } ] });
note if use columns
define columns, must have entry in array every single column have in table (these can null
if don't specify options). alternatively, can use columndefs target specific columns.
see example below code , demonstration. please note, i'm using jquery mockjax plug-in sake of demonstrating ajax request, it's not needed production code.
$(document).ready(function () { // ajax emulation demonstration $.mockjax({ url: '/test/0', responsetime: 200, responsetext: { "data": [ [ "100x150", "http://placehold.it/100x150" ], [ "200x150", "http://placehold.it/200x150" ], [ "300x150", "http://placehold.it/300x150" ] ] } }); $('#example').datatable({ "ajax": { url: "/test/0", }, "columns": [ { "data": 0 }, { "data": 1, "orderable": false, "sortable": false, "render": function (data, type, row, meta){ if(type === "display"){ return '<img src="' + data + '">'; } else { return data; } } } ] }); });
<!doctype html> <html> <head> <meta charset="iso-8859-1"> <link href="//cdn.datatables.net/1.10.7/css/jquery.datatables.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="//cdn.datatables.net/1.10.7/js/jquery.datatables.min.js"></script> <script src="http://vitalets.github.com/x-editable/assets/mockjax/jquery.mockjax.js"></script> </head> <body> <table id="example" class="display" cellspacing="0" width="100%"> <thead> <tr> <th>name</th> <th>picture</th> </tr> </thead> </table> </body> </html>
Comments
Post a Comment