1

I'm setting up a jqGrid (in a Google Chrome Extension) which will handle local JSON data.

My concern is performance due to my unique use case. I have thousands of records getting dynamically generated on the client side over a few minutes and I can't wait for the data to be generated so currently I add this data to the grid row by row using 'addRowData'.

But the problem is, when I'm adding data to the grid I have to check if that data already exists and if it does I need to update the existing record. I'm just having trouble understanding the best way to accomplish this, is the only way I can search the grid by calling 'getCol' and then searching the array. My concern with calling getCol is I presume this searches the DOM? But I could be wrong, I have scroll: 1 set and I'm starting to think this might mean its pulling data directly from an array?

Or maybe I should be implementing this a totally different way? It would of been so much easier if I could of just inserted all of this data into an array and then loaded the grid but due to the time taken to generate the data the user needs to see it ASAP.

Sean Bannister
  • 3,105
  • 4
  • 31
  • 43
  • How the data, which are dynamically generated, are sorted? Do you use local data paging? Are you place the data, which you add new row of data with respect of `addRowData`, at the end of grid? Can the user see the added data? – Oleg Nov 18 '11 at 15:11
  • @Oleg I don't use paging. I place data at the end of the grid and the user can't see the data unless they scroll. – Sean Bannister Nov 18 '11 at 15:38
  • What about sorting of the data? – Oleg Nov 18 '11 at 15:45
  • @Oleg Yes I am using column sorting when a user clicks on the row header but don't do any other sorting. – Sean Bannister Nov 18 '11 at 16:25
  • @Oleg I'm really starting to think the only way to do this is searching the data in the column via getCol. – Sean Bannister Nov 19 '11 at 06:39
  • I don't think so. I have now not so much time, but I want to create later a demo for you which would show how you can do this more effectively. If you work with *local* data the most direct way to analyze the data is the usage of `data` parameter of jqGrid. I think that you should better modify the `data` parameter directly if you need add data to the grid which the user don't see directly (because of scrolling) . – Oleg Nov 19 '11 at 08:27
  • @Oleg I tried using the data parameter and then pushing to the end of the array but the new data didn't appear in the grid. I'm not very familiar with it so I'm not sure how it works however my concern is when I find a duplicate bit of data in the grid I have to update the row and this could potentially be the first row in the grid. – Sean Bannister Nov 19 '11 at 10:49
  • If would be much better if you add some code fragments to your question. At least it could be interesting to know the type of data (`sorttype`). Moreover some general information could be helpful about the problem and from where come the data. Do you receive the data always as one row or as an *array of rows*? If you post enough code I could try to modify it to show how one can work with `data` and `_index` parameters. What the user can do with the grid during the data are filled row per row? How many rows the grid has? I think that one can improve performance in your solution in many times. – Oleg Nov 19 '11 at 11:19
  • @Oleg Its actually the code you previous saw at seanbannister.com/example The grid is being displayed in a Google Chrome extension which allows us to do cross domain calls so I had to strip that from my example code and its not all written yet as I'm not sure how to implement it due to this issue. So basically we're scraping JSON data we get roughly 10 rows at a time and they come in every second. If the 'keyword' column matches an incoming JSON value we have to update some of the columns for the matching row. – Sean Bannister Nov 19 '11 at 13:23
  • @Oleg The grid will end up with a few 1000 rows depending on how the end user is using the app, potentially it could be any number but a few 1000 would be normal. The scraping process can take a long time depending once again on how the apps been used maybe 10 mins+ and during this time I want the user to be able to browse the data. – Sean Bannister Nov 19 '11 at 13:26
  • @Oleg While I'm concerned about performance at the moment I'm not even sure the best way to search for and update the matched rows. – Sean Bannister Nov 19 '11 at 13:34
  • The example from http://www.seanbannister.com/example/ has no usage of `addRowData` which is your main question. More realistic example could clear many things. You can improve performance in many times if you would use paring of local data. One can use JSONP directly in jqGrid see an example [here](http://stackoverflow.com/questions/4317646/jqgrid-returns-blank-cells/4326986#4326986). With respect of `beforeProcessing` you can make **any modifications** of received data (filtering for example). – Oleg Nov 19 '11 at 14:16
  • @Oleg Oh sorry the only difference in the 'real' version of the app is I insert that data row by row using addRowData as it gets scraped. There's really nothing to show there as I haven't got any further with the code that's where I'm stuck. Because I actually can't use addRowData until I've searched the existing inserted rows and checked if the keyword already exists. I'll have a look into before processing. – Sean Bannister Nov 19 '11 at 14:52
  • One more small tip. Look at [starting lines](https://github.com/tonytomov/jqGrid/blob/v4.2.0/js/grid.base.js#L1210) of `addJSONData`. You can see that setting some jqGrid parameters can follow that **the grid's data will not deleted** during processing. You can use `beforeProcessing` to make the trick and append existing grid with more data. The main advantage is that the HTML data for new rows can be added *at once* as one HTML string fragment (`gridview: true` must be used). In the way you can improve performance. – Oleg Nov 19 '11 at 15:18
  • You could see the rough idea of what I mean with appending data in the grid on [the demo](http://www.ok-soft-gmbh.com/jqGrid/AppendJsonDataToGrid.htm). If you need verify whether the row is already in the grid you can use `data` and `_index` parameters instead of `getCol`. The `'keyword'` column could be used as `id` if I correct understand you. So you can add `key: true` in the column. The object `_index` will hold ids of grid as properties. So to verify that id (keyword) is in grid you can just test `if (typeof($("#list")[0].p._index[keyword]) !== "undefined")`. – Oleg Nov 20 '11 at 11:53
  • You can of cause use `in` operator to test whether property exist in the object: `if (keyword in $("#list")[0].p._index)` – Oleg Nov 20 '11 at 11:54

0 Answers0