jQuery UI Widgets › Forums › DataTable › Binding jqxDataTable to Remote data
Tagged: bind, binding, cellsrenderer, data, data table, datatable, jqxdatatable, php, remote, Remote data, rowdata
This topic contains 14 replies, has 3 voices, and was last updated by cje 10 years ago.
-
Author
-
I like the layout of Your demo jqxDataTable but I cant´t make it work to remote data (PHP).
Do You have any demo which han help me ?Hello cje,
We do not currently have any jqxDataTable PHP examples but we have a number of examples with jqxGrid and there is virtually no difference in the data binding part of both widgets. Please also check the jqxDataTable Remote Data demo, which shows how to bind to an external service.
Best Regards,
DimitarjQWidgets team
http://www.jqwidgets.com/Thank You for quick answer.
How can I put data into the table instead of “laptopdata” ?
<!DOCTYPE html>
<html lang=”en”>
<head>
<link rel=”stylesheet” href=”../../jqwidgets/styles/jqx.base.css” media=”screen” />
<link rel=”stylesheet” href=”../../jqwidgets/styles/jqx.arctic.css” media=”screen” />
<link rel=”stylesheet” href=”../../styles/jqx.apireference.css” media=”screen” />
<link rel=”stylesheet” href=”../../styles/site.css” media=”screen” /><script type=”text/javascript” src=”../../scripts/format.js”></script>
<script type=”text/javascript” src=”../../scripts/jquery-1.11.1.min.js”></script>
<script type=”text/javascript” src=”../../scripts/demos.js”></script>
<script type=”text/javascript” src=”../../scripts/demofunctions.js”></script>
<script type=”text/javascript” src=”../../jqwidgets/jqxcore.js”></script>
<script type=”text/javascript” src=”../../jqwidgets/jqxtabs.js”></script>
<script type=”text/javascript” src=”../../jqwidgets/jqxdata.js”></script>
<script type=”text/javascript” src=”../../jqwidgets/jqxdatatable.js”></script>
<script type=”text/javascript” src=”../../jqwidgets/jqxbuttons.js”></script>
<script type=”text/javascript” src=”../../jqwidgets/jqxscrollbar.js”></script>
<script type=”text/javascript” src=”../../jqwidgets/jqxlistbox.js”></script>
<script type=”text/javascript” src=”../../jqwidgets/jqxdropdownlist.js”></script>
<script type=”text/javascript” src=”../../jqwidgets/jqxmenu.js”></script>
<script type=”text/javascript” src=”../../jqwidgets/jqxresponse.js”></script><script type=”text/javascript”>
$(document).ready(function () {
//initDemo();var source =
{
datatype: “json”,
datafields: [
{ name: ‘CompanyName’, type: ‘string’},
{ name: ‘ContactName’, type: ‘string’},
{ name: ‘ContactTitle’, type: ‘string’},
{ name: ‘Address’, type: ‘string’},
{ name: ‘City’, type: ‘string’}
],
url: ‘data.php’,
cache: false
};var dataAdapter = new $.jqx.dataAdapter(source);
var itemsInCart = 0;
$(“#dataTable”).jqxDataTable(
{
width: 860,
source: dataAdapter,
sortable: true,
pageable: true,
pageSize: 3,
theme: ‘arctic’,
pagerButtonsCount: 5,
enableHover: false,
selectionMode: ‘none’,
rendered: function () {
$(“.buy”).jqxButton({ theme: ‘arctic’ });
$(“.buy”).click(function () {
itemsInCart++;
$(“.cart-top p”).html(itemsInCart + ” products”);
});
},
columns: [
{
text: ‘Products’, align: ‘left’, dataField: ‘model’,
// row – row’s index.
// column – column’s data field.
// value – cell’s value.
// rowData – rendered row’s object.
cellsRenderer: function (row, column, value, rowData) {
//var laptops = rowData.laptops;
var container = “<div>”;
//alert(laptops.length);
for (var i = 0; i < 4; i++) {
//var laptop = laptops[i];
var item = “<div style=’float: left; width: 210px; overflow: hidden; white-space: nowrap; height: 265px;’>”;
var image = “<div style=’margin: 5px; margin-bottom: 3px;’>”;
var imgurl = ‘../../images/l-18.gif’;
var img = ‘‘;
image += img;
image += “</div>”;var info = “<div style=’margin: 5px; margin-left: 10px; margin-bottom: 3px;’>”;
info += “<div><i>” + “res[i+1].ContactName” + “</i></div>”;
info += “<div>Price: $” + “laptop.price” + “</div>”;
info += “<div>RAM: ” + “laptop.ram” + “</div>”;
info += “<div>HDD: ” + “laptop.hdd” + “</div>”;
info += “<div>CPU: ” + “laptop.cpu” + “</div>”;
info += “<div>Display: ” + “laptop.display” + “</div>”;
info += “</div>”;var buy = “<button class=’buy’ style=’margin: 5px; width: 80px; left: -40px; position: relative; margin-left: 50%; margin-bottom: 3px;’>Buy</button>”;
item += image;
item += info;
item += buy;
item += “</div>”;
container += item;
}
container += “</div>”;
return container;
}
}]
});
});</script>
</head>
<body class=’default’><div id=”dataTable”></div>
</body></html>
Hello cje,
You should also implement the data table’s rendered callback function and the column cellsRenderer. These would be specific to your project, as is the case with the ones in the demo which are specific to the laptop data.
Best Regards,
DimitarjQWidgets team
http://www.jqwidgets.com/Now I can put the data into the table but I get 4 equal containers(columns) in one row. How can I make it 4 different ?
<script type=”text/javascript”>
$(document).ready(function () {
var source =
{
datatype: “json”,
datafields: [
{ name: ‘CompanyName’, type: ‘string’},
{ name: ‘ContactName’, type: ‘string’},
{ name: ‘ContactTitle’, type: ‘string’},
{ name: ‘Address’, type: ‘string’},
{ name: ‘City’, type: ‘string’}
],
url: ‘data.php’,
cache: false
};
var dataAdapter = new $.jqx.dataAdapter(source);
var itemsInCart = 0;$(“#dataTable”).jqxDataTable(
{
width: 860,
source: dataAdapter,
sortable: true,
pageable: true,
pageSize: 3,
theme: ‘arctic’,
pagerButtonsCount: 5,
enableHover: false,
selectionMode: ‘none’,
rendered: function () {
$(“.buy”).jqxButton({ theme: ‘arctic’ });
$(“.buy”).click(function () {
itemsInCart++;
$(“.cart-top p”).html(itemsInCart + ” products”);
});
},
columns: [
{
text: ‘Products’, align: ‘left’, dataField: ‘model’,
// row – row’s index.
// column – column’s data field.
// value – cell’s value.
// rowData – rendered row’s object.
cellsRenderer: function (row, column, value, rowData) {
var laptops = rowData;
//alert (laptops.CompanyName);
var container = “<div>”;
//alert(laptops.length);
for (var i = 0; i < 4; i++) {
//var laptop = laptops[i];
var item = “<div style=’float: left; width: 210px; overflow: hidden; white-space: nowrap; height: 265px;’>”;
var image = “<div style=’margin: 5px; margin-bottom: 3px;’>”;
var imgurl = ‘../../images/l-18.gif’;
var img = ‘‘;
image += img;
image += “</div>”;var info = “<div style=’margin: 5px; margin-left: 10px; margin-bottom: 3px;’>”;
info += “<div><i>” + “res[i+1].ContactName” + “</i></div>”;
info += “<div>Price: $” + laptops.ContactName + “</div>”;
info += “<div>RAM: ” + “laptop.ram” + “</div>”;
info += “<div>HDD: ” + “laptop.hdd” + “</div>”;
info += “<div>CPU: ” + “j” + “</div>”;
info += “<div>Display: ” + laptops.CompanyName + “</div>”;
info += “</div>”;var buy = “<button class=’buy’ style=’margin: 5px; width: 80px; left: -40px; position: relative; margin-left: 50%; margin-bottom: 3px;’>Buy</button>”;
item += image;
item += info;
item += buy;
item += “</div>”;
container += item;
}
container += “</div>”;
return container;
}
}]
});
});</script>
Hi cje,
The data table code you posted seems to be the one from the demo. We also do not know what your data is and cannot assist you. If you wish, post your data table with some sample data and remember to format your code by selecting it and pressing the
code
button in the toolbar.Best Regards,
DimitarjQWidgets team
http://www.jqwidgets.com/Hi !
I’m using Northwind as in your demo.<!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" media="screen" /> <link rel="stylesheet" href="../../jqwidgets/styles/jqx.arctic.css" media="screen" /> <link rel="stylesheet" href="../../styles/jqx.apireference.css" media="screen" /> <link rel="stylesheet" href="../../styles/site.css" media="screen" /> <script type="text/javascript" src="../../scripts/format.js"></script> <script type="text/javascript" src="../../scripts/jquery-1.11.1.min.js"></script> <script type="text/javascript" src="../../scripts/demos.js"></script> <script type="text/javascript" src="../../scripts/demofunctions.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxtabs.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxdatatable.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxbuttons.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxscrollbar.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxlistbox.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxdropdownlist.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxmenu.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxresponse.js"></script> <script type="text/javascript"> $(document).ready(function () { var source = { datatype: "json", datafields: [ { name: 'CompanyName', type: 'string'}, { name: 'ContactName', type: 'string'}, { name: 'ContactTitle', type: 'string'}, { name: 'Address', type: 'string'}, { name: 'City', type: 'string'} ], url: 'data.php', cache: false }; var dataAdapter = new $.jqx.dataAdapter(source); var itemsInCart = 0; $("#dataTable").jqxDataTable( { width: 860, source: dataAdapter, sortable: true, pageable: true, pageSize: 3, theme: 'arctic', pagerButtonsCount: 5, enableHover: false, selectionMode: 'none', rendered: function () { $(".buy").jqxButton({ theme: 'arctic' }); $(".buy").click(function () { itemsInCart++; $(".cart-top p").html(itemsInCart + " products"); }); }, columns: [ { text: 'Products', align: 'left', dataField: 'model', // row - row's index. // column - column's data field. // value - cell's value. // rowData - rendered row's object. cellsRenderer: function (row, column, value, rowData) { var laptops = rowData; //alert (laptops.CompanyName); var container = "<div>"; //alert(laptops.length); for (var i = 0; i < 4; i++) { //var laptop = laptops[i]; var item = "<div style='float: left; width: 210px; overflow: hidden; white-space: nowrap; height: 265px;'>"; var image = "<div style='margin: 5px; margin-bottom: 3px;'>"; var imgurl = '../../images/l-18.gif'; var img = '<img width=160 height=120 style="display: block;" src="' + imgurl + '"/>'; image += img; image += "</div>"; var info = "<div style='margin: 5px; margin-left: 10px; margin-bottom: 3px;'>"; info += "<div><i>" + "res[i+1].ContactName" + "</i></div>"; info += "<div>Price: $" + laptops.ContactName + "</div>"; info += "<div>RAM: " + "laptop.ram" + "</div>"; info += "<div>HDD: " + "laptop.hdd" + "</div>"; info += "<div>CPU: " + "j" + "</div>"; info += "<div>Display: " + laptops.CompanyName + "</div>"; info += "</div>"; var buy = "<button class='buy' style='margin: 5px; width: 80px; left: -40px; position: relative; margin-left: 50%; margin-bottom: 3px;'>Buy</button>"; item += image; item += info; item += buy; item += "</div>"; container += item; } container += "</div>"; return container; } } ] }); }); </script> </head> <body class='default'> <div id="dataTable"></div> </body> </html>
<?php
#Include the connect.php file
include(‘connect.php’);
#Connect to the database
//connection String
$connect = mysql_connect($hostname, $username, $password)
or die(‘Could not connect: ‘ . mysql_error());
//select database
mysql_select_db($database, $connect);
//Select The database
$bool = mysql_select_db($database, $connect);
if ($bool === False){
print “can’t find $database”;
}
// get data and store in a json array
$query = “SELECT * FROM Customers”;
$from = 0;
$to = 30;
$query .= ” LIMIT “.$from.”,”.$to;$result = mysql_query($query) or die(“SQL Error 1: ” . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$customers[] = array(
‘CompanyName’ => $row[‘CompanyName’],
‘ContactName’ => $row[‘ContactName’],
‘ContactTitle’ => $row[‘ContactTitle’],
‘Address’ => $row[‘Address’],
‘City’ => $row[‘City’]
);
}echo json_encode($customers);
?>`<?php
#Include the connect.php file
include(‘connect.php’);
#Connect to the database
//connection String
$connect = mysql_connect($hostname, $username, $password)
or die(‘Could not connect: ‘ . mysql_error());
//select database
mysql_select_db($database, $connect);
//Select The database
$bool = mysql_select_db($database, $connect);
if ($bool === False){
print “can’t find $database”;
}
// get data and store in a json array
$query = “SELECT * FROM Customers”;
$from = 0;
$to = 30;
$query .= ” LIMIT “.$from.”,”.$to;$result = mysql_query($query) or die(“SQL Error 1: ” . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$customers[] = array(
‘CompanyName’ => $row[‘CompanyName’],
‘ContactName’ => $row[‘ContactName’],
‘ContactTitle’ => $row[‘ContactTitle’],
‘Address’ => $row[‘Address’],
‘City’ => $row[‘City’]
);
}echo json_encode($customers);
?>Sorry forgot Code
Hi cje,
We cannot assist you because we do not know what part of the data you want to display and how you want to display it. This is a custom rendering and it is entirely up to you to implement it in cellsRenderer where you can access each row’s data in rowData.
Best Regards,
DimitarjQWidgets team
http://www.jqwidgets.com/How can I access each data in rowData ? I get the same data four times before I get the next.
And if I change the loop I only got one container/column in each row
Hi cje,
We suggest you take a careful look at the demo and the implemented functionality in it. Make sure your data is loaded in a similar manner to be able to construct a similar row template.
Best Regards,
DimitarjQWidgets team
http://www.jqwidgets.com/cje,
Here’s how I did it.
<div id="campaigns_table"></div> <!-- Campaign content javascript --> <script type="text/javascript" src="js/jqwidgets/jqxdata.js"></script> <script type="text/javascript" src="js/jqwidgets/jqxbuttons.js"></script> <script type="text/javascript" src="js/jqwidgets/jqxscrollbar.js"></script> <script type="text/javascript" src="js/jqwidgets/jqxdatatable.js"></script> <script type="text/javascript" src="js/jqwidgets/jqxdropdownlist.js"></script> <script type="text/javascript" src="js/jqwidgets/jqxlistbox.js"></script> <script type="text/javascript"> function contentReady() { // prepare the data var source = { dataType: "json", dataFields: [ { name: 'id', type: 'integer' }, { name: 'campaign_name', type: 'string' }, { name: 'total_sales', type: 'float' }, { name: 'status', type: 'string' } ], url: "campaigns/db_campaign_list.php" }; var dataAdapter = new $.jqx.dataAdapter(source, { formatData: function (data) { $.extend(data, { featureClass: "P", style: "full", username: "jqwidgets", maxRows: 50 }); return data; } } ); $("#campaigns_table").jqxDataTable({ theme: "<?php echo $widget_style; ?>", width: 750, pageable: true, pagerButtonsCount: 10, source: dataAdapter, columnsResize: true, sortable: true, showToolbar: true, altRows: true, filterable: true, filterMode: "advanced", columns: [ { text: 'ID', dataField: 'id', width: 50 }, { text: 'Campaign Name', dataField: 'campaign_name', width: 250 }, { text: 'Total Sales', dataField: 'total_sales', cellsFormat: 'c', width: 250 }, { text: 'Status', dataField: 'status' } ] }); $("#campaigns_table").on("rowDoubleClick", function (event) { var row = event.args.row; alert("You will be editing - " + row.campaign_name); }); } </script>
Here’s the data part from a PHP file.
<?php $list = array(); $list[] = array("id" => 32, "campaign_name" => "A Brand New Campaign", "total_sales" => 0.00, "status" => "Active"); $list[] = array("id" => 31, "campaign_name" => "2015 Griffin Ga PD", "total_sales" => 3420.00, "status" => "Active"); $list[] = array("id" => 30, "campaign_name" => "2015 York Co", "total_sales" => 475.00, "status" => "Active"); $list[] = array("id" => 29, "campaign_name" => "2015 Hampton County", "total_sales" => 511.00, "status" => "Active"); $list[] = array("id" => 28, "campaign_name" => "2015 Liberty County", "total_sales" => 12458.00, "status" => "Inactive"); $list[] = array("id" => 27, "campaign_name" => "2015 Barnwell County", "total_sales" => 25365.00, "status" => "Inactive"); $list[] = array("id" => 26, "campaign_name" => "2015 Calhoun Co", "total_sales" => 4582.00, "status" => "Inactive"); $list[] = array("id" => 25, "campaign_name" => "2015 Chester County", "total_sales" => 125.00, "status" => "Inactive"); $list[] = array("id" => 24, "campaign_name" => "2015 Midland County", "total_sales" => 3541.00, "status" => "Active"); $list[] = array("id" => 23, "campaign_name" => "2015 Midland County", "total_sales" => 458.00, "status" => "Inactive"); $list[] = array("id" => 22, "campaign_name" => "2015 Gastonia, NC Explorers", "total_sales" => 985.00, "status" => "Inactive"); $list[] = array("id" => 21, "campaign_name" => "2015 Powder Springs Explorers", "total_sales" => 4592.00, "status" => "Inactive"); $list[] = array("id" => 20, "campaign_name" => "2015 Anderson Explorers", "total_sales" => 0.00, "status" => "Active"); $list[] = array("id" => 19, "campaign_name" => "2015 Richland Co", "total_sales" => 1256.00, "status" => "Inactive"); $list[] = array("id" => 18, "campaign_name" => "2015 Pickens, Sc Explorers", "total_sales" => 2453.00, "status" => "Inactive"); $list[] = array("id" => 17, "campaign_name" => "2015 Frankfort, KY P.D.", "total_sales" => 1253.00, "status" => "Inactive"); $list[] = array("id" => 16, "campaign_name" => "2015 Paducah Explorer", "total_sales" => 45.00, "status" => "Inactive"); $list[] = array("id" => 15, "campaign_name" => "2015 NCDOA BURLINGTON", "total_sales" => 3658.00, "status" => "Inactive"); $list[] = array("id" => 14, "campaign_name" => "2014 Burleson PD Explorer", "total_sales" => 0.00, "status" => "Inactive"); $list[] = array("id" => 13, "campaign_name" => "Nassau Co. FL", "total_sales" => 425.00, "status" => "Inactive"); $list[] = array("id" => 12, "campaign_name" => "2014 NCDOA Watauga", "total_sales" => 965.00, "status" => "Inactive"); $list[] = array("id" => 11, "campaign_name" => "2014 Reidsville NCPD", "total_sales" => 2632.00, "status" => "Inactive"); $list[] = array("id" => 10, "campaign_name" => "2014 NCDOA Monroe", "total_sales" => 125.00, "status" => "Inactive"); $list[] = array("id" => 9, "campaign_name" => "2014 NCDOA Waxhaw", "total_sales" => 0.00, "status" => "Inactive"); $list[] = array("id" => 8, "campaign_name" => "Liberty Co. TX", "total_sales" => 459.00, "status" => "Inactive"); $list[] = array("id" => 7, "campaign_name" => "2014 NCDOA Monroe", "total_sales" => 256.00, "status" => "Inactive"); echo json_encode($list); ?>
It doesn’t matter how I get the $list array. I haven’t gotten it from a db query yet but I will.
I solved the problem like this (create 5 different divs in the container)
data.php<?php header('Content-Type: text/javascript; charset=utf8'); include('connect.php'); //connection String $db = new mysqli($hostname, $username, $password, $database); // get data and store in a json array $query = "SELECT * FROM DEV_OBJEKT ORDER BY OBJ_NR DESC "; $from = 0; $to = 26; $query .= "LIMIT ".$from.",".$to; $result=$db->query($query); $result->data_seek(0); $i=1; while($row = $result->fetch_assoc()) { $arrbild[$i]=$row['OBJ_NR']."_1.JPG"; $arrnr[$i]=$row['OBJ_NR']; $arrobj[$i]=$row['OBJ']; $arrut[$i]=$row['UTROP']; $arrbud[$i]=$row['BUD']; $arrst[$i]=$row['STOPPTID']; $i++; if($i==6) { $i=1; $objekt[] = array( 'Bild1' => utf8_encode($arrbild[1]), 'Nr1' => $arrnr[1], 'Obj1' => utf8_encode($arrobj[1]), 'Ut1' => $arrut[1], 'Bud1' => $arrbud[1], 'St1' => $arrst[1], 'Bild2' => $arrbild[2], 'Nr2' => $arrnr[2], 'Obj2' => utf8_encode($arrobj[2]), 'Ut2' => $arrut[2], 'Bud2' => $arrbud[2], 'St2' => $arrst[2], 'Bild3' => $arrbild[3], 'Nr3' => $arrnr[3], 'Obj3' => utf8_encode($arrobj[3]), 'Ut3' => $arrut[3], 'Bud3' => $arrbud[3], 'St3' => $arrst[3], 'Bild4' => $arrbild[4], 'Nr4' => $arrnr[4], 'Obj4' => utf8_encode($arrobj[4]), 'Ut4' => $arrut[4], 'Bud4' => $arrbud[4], 'St4' => $arrst[4], 'Bild5' => $arrbild[5], 'Nr5' => $arrnr[5], 'Obj5' => utf8_encode($arrobj[5]), 'Ut5' => $arrut[5], 'Bud5' => $arrbud[5], 'St5' => $arrst[5] ); } } echo json_encode($objekt); ?> demo
$(document).ready(function () {
var source =
{
datatype: “json”,
datafields: [
{ name: ‘Bild1’, type: ‘string’},
{ name: ‘Nr1’, type: ‘string’},
{ name: ‘Obj1’, type: ‘string’},
{ name: ‘Ut1’, type: ‘string’},
{ name: ‘Bud1’, type: ‘string’},
{ name: ‘St1’, type: ‘string’},
{ name: ‘Bild2’, type: ‘string’},
{ name: ‘Nr2’, type: ‘string’},
{ name: ‘Obj2’, type: ‘string’},
{ name: ‘Ut2’, type: ‘string’},
{ name: ‘Bud2’, type: ‘string’},
{ name: ‘St2’, type: ‘string’},
{ name: ‘Bild3’, type: ‘string’},
{ name: ‘Nr3’, type: ‘string’},
{ name: ‘Obj3’, type: ‘string’},
{ name: ‘Ut3’, type: ‘string’},
{ name: ‘Bud3’, type: ‘string’},
{ name: ‘St3’, type: ‘string’},
{ name: ‘Bild4’, type: ‘string’},
{ name: ‘Nr4’, type: ‘string’},
{ name: ‘Obj4’, type: ‘string’},
{ name: ‘Ut4’, type: ‘string’},
{ name: ‘Bud4’, type: ‘string’},
{ name: ‘St4’, type: ‘string’},
{ name: ‘Bild5’, type: ‘string’},
{ name: ‘Nr5’, type: ‘string’},
{ name: ‘Obj5’, type: ‘string’},
{ name: ‘Ut5’, type: ‘string’},
{ name: ‘Bud5’, type: ‘string’},
{ name: ‘St5’, type: ‘string’}
],
url: ‘data.php’,
cache: false
};
var dataAdapter = new $.jqx.dataAdapter(source); -
AuthorPosts
You must be logged in to reply to this topic.