Greetings,
I'll try to be clear as possible in describing this problem as I couldn't catch its cause until I did long debugging inside Rad controls' code.
What I'm trying to create is a serial column (1, 2, 3, …) that keeps its proper serialing with the user deleting from and adding to the grid in batch mode.
The logic is very simple. I added a handler to the RowCreated event as follows:
The relevant part is the line marked with bold and italic (dataItems = tableView.get_dataItems())
Here's the sequence I do to reproduce the problem:
I'll try to be clear as possible in describing this problem as I couldn't catch its cause until I did long debugging inside Rad controls' code.
What I'm trying to create is a serial column (1, 2, 3, …) that keeps its proper serialing with the user deleting from and adding to the grid in batch mode.
The logic is very simple. I added a handler to the RowCreated event as follows:
grid.add_rowCreated(function (sender, args) { var item = args.get_item(); if(Edge.UI.GridExtensions.isNewRow(item)) Edge.UI.GridExtensions.setCellValue(item, uniqueName, Edge.UI.GridExtensions.SerialColumn.getMaxSerialValue(tableView, uniqueName) + 1); });Edge.UI.GridExtensions.SerialColumn.getMaxSerialValue = function (tableView, uniqueName){ var max = Number.NEGATIVE_INFINITY, dataItems = tableView.get_dataItems(); if(dataItems && dataItems.length) { for(var i = dataItems.length; i--;) { var value = parseInt(Edge.UI.GridExtensions.getCellValue(dataItems[i], uniqueName)); if(value > max) max = value; } } return max === Number.NEGATIVE_INFINITY ? 0 : max;}The relevant part is the line marked with bold and italic (dataItems = tableView.get_dataItems())
Here's the sequence I do to reproduce the problem:
- I start with an empty grid, then click the "Add New" button.
- The button calls BatchEditingManager.addNewRecord which in turn calls GridTableView.createItem.
- createItem creates the TR element and appends it to the DOM and creates a GridDataItem object for it.
- Then, near the end of the method body, come two lines of code who cause the problem:
I extracted these from the minified code with the usage of chrome developer tools' pretty print feature. You may find them using the find functionality of your text editor. The first line calls my event handler, which calls get_dataItems (the line in bold in the first code block). Inside get_dataItems the code checks the length of the internal _dataItems array:
p._owner.raise_rowCreated(newTelerik.Web.UI.GridDataItemEventArgs(n,null));Array.insert(e, k, d);Of course it finds the length equal to zero because the GridDataItem object wasn't added to the array yet in createItem. The code then goes into initialising the _dataItems array from the TR elements:if(this._dataItems.length > 0 || (this._cachedItems &&this._cachedItems.length > 0)) {returnthis._dataItems;}I replaced the irrelevant parts with ellipsis. This code adds a GridDataItem object to the array then raises the RowCreated event (which is the proper behaviour), but I wonder why isn't the event raised inside the if(!b) block? If the b object exists from a previous initialisation, why raising the RowCreated event for it again? The last piece of code causes my event handler to be called again and my serial result to be wrongly evaluated (it returns 2 instead of one), but this is not the main problem.varo = ($telerik.isOpera) ?this.get_element() :this.get_element().tBodies[0];varn = o.rows;for(varc = 0, k = n.length;c < k;c++) {…this._owner.raise_rowCreating(newSys.EventArgs());…if(!b) {b = $create(Telerik.Web.UI.GridDataItem, {_owner:this,_data: a},null,null, m);}…this._dataItems[this._dataItems.length] = b;this._owner.raise_rowCreated(newTelerik.Web.UI.GridDataItemEventArgs(m,null));} - After all that code executes, the _dataItems array now have two objects for the same TR element (one created by createItem and the other created by the get_dataItems accessor), if I remove the single grid row using BatchEditingManager.deleteRecord the grid remains with an orphan GridDataItem object.
After this, the grid stops working. When I try adding new lines, an exception erupts. I don't know certainly the cause, but it should be because of the orphan item.
After the description of the problem, I'd like to say that some accessors in the Rad controls code involve a lot of work which shouldn't be the nature of accessors. I have some places where get_dataItems raises exceptions, and I had no means to avoid but a nasty dummy try/catch
try { gridDataItems = this.tableView.get_dataItems(); } catch(dummy) { }