Implement Radio Button Selection In JQWidgets JQXGrid

In this post, we will see how we can implement the radio button selection feature in jQWidgets jQXGrid. In JQXGrid, we don’t have this functionality yet, but we can do it ourselves by using the check box selection which is already available. We will load the grid with the sample data and implement the radio button selection. If you are new to JQX Grid, you can see some tips here. I hope you will like this.

Download Source Code

You can always download the source code from here: Radio button selection in JQXGrid

Background

I am using JQXGrid in one of my projects where I want to implement the radio button. When I talked with the JQXGrid development team, I was told that the functionality is not yet available but they have given an option as a selection mode: ‘checkbox’, with the help of which we can implement the requirement. As you know, to load a jQWidget JQX Grid, you need the following mandatory things:

  • Datafields
  • Columns
  • Local data or Server side data

Using the code

First of all, load the needed references. You can download the needed files from here.

  1. <link rel="stylesheet" href="jqwidgets/styles/jqx.base.css" type="text/css" />  
  2. <script src="jquery-2.2.3.min.js"></script>  
  3. <script type="text/javascript" src="jqwidgets/jqxcore.js"></script>  
  4. <script type="text/javascript" src="jqwidgets/jqxdata.js"></script>  
  5. <script type="text/javascript" src="jqwidgets/jqxbuttons.js"></script>  
  6. <script type="text/javascript" src="jqwidgets/jqxscrollbar.js"></script>  
  7. <script type="text/javascript" src="jqwidgets/jqxmenu.js"></script>  
  8. <script type="text/javascript" src="jqwidgets/jqxgrid.js"></script>  
  9. <script type="text/javascript" src="jqwidgets/jqxgrid.edit.js"></script>  
  10. <script type="text/javascript" src="jqwidgets/jqxgrid.selection.js"></script>  
  11. <script type="text/javascript" src="jqwidgets/jqxgrid.pager.js"></script>  
  12. <script type="text/javascript" src="jqwidgets/jqxlistbox.js"></script>  
  13. <script type="text/javascript" src="jqwidgets/jqxdropdownlist.js"></script>  
  14. <script type="text/javascript" src="jqwidgets/jqxgrid.sort.js"></script>  
  15. <script type="text/javascript" src="jqwidgets/jqxcheckbox.js"></script>  
  16. <script type="text/javascript" src="jqwidgets/jqxgrid.columnsresize.js"></script>  
Now, we are going to set our data source.This time, we will use an XML file as a data source.
  1. <?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>  
  2. <feed xml:base="http://services.odata.org/Northwind/Northwind.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">  
  3.     <title type="text">Orders</title>  
  4.     <id>http://services.odata.org/Northwind/Northwind.svc/Orders</id>  
  5.     <updated>2011-12-01T11:55:06Z</updated>  
  6.     <link rel="self" title="Orders" href="Orders" />  
  7.     <entry>  
  8.         <id>http://services.odata.org/Northwind/Northwind.svc/Orders(10248)</id>  
  9.         <title type="text"></title>  
  10.         <updated>2011-12-01T11:55:06Z</updated>  
  11.         <author>  
  12.             <name />  
  13.         </author>  
  14.         <link rel="edit" title="Order" href="Orders(10248)" />  
  15.         <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Customer" type="application/atom+xml;type=entry" title="Customer" href="Orders(10248)/Customer" />  
  16.         <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Employee" type="application/atom+xml;type=entry" title="Employee" href="Orders(10248)/Employee" />  
  17.         <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Order_Details" type="application/atom+xml;type=feed" title="Order_Details" href="Orders(10248)/Order_Details" />  
  18.         <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Shipper" type="application/atom+xml;type=entry" title="Shipper" href="Orders(10248)/Shipper" />  
  19.         <category term="NorthwindModel.Order" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />  
  20.         <content type="application/xml">  
  21.             <m:properties>  
  22.                 <d:OrderID m:type="Edm.Int32">10248</d:OrderID>  
  23.                 <d:CustomerID>VINET</d:CustomerID>  
  24.                 <d:EmployeeID m:type="Edm.Int32">5</d:EmployeeID>  
  25.                 <d:OrderDate m:type="Edm.DateTime">1996-07-04T00:00:00</d:OrderDate>  
  26.                 <d:RequiredDate m:type="Edm.DateTime">1996-08-01T00:00:00</d:RequiredDate>  
  27.                 <d:ShippedDate m:type="Edm.DateTime">1996-07-16T00:00:00</d:ShippedDate>  
  28.                 <d:ShipVia m:type="Edm.Int32">3</d:ShipVia>  
  29.                 <d:Freight m:type="Edm.Decimal">32.3800</d:Freight>  
  30.                 <d:ShipName>Vins et alcools Chevalier</d:ShipName>  
  31.                 <d:ShipAddress>59 rue de l'Abbaye</d:ShipAddress>  
  32.                 <d:ShipCity>Reims</d:ShipCity>  
  33.                 <d:ShipRegion m:null="true" />  
  34.                 <d:ShipPostalCode>51100</d:ShipPostalCode>  
  35.                 <d:ShipCountry>France</d:ShipCountry>  
  36.             </m:properties>  
  37.         </content>  
  38.     </entry>  
  39.     <entry>  
  40.         <id>http://services.odata.org/Northwind/Northwind.svc/Orders(10249)</id>  
  41.         <title type="text"></title>  
  42.         <updated>2011-12-01T11:55:06Z</updated>  
  43.         <author>  
  44.             <name />  
  45.         </author>  
  46.         <link rel="edit" title="Order" href="Orders(10249)" />  
  47.         <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Customer" type="application/atom+xml;type=entry" title="Customer" href="Orders(10249)/Customer" />  
  48.         <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Employee" type="application/atom+xml;type=entry" title="Employee" href="Orders(10249)/Employee" />  
  49.         <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Order_Details" type="application/atom+xml;type=feed" title="Order_Details" href="Orders(10249)/Order_Details" />  
  50.         <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Shipper" type="application/atom+xml;type=entry" title="Shipper" href="Orders(10249)/Shipper" />  
  51.         <category term="NorthwindModel.Order" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />  
  52.         <content type="application/xml">  
  53.             <m:properties>  
  54.                 <d:OrderID m:type="Edm.Int32">10249</d:OrderID>  
  55.                 <d:CustomerID>TOMSP</d:CustomerID>  
  56.                 <d:EmployeeID m:type="Edm.Int32">6</d:EmployeeID>  
  57.                 <d:OrderDate m:type="Edm.DateTime">1996-07-05T00:00:00</d:OrderDate>  
  58.                 <d:RequiredDate m:type="Edm.DateTime">1996-08-16T00:00:00</d:RequiredDate>  
  59.                 <d:ShippedDate m:type="Edm.DateTime">1996-07-10T00:00:00</d:ShippedDate>  
  60.                 <d:ShipVia m:type="Edm.Int32">1</d:ShipVia>  
  61.                 <d:Freight m:type="Edm.Decimal">11.6100</d:Freight>  
  62.                 <d:ShipName>Toms Spezialitäten</d:ShipName>  
  63.                 <d:ShipAddress>Luisenstr. 48</d:ShipAddress>  
  64.                 <d:ShipCity>Münster</d:ShipCity>  
  65.                 <d:ShipRegion m:null="true" />  
  66.                 <d:ShipPostalCode>44087</d:ShipPostalCode>  
  67.                 <d:ShipCountry>Germany</d:ShipCountry>  
  68.             </m:properties>  
  69.         </content>  
  70.     </entry>  
  71.     <link rel="next" href="http://services.odata.org/Northwind/Northwind.svc/Orders?$skiptoken=10447" />  
  72. </feed>  
You can get the full data from the file
orders.xml from the solution attached.

Now, create a div element where we can bind the grid.

  1. <div id='jqxWidget' style="font-size: 13px; font-family: Verdana; float: left;">  
  2. <div id="jqxgrid">  
  3. </div>  
  4. </div>  
Now, we will define our grid as follows.
  1. $("#jqxgrid").jqxGrid  
  2. ({  
  3.     width: 850,  
  4.     height: 450,  
  5.     source: dataAdapter,  
  6.     sortable: true,  
  7.     selectionmode: 'checkbox',  
  8.     altrows: true,  
  9.     ready: function()  
  10.     {  
  11.         $('#columntablejqxgrid .jqx-checkbox-default').hide();  
  12.     },  
  13.     columns: [  
  14.       {  
  15.         text: 'Ship Name',  
  16.         datafield: 'ShipName',  
  17.         width: 250  
  18.     }, {  
  19.         text: 'Shipped Date',  
  20.         datafield: 'ShippedDate',  
  21.         width: 100,  
  22.         cellsformat: 'yyyy-MM-dd'  
  23.     }, {  
  24.         text: 'Freight',  
  25.         datafield: 'Freight',  
  26.         width: 150,  
  27.         cellsformat: 'F2',  
  28.         cellsalign: 'right'  
  29.     }, {  
  30.         text: 'Ship City',  
  31.         datafield: 'ShipCity',  
  32.         width: 150  
  33.     }, {  
  34.         text: 'Ship Country',  
  35.         datafield: 'ShipCountry'  
  36.     }]  
  37. }); 
As you can see, we have applied dataAdapter as the source. Below is the definition for your data adapter.
  1. var dataAdapter = new $.jqx.dataAdapter(source);  
Now, we will create the source object as follows:
  1. var source =  
  2.     {  
  3.     datatype: "xml",  
  4.     datafields: [{  
  5.         name: 'ShippedDate',  
  6.         map: 'm\\:properties>d\\:ShippedDate',  
  7.         type: 'date'  
  8.     }, {  
  9.         name: 'Freight',  
  10.         map: 'm\\:properties>d\\:Freight',  
  11.         type: 'float'  
  12.     }, {  
  13.         name: 'ShipName',  
  14.         map: 'm\\:properties>d\\:ShipName',  
  15.         type: 'string'  
  16.     }, {  
  17.         name: 'ShipAddress',  
  18.         map: 'm\\:properties>d\\:ShipAddress',  
  19.         type: 'string'  
  20.     }, {  
  21.         name: 'ShipCity',  
  22.         map: 'm\\:properties>d\\:ShipCity',  
  23.         type: 'string'  
  24.     }, {  
  25.         name: 'ShipCountry',  
  26.         map: 'm\\:properties>d\\:ShipCountry',  
  27.         type: 'string'  
  28.     }],  
  29.     root: "entry",  
  30.     record: "content",  
  31.     id: 'm\\:properties>d\\:OrderID',  
  32.     url: url  
  33. };  
As the grid definition is over, it is the time to run our Application and check whether the grid is loading fine or not.

application
Grid with check box selection:

What have you noticed? You are able to do the multiple selection, right? But what do we need? A radio button selection, isn’t it? Hence, we are supposed to select only one row, right? This is why,we haven’t done the needed implementation yet. Let us add some script then, as follows:

  1. $('#jqxgrid').on('rowselect'function(event)   
  2. {  
  3.     // event arguments.  
  4.     var args = event.args;  
  5.     // row's bound index.  
  6.     var rowBoundIndex = args.rowindex;  
  7.     var selectedIndexes = $('#jqxgrid').jqxGrid('selectedrowindexes');  
  8.     for (var i = 0; i < selectedIndexes.length; i++)  
  9.     {  
  10.         if (rowBoundIndex !== selectedIndexes[i])  
  11.         {  
  12.             $('#jqxgrid').jqxGrid('unselectrow', selectedIndexes[i]);  
  13.         }  
  14.     }  
  15. });  
We are just deleting the other row selection in the event rowselect. Now, run your Application again. I am sure you will be able to select only one row at a time.

application
Grid with radio button selection

Now, we can see the complete code.

Complete code

  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3.   
  4. <head>  
  5.     <link rel="stylesheet" href="jqwidgets/styles/jqx.base.css" type="text/css" />  
  6.     <script src="jquery-2.2.3.min.js"></script>  
  7.     <script type="text/javascript" src="jqwidgets/jqxcore.js"></script>  
  8.     <script type="text/javascript" src="jqwidgets/jqxdata.js"></script>  
  9.     <script type="text/javascript" src="jqwidgets/jqxbuttons.js"></script>  
  10.     <script type="text/javascript" src="jqwidgets/jqxscrollbar.js"></script>  
  11.     <script type="text/javascript" src="jqwidgets/jqxmenu.js"></script>  
  12.     <script type="text/javascript" src="jqwidgets/jqxgrid.js"></script>  
  13.     <script type="text/javascript" src="jqwidgets/jqxgrid.edit.js"></script>  
  14.     <script type="text/javascript" src="jqwidgets/jqxgrid.selection.js"></script>  
  15.     <script type="text/javascript" src="jqwidgets/jqxgrid.pager.js"></script>  
  16.     <script type="text/javascript" src="jqwidgets/jqxlistbox.js"></script>  
  17.     <script type="text/javascript" src="jqwidgets/jqxdropdownlist.js"></script>  
  18.     <script type="text/javascript" src="jqwidgets/jqxgrid.sort.js"></script>  
  19.     <script type="text/javascript" src="jqwidgets/jqxcheckbox.js"></script>  
  20.     <script type="text/javascript" src="jqwidgets/jqxgrid.columnsresize.js"></script>  
  21.     <script type="text/javascript">  
  22.         $(document).ready(function() {  
  23.             var url = "orders.xml";  
  24.             // prepare the data  
  25.             var source = {  
  26.                 datatype: "xml",  
  27.                 datafields: [{  
  28.                     name: 'ShippedDate',  
  29.                     map: 'm\\:properties>d\\:ShippedDate',  
  30.                     type: 'date'  
  31.                 }, {  
  32.                     name: 'Freight',  
  33.                     map: 'm\\:properties>d\\:Freight',  
  34.                     type: 'float'  
  35.                 }, {  
  36.                     name: 'ShipName',  
  37.                     map: 'm\\:properties>d\\:ShipName',  
  38.                     type: 'string'  
  39.                 }, {  
  40.                     name: 'ShipAddress',  
  41.                     map: 'm\\:properties>d\\:ShipAddress',  
  42.                     type: 'string'  
  43.                 }, {  
  44.                     name: 'ShipCity',  
  45.                     map: 'm\\:properties>d\\:ShipCity',  
  46.                     type: 'string'  
  47.                 }, {  
  48.                     name: 'ShipCountry',  
  49.                     map: 'm\\:properties>d\\:ShipCountry',  
  50.                     type: 'string'  
  51.                 }],  
  52.                 root: "entry",  
  53.                 record: "content",  
  54.                 id: 'm\\:properties>d\\:OrderID',  
  55.                 url: url  
  56.             };  
  57.             var dataAdapter = new $.jqx.dataAdapter(source);  
  58.             // create jqxgrid.  
  59.             $("#jqxgrid").jqxGrid({  
  60.                 width: 850,  
  61.                 height: 450,  
  62.                 source: dataAdapter,  
  63.                 sortable: true,  
  64.                 selectionmode: 'checkbox',  
  65.                 altrows: true,  
  66.                 ready: function() {  
  67.                     $('#columntablejqxgrid .jqx-checkbox-default').hide();  
  68.                 },  
  69.                 columns: [{  
  70.                     text: 'Ship Name',  
  71.                     datafield: 'ShipName',  
  72.                     width: 250  
  73.                 }, {  
  74.                     text: 'Shipped Date',  
  75.                     datafield: 'ShippedDate',  
  76.                     width: 100,  
  77.                     cellsformat: 'yyyy-MM-dd'  
  78.                 }, {  
  79.                     text: 'Freight',  
  80.                     datafield: 'Freight',  
  81.                     width: 150,  
  82.                     cellsformat: 'F2',  
  83.                     cellsalign: 'right'  
  84.                 }, {  
  85.                     text: 'Ship City',  
  86.                     datafield: 'ShipCity',  
  87.                     width: 150  
  88.                 }, {  
  89.                     text: 'Ship Country',  
  90.                     datafield: 'ShipCountry'  
  91.                 }]  
  92.             });  
  93.             $('#jqxgrid').on('rowselect', function(event) {  
  94.                 // event arguments.  
  95.                 var args = event.args;  
  96.                 // row's bound index.  
  97.                 var rowBoundIndex = args.rowindex;  
  98.                 var selectedIndexes = $('#jqxgrid').jqxGrid('selectedrowindexes');  
  99.                 for (var i = 0; i < selectedIndexes.length; i++) {  
  100.                     if (rowBoundIndex !== selectedIndexes[i]) {  
  101.                         $('#jqxgrid').jqxGrid('unselectrow', selectedIndexes[i]);  
  102.                     }  
  103.                 }  
  104.             });  
  105.         });  
  106.     </script>  
  107. </head>  
  108.   
  109. <body class='default'>  
  110.     <div id='jqxWidget' style="font-size: 13px; font-family: Verdana; float: left;">  
  111.         <div id="jqxgrid">  
  112.         </div>  
  113.     </div>  
  114. </body>  
  115.   
  116. </html>
Conclusion 

Did I miss anything that you may think is required? Have you found this post useful? I hope you liked this article. Please share with me your valuable suggestions and feedback.

Your turn. What do you think?

A blog isn’t a blog without comments, but do try to stay on the topic. If you have a question unrelated to this post, you’re better off, posting it on C# Corner, Code Project, Stack Overflow, ASP.NET Forum, instead of commenting here. Tweet or Email me a link to your question there and I’ll definitely try to help, if I can.

Please see this article in my blog here.

Up Next
    Ebook Download
    View all
    Learn
    View all