jQuery UI Widgets › Forums › Grid › Uncaught TypeError: Cannot read property 'length' of undefined
This topic contains 2 replies, has 2 voices, and was last updated by selva 10 years, 8 months ago.
-
Author
-
Hi,
Dynamically I created a new row and set values for cells.The new empty row was generated after that i set values for that row at that time i got this below error.
Uncaught TypeError: Cannot read property ‘length’ of undefined
Here is my code for your reference:
var source = { datatype: "json", datafields: [ {name: 'advertiserId', type: 'string'}, {name: 'advertiserName', type: 'string'} ], id: 'id' }; var gridAdapter = new $.jqx.dataAdapter(source); $('#brand_grid_edit').jqxGrid( { width: 500, height: 250, filterable: true, autoshowfiltericon: true, sortable: true, theme: theme_name, source: gridAdapter, showtoolbar: true, toolbarheight: 25, rendertoolbar: function(toolbar) { var me = this; var container = $("<div style='margin: 5px;'></div>"); var addButton = $("<div style='float: left; margin-left: 5px;margin-top:-4px;' title='Add Row'><img style='position: relative; margin-top: 0px;' src='jqx/images/add.png'/></div>"); var deleteButton = $("<div style='float: left; margin-left: 5px;margin-top:-4px;' title='Delete Row'><img style='position: relative; margin-top: 0px;' src='jqx/images/minus.png'/></div>"); toolbar.append(container); container.append(addButton); container.append(deleteButton); addButton.jqxButton({width: 20, height: 15, theme: theme_name}); deleteButton.jqxButton({width: 20, height: 15, theme: theme_name}); addButton.click(function() { var element = {}; var commit = $('#brand_grid_edit').jqxGrid('addrow', null, element); }); deleteButton.click(function() { var selectedrowindex = $("#brand_grid_edit").jqxGrid('getselectedrowindex'); var rowscount = $("#brand_grid_edit").jqxGrid('getrows'); if (selectedrowindex >= 0 && selectedrowindex < rowscount.length) { var id = $("#brand_grid_edit").jqxGrid('getrowid', selectedrowindex); var commit = $("#brand_grid_edit").jqxGrid('deleterow', id); } }); }, columns: [ {text: 'Advertiser ID', width: 200, datafield: 'advertiserId'}, {text: 'Advertiser Name', width: 300, datafield: 'advertiserName'} ] }); $('#brand_id_ok').click(function() { var rows = $('#brand_data').jqxGrid('getselectedrowindexes'); if (rows.length > 0) { var brand_id = $('#brand_data').jqxGrid('getcellvalue', rows, 'brandId'); var result = $.ajax({type: "GET", data: "brand_id=" + brand_id, url: "getAdvertiserByID", async: false}).responseText; var temp = result.split("^"); for (var i = 0; i < temp.length; i++) { var data = temp[i].split(":"); var commit = $('#brand_grid_edit').jqxGrid('addrow', null, {}); $('#brand_grid_edit').jqxGrid('setcellvalue', i, 'advertiserId', data[0]); $('#brand_grid_edit').jqxGrid('setcellvalue', i, 'advertiserName', data[1]); } } });
while i set values in grid using setcellvalue method, i got error. Could you please help me where i made mistakes
Hello selva,
The error most probably originates from these lines:
var result = $.ajax({type: "GET", data: "brand_id=" + brand_id, url: "getAdvertiserByID", async: false}).responseText; var temp = result.split("^");
It may be that result is not actually set (the Ajax call fails for some reason). Then temp is undefined, too. This means, when you call
temp.length
in the for loop, you get “Cannot read property ‘length’ of undefined”.Best Regards,
DimitarjQWidgets team
http://www.jqwidgets.com/Hi Dimitar,
I checked that using alert() function.If i removed this below code,
$('#brand_grid_edit').jqxGrid('setcellvalue', i, 'advertiserId', data[0]); $('#brand_grid_edit').jqxGrid('setcellvalue', i, 'advertiserName', data[1]);
for example if temp.length is equvalent to 2,two empty row was generated successfully.
-
AuthorPosts
You must be logged in to reply to this topic.