jQWidgets Forums
jQuery UI Widgets › Forums › Grid › Sizzle error when loading ‘local’ xml data
Tagged: datagrid, datgridview, grid, grid from xml, jquery grid
This topic contains 1 reply, has 2 voices, and was last updated by Peter Stoev 13 years ago.
-
Author
-
I am trying to load ‘local’ XML data into the jqwidgets ver1.9 grid. Here is the code:
<blockquote><pre><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <title></title><link href="jqwidgets/styles/jqx.base.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="scripts/jquery-1.7.1.js"></script> <script type="text/javascript" src="jqwidgets/jqxcore.js"></script> <script type="text/javascript" src="jqwidgets/jqxdata.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/jqxmenu.js"></script> <script type="text/javascript" src="jqwidgets/jqxcheckbox.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/jqxgrid.js"></script> <script type="text/javascript" src="jqwidgets/jqxgrid.sort.js"></script> <script type="text/javascript" src="jqwidgets/jqxgrid.pager.js"></script> <script type="text/javascript" src="jqwidgets/jqxgrid.selection.js"></script> <script type="text/javascript" src="scripts/gettheme.js"></script> <script type="text/javascript"> $(document).ready(function () { var theme = getTheme(); var getData = function () { return '\ <?xml version="1.0" encoding="utf-8" ?>\ <Products>\ <Product ProductID="1">\ <ProductName>Chai</ProductName>\ <SupplierID>1</SupplierID>\ <CategoryID>1</CategoryID>\ <QuantityPerUnit>10 boxes x 20 bags</QuantityPerUnit>\ <UnitPrice>18.0000</UnitPrice>\ <UnitsInStock>39</UnitsInStock>\ <UnitsOnOrder>0</UnitsOnOrder>\ <ReorderLevel>10</ReorderLevel>\ <Discontinued>false</Discontinued>\ </Product>\ </Products>'; } // prepare the data var source = { localdata: getData(), datatype: "xml", datafields: [ { name: 'ProductName' }, { name: 'QuantityPerUnit' }, { name: 'UnitPrice', type: 'float' }, { name: 'UnitsInStock', type: 'float' }, { name: 'Discontinued', type: 'bool' } ], root: "Products", record: "Product", id: 'ProductID' }; var cellsrenderer = function (row, columnfield, value, defaulthtml, columnproperties) { if (value < 20) { return '<span style="margin: 4px; float: ' + columnproperties.cellsalign + '; color: #ff0000;">' + value + '</span>'; } else { return '<span style="margin: 4px; float: ' + columnproperties.cellsalign + '; color: #008000;">' + value + '</span>'; } } var dataAdapter = new $.jqx.dataAdapter(source, { downloadComplete: function (data, status, xhr) { }, loadComplete: function (data) { }, loadError: function (xhr, status, error) { } }); // initialize jqxGrid $("#jqxgrid").jqxGrid( { width: 670, source: dataAdapter, theme: theme, pageable: true, autoheight: true, sortable: true, altrows: true, enabletooltips: true, columns: [ { text: 'Product Name', datafield: 'ProductName', width: 250 }, { text: 'Quantity per Unit', datafield: 'QuantityPerUnit', width: 150 }, { text: 'Unit Price', datafield: 'UnitPrice', cellsalign: 'right', cellsformat: 'c2', width: 70 }, { text: 'Units In Stock', datafield: 'UnitsInStock', cellsalign: 'right', cellsrenderer: cellsrenderer, width: 100 }, { text: 'Discontinued', columntype: 'checkbox', datafield: 'Discontinued', width: 100 }, ] }); }); </script></head><body> Grid Example: <div id='jqxWidget' style="font-size: 13px; font-family: Verdana; float: left;"> <div id="jqxgrid"> </div> </div></body></html></pre></blockquote>
Here is the Sizzle error I receive (it is generated deep from within jQuery) :
Sizzle.error = function( msg ) { throw new Error( "Syntax error, unrecognized expression: " + msg ); };
The parameter msg contains “<” which is the “<” at the start of the XML data (i.e “<Products …”). This is a far as I was able to debug the issue. Here is a stack trace:
Uncaught Error: Syntax error, unrecognized expression:
Sizzle.errorjquery-1.7.1.js:4179Sizzlejquery-1.7.1.js:3992Sizzlejquery-1.7.1.js:5083jQuery.fn.extend.findjquery-1.7.1.js:5339jQuery.fn.jQuery.initjquery-1.7.1.js:187jQueryjquery-1.7.1.js:27jQuery.fn.jQuery.initjquery-1.7.1.js:192jQueryjquery-1.7.1.js:27a.jqx.dataAdapter.getidjqxdata.js:7a.jqx.dataAdapter.loadxmljqxdata.js:7a.jqx.dataAdapter.dataBindjqxdata.js:7b.jqx.dataview.databindjqxgrid.js:7b.extend.databindjqxgrid.js:7b.extend.createInstancejqxgrid.js:7(anonymous function)jqxcore.js:7jQuery.extend.eachjquery-1.7.1.js:658jQuery.fn.jQuery.eachjquery-1.7.1.js:271a.jqx.jqxWidget.a.fn.(anonymous function)jqxcore.js:7(anonymous function)HTMLPage2.htm:76jQuery.Callbacks.firejquery-1.7.1.js:1046jQuery.Callbacks.self.fireWithjquery-1.7.1.js:1164jQuery.extend.readyjquery-1.7.1.js:435DOMContentLoadedWhats is the problem?
Any help will be greatly appreciated.
Dror (dror@caspit.biz)
Hi Dror Saddan,
The solution is to parse the XML by using the jQuery’s ParseXML function.
For example:
// prepare the datavar source ={ localdata: $($.parseXML(getData())), datatype: "xml", datafields: [ { name: 'ProductName' }, { name: 'QuantityPerUnit' }, { name: 'UnitPrice', type: 'float' }, { name: 'UnitsInStock', type: 'float' }, { name: 'Discontinued', type: 'bool' } ], root: "Products", record: "Product", id: 'ProductID'};
Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.com -
AuthorPosts
You must be logged in to reply to this topic.