jQuery UI Widgets › Forums › Grid › Problem with addrow in grid
Tagged: jquery datagrid, jquery grid, jquery grid widget
This topic contains 4 replies, has 2 voices, and was last updated by priyam.basu 11 years, 4 months ago.
-
Author
-
Hi,
When I try to add a row with an id, I seem to lose the id when I click on the row. It show up the first time I click on it, but not after that.
I’m adding the row using var value = $(‘#TableDefinitionGrid’).jqxGrid(‘addrow’, guid(), {});
And on $(‘#TableDefinitionGrid’).on(‘rowselect’, function (event) {
TableFieldDefinitionID = event.args.row.TableFieldDefinitionID;
alert(TableFieldDefinitionID );
}This shows me the ID, the first time I click on the row. But it becomes null after that.
Hi priyam.basu,
The ID passed when you call the “addrow” method is not stored in a variable defined by you. It is internally stored in the Grid and you can access it by calling “getrowid” method.
Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.comHi Peter,
I had tried doing
var selectedrowindex = $(“#TableDefinitionGrid”).jqxGrid(‘getselectedrowindex’);
alert(selectedrowindex);
TableFieldDefinitionID = $(“#TableDefinitionGrid”).jqxGrid(‘getrowid’, selectedrowindex);
alert(“id: ” + TableFieldDefinitionID);But this gives me the id of the previous selected row.
Hi priyam,
Please, find below a sample which illustrates how to do the following:
1. Add Row with Custom ID
2. Select the Row with the Custom ID
3. Get the New Row’s ID using “getrowid”
4. Result of method call – alert with text – ‘myID’<!DOCTYPE html> <html lang="en"> <head> <title id='Description'>This example shows how to create a Grid from Array data.</title> <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" /> <script type="text/javascript" src="../../scripts/jquery-1.10.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/jqxgrid.columnsresize.js"></script> <script type="text/javascript" src="../../scripts/demos.js"></script> <script type="text/javascript"> $(document).ready(function () { // prepare the data var data = new Array(); 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", "Cramel 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 < 5; 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["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; data[i] = row; } var source = { localdata: data, datatype: "array", datafields: [ { name: 'firstname', type: 'string' }, { name: 'lastname', type: 'string' }, { name: 'productname', type: 'string' }, { name: 'quantity', type: 'number' }, { name: 'price', type: 'number' }, { name: 'total', type: 'number' } ] }; var dataAdapter = new $.jqx.dataAdapter(source); $("#jqxgrid").jqxGrid( { width: 670, source: dataAdapter, columnsresize: true, columns: [ { text: 'Name', dataField: 'firstname', width: 100 }, { text: 'Last Name', dataField: 'lastname', width: 100 }, { text: 'Product', editable: false, dataField: 'productname', width: 180 }, { text: 'Quantity', dataField: 'quantity', width: 80, cellsalign: 'right' }, { text: 'Unit Price', dataField: 'price', width: 90, cellsalign: 'right', cellsformat: 'c2' }, { text: 'Total', dataField: 'total', cellsalign: 'right', minwidth: 100, cellsformat: 'c2' } ] }); $("#jqxgrid").jqxGrid('addrow', 'myID', {}); $("#jqxgrid").jqxGrid('selectrow', 5); var id = $("#jqxgrid").jqxGrid('getrowid', 5); alert(id); }); </script> </head> <body class='default'> <div id='jqxWidget'> <div id="jqxgrid"> </div> </div> </body> </html>
Best Regards,
Peter StoevjQWidgets Team
http://www.jqwidgets.comHi Peter,
Thanks you for a quick response. If I want to send data through add row, I do so in JSON format. Does it matter if my JSON has extra information in it? Because it doesnt seem to be populating the data. It adds a blank row.
-
AuthorPosts
You must be logged in to reply to this topic.