{"action":"opened","issue":{"url":"https://api.github.com/repos/free-jqgrid/jqGrid/issues/195","labels_url":"https://api.github.com/repos/free-jqgrid/jqGrid/issues/195/labels{/name}","comments_url":"https://api.github.com/repos/free-jqgrid/jqGrid/issues/195/comments","events_url":"https://api.github.com/repos/free-jqgrid/jqGrid/issues/195/events","html_url":"https://github.com/free-jqgrid/jqGrid/issues/195","id":125224323,"number":195,"title":"Wrong insertion position (bug)","user":{"login":"okadurin","id":8991129,"avatar_url":"https://avatars.githubusercontent.com/u/8991129?v=3","gravatar_id":"","url":"https://api.github.com/users/okadurin","html_url":"https://github.com/okadurin","followers_url":"https://api.github.com/users/okadurin/followers","following_url":"https://api.github.com/users/okadurin/following{/other_user}","gists_url":"https://api.github.com/users/okadurin/gists{/gist_id}","starred_url":"https://api.github.com/users/okadurin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/okadurin/subscriptions","organizations_url":"https://api.github.com/users/okadurin/orgs","repos_url":"https://api.github.com/users/okadurin/repos","events_url":"https://api.github.com/users/okadurin/events{/privacy}","received_events_url":"https://api.github.com/users/okadurin/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-01-06T17:32:28Z","updated_at":"2016-01-06T17:32:28Z","closed_at":null,"body":"Hi! It turns out that the bug/issue that jqGrid 4.7.0 has free-jqGrid has as well. The issue is about a wrong position of the inserted row. The **addChildNode** function inserts child row in different position depending on how the grid load children rows. If the grid loads all rows at once than position of the inserted child row is the last child. It's OK. But when a row expands if the grid loads data dynamically than position of the inserted child can be different. [Here](https://jsfiddle.net/ovkadurin/ve6k2q7L/) is a jsFiddle that shows grids and describes how to reproduce a bug. \r\n\r\nBelow I copied the description of a bug from the above link just in case.\r\n\r\n---\r\nHere are 3 jqGrid tables. First two tables use autoid=true/false and load children\r\nrows dynamically. The main thing is dynamic loading. Testing of the \"autoid\" property was just \r\na wrong assumption.\r\n  The third table loads data at once.\r\n  \r\n  The bug is about a wrong inserted row position in case of dynamic children rows loading. So first two\r\ntables have this bug and the third one doesn't.\r\n\r\n  Steps to reproduce a bug (might be applied to any of grids on the right)\r\n1) Expand \"name3\"\r\n2) Expand \"name1\"\r\n3) Expand \"nam11\"\r\n4) [Optional] Collapse \"name11\"\r\n5) Click on the button below the correspondind grid\r\n\r\n  In case of first two grids the inserted row will be after the \"name11\" (keeping right level) which is a wrong position.\r\n  In case of the third grid the position will be \"the last child of name1\" which is a right position.\r\n\r\n  BTW, on the step 3 you can expand \"name12\". In that case the position of the inserted row will be after \"name12\" (keeping right level).\r\n\r\n---\r\n\r\nI drilled down a little. And it turned out that the reason of so different behaviour is because of how children rows of a new parent row are fetched (and/or how the rows are stored in the **data** variable). In case of dynamic data loading when a row is expanded all its children are pushed in the **data** array (at the end). And when **addChildrenNode** is [invoked](https://github.com/free-jqgrid/jqGrid/blob/master/js/grid.treegrid.js#L499) it [calls](https://github.com/free-jqgrid/jqGrid/blob/master/js/grid.treegrid.js#L534) **getFullTreeNode**  function which returns unordered array of new parent row children. The last element in that array is the last child of a last expanded row. And the further logic is to [insert](https://github.com/free-jqgrid/jqGrid/blob/master/js/grid.treegrid.js#L612) a new row after the last row in the array (keeping right level). \r\nIn case when the grid loads all the data at once the last element in the **data** array is the last visual element. It can't be at the middle. So in this case it works fine.\r\n\r\n---\r\nTo fix a bug for this case it is needed to sort children propely in the function **getFullTreeNode**.  [Here](https://github.com/free-jqgrid/jqGrid/blob/master/js/grid.treegrid.js#L292) is a place where children are sorted currently. How to make sorting and at what place is another task which is up to you.\r\n "}}