jQWidgets Forums
jQuery UI Widgets › Forums › Grid › Select multiple rows and get there values using checkbox.
Tagged: checkbox selection, grid
This topic contains 13 replies, has 5 voices, and was last updated by Peter Stoev 10 years, 2 months ago.
-
Author
-
In my grid, I want to add checkbox to each and every rows.When I check multiple check boxes and click a button, I want to get all of the values from selected rows.Can anybody please help me achieving this?
Hi basim.sherif,
The code below demonstrates the following:
– Adds a Checkbox Column.
– Disables the built-in selection.
– When the user checks a checkbox, the row is selected. When a checkbox is unchecked, the row is unselected.
– Pressing a button will get all selected rows.<!DOCTYPE html><html lang="en"><head> <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" /> <script type="text/javascript" src="../../scripts/jquery-1.7.1.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.edit.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxgrid.selection.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxcheckbox.js"></script> <script type="text/javascript" src="../../scripts/gettheme.js"></script> <script type="text/javascript"> function generatedata(rowscount) { // prepare the data var data = new Array(); if (rowscount == undefined) rowscount = 100; var firstNames = [ "Andrew", "Nancy", "Shelley", "Regina", "Yoshi", "Antoni", "Mayumi", "Ian", "Peter", "Lars", "Petra", "Martin", "Sven", "Elio", "Beate", "Cheryl", "Michael", "Guylene" ]; var lastNames = [ "Fuller", "Davolio", "Burke", "Murphy", "Nagase", "Saavedra", "Ohno", "Devling", "Wilson", "Peterson", "Winkler", "Bein", "Petersen", "Rossi", "Vileid", "Saylor", "Bjorn", "Nodier" ]; var productNames = [ "Black Tea", "Green Tea", "Caffe Espresso", "Doubleshot Espresso", "Caffe Latte", "White Chocolate Mocha", "Caramel Latte", "Caffe Americano", "Cappuccino", "Espresso Truffle", "Espresso con Panna", "Peppermint Mocha Twist" ]; var priceValues = [ "2.25", "1.5", "3.0", "3.3", "4.5", "3.6", "3.8", "2.5", "5.0", "1.75", "3.25", "4.0" ]; for (var i = 0; i < rowscount; i++) { var row = {}; var productindex = Math.floor(Math.random() * productNames.length); var price = parseFloat(priceValues[productindex]); var quantity = 1 + Math.round(Math.random() * 10); row["id"] = i; row["available"] = false; row["firstname"] = firstNames[Math.floor(Math.random() * firstNames.length)]; row["lastname"] = lastNames[Math.floor(Math.random() * lastNames.length)]; row["productname"] = productNames[productindex]; row["price"] = price; row["quantity"] = quantity; row["total"] = price * quantity; var date = new Date(); date.setFullYear(2012, Math.floor(Math.random() * 11), Math.floor(Math.random() * 27)); date.setHours(0, 0, 0, 0); row["date"] = date; data[i] = row; } return data; } $(document).ready(function () { var theme = getTheme(); // prepare the data var data = generatedata(200); var source = { localdata: data, datatype: "array", updaterow: function (rowid, rowdata) { // synchronize with the server - send update command } }; var dataAdapter = new $.jqx.dataAdapter(source); // initialize jqxGrid. Disable the built-in selection. $("#jqxgrid").jqxGrid( { source: dataAdapter, editable: true, selectionmode: 'none', columns: [ { text: 'Available', datafield: 'available', columntype: 'checkbox', width: 67 }, { text: 'First Name', editable: false, columntype: 'textbox', datafield: 'firstname', width: 90 }, { text: 'Last Name', editable: false, datafield: 'lastname', columntype: 'textbox', width: 90 }, { text: 'Product', editable: false, datafield: 'productname' } ] }); // select or unselect rows when the checkbox is clicked. $("#jqxgrid").bind('cellendedit', function (event) { if (event.args.value) { $("#jqxgrid").jqxGrid('selectrow', event.args.rowindex); } else { $("#jqxgrid").jqxGrid('unselectrow', event.args.rowindex); } }); // get all selected records. $("#Button").click(function () { var rows = $("#jqxgrid").jqxGrid('selectedrowindexes'); var selectedRecords = new Array(); for (var m = 0; m < rows.length; m++) { var row = $("#jqxgrid").jqxGrid('getrowdata', rows[m]); selectedRecords[selectedRecords.length] = row; } }); }); </script></head><body class='default'> <div id='jqxWidget'> <div id="jqxgrid"></div> <input type="button" id="Button" value="Get Selected Rows Date" /> </div></body></html>
Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.comThank you so much Peter…I will try this and get back to you for further help…thanks again
It works like charm…
The above code does not work for me…
Any modification needed ?Hi vsriram92,
The above code is from April 2012. I suggest you to look at the Checkbox selection demo and the Grid’s API for getting the selected rows.
Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.comI am using jqwidgets version 2.8.3 and the selectionmode: ‘checkbox’ does not work. Meaning a separate column does not get added to me.
which version of jqwidgets supports this feature?
Hi jignyasa,
It will not work, because in that version(2.8.3) there’s no such selection mode. It was introduced in a more recent version.
Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.comHi there,
can you post the same xample for the Treegrid?
Best
THDHi THD,
The TreeGrid has getSelection method which returns an Array of selected rows. I believe you can loop through an array.
Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.comHi Peter,
but the getSelection Method gets the selected rows not these one which are checked by the checkboxes.
Best
ThDHi THD,
The example in this topic shows how to select/unselect rows with checkboxes. That’s why I suggested you to use “getSelection” to get the selected rows. jqxTreeGrid does not have CheckBox Rows Selection feature. It has events raised when a row’s checkbox is checked/unchecked. You can subscribe to these events and fill a custom Array with Checked Rows.
Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.comhow do I do that?
how to get the events?
why it can’t be simply like the grid?
is there a way making the normal grid a treegrid (some tweaks)Hi THD,
All widgets with their available features are demonstrated and available online: http://www.jqwidgets.com/jquery-widgets-demo/. I am sorry that we do not have all the features you’re looking for.
Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.com -
AuthorPosts
You must be logged in to reply to this topic.