In this topic, we will illustrate how to add a Tooltip to a Grid Column header.
The first step is to include the required JavaScript and CSS files.
<link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" /><script type="text/javascript" src="../../scripts/jquery-1.8.2.min.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/jqxgrid.js"></script><script type="text/javascript" src="../../jqwidgets/jqxgrid.selection.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxlistbox.js"></script><script type="text/javascript" src="../../jqwidgets/jqxtooltip.js"></script>
The second step is to prepare the data. We will do this by using the jqxDataAdapter plug-in.
var url = "../sampledata/orders.xml";// prepare the datavar source ={ datatype: "xml", datafields: [ { name: 'ShippedDate', map: 'm\\:properties>d\\:ShippedDate', type: 'date' }, { name: 'Freight', map: 'm\\:properties>d\\:Freight', type: 'float' }, { name: 'ShipName', map: 'm\\:properties>d\\:ShipName', type: 'string' }, { name: 'ShipAddress', map: 'm\\:properties>d\\:ShipAddress', type: 'string' }, { name: 'ShipCity', map: 'm\\:properties>d\\:ShipCity', type: 'string' }, { name: 'ShipCountry', map: 'm\\:properties>d\\:ShipCountry', type: 'string' } ], root: "entry", record: "content", id: 'm\\:properties>d\\:OrderID', url: url, sortcolumn: 'ShipName', sortdirection: 'asc'};var dataAdapter = new $.jqx.dataAdapter(source);
The third step is to create the Grid. The Grid Column object has a callback function which is called “rendered”. The “rendered” callback is called once the column is rendered. We will use it for the initialization of the tooltip.
// create jqxgrid.$("#jqxgrid").jqxGrid({ source: dataAdapter, columns: [ { text: 'Ship Name', datafield: 'ShipName', width: 250, rendered: tooltiprenderer }, { text: 'Shipped Date', datafield: 'ShippedDate', width: 100, cellsformat: 'yyyy-MM-dd', rendered: tooltiprenderer }, { text: 'Freight', datafield: 'Freight', width: 80, cellsformat: 'F2', cellsalign: 'right', rendered: tooltiprenderer }, { text: 'Ship Address', datafield: 'ShipAddress', width: 350, rendered: tooltiprenderer }, { text: 'Ship City', datafield: 'ShipCity', width: 100, rendered: tooltiprenderer }, { text: 'Ship Country', datafield: 'ShipCountry', width: 101, rendered: tooltiprenderer } ]});
The final step is to implement the “tooltiprenderer” function. The Grid Column’s “rendered” callback is set to point to the “tooltiprenderer”
var tooltiprenderer = function (element) { $(element).jqxTooltip({position: 'mouse', content: $(element).text() });}