2

I have a page that has two jqGrids on it. One contains a list of available options, the other contains a list of included options. When the page is loaded each grid gets it's initial data from the server. After that I want the user to be able to move rows between grids until they're ready to submit the final result.

My code to accomplish this is:

function CopySelected(fromGrid, toGrid)
    {
        var grid = jQuery(fromGrid);
        var rowKey = grid.getGridParam("selrow");
        if(rowKey != null)
        {
            var row = grid.jqGrid('getRowData', rowKey);
            grid.delRowData(rowKey);
            jQuery(toGrid).addRowData(rowKey, row);
        }
    }

The problem with this approach is that I end up with duplicate rowids in the destination grid. Is there a way tell jqGrid to create a new ID, or get the next free ID?

Donavan Stanley
  • 107
  • 2
  • 6

1 Answers1

1

You can just use any prefix for the ids from the destination grid:

jQuery(toGrid).addRowData("bla_"+rowKey, row);
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • That solved the duplicate IDs problem but led right to a new problem. I'll see what I can dig up before starting a new question. – Donavan Stanley Feb 09 '11 at 15:19
  • @Donavan Stanley: I suppose that you could have problem in editing of the the second grid. In the case you can use `serializeCellData` (in case of cell editing), `beforeSubmit` or `serializeEditData` (in case of form editing) or `serializeRowData` (in case of inline editing) to modify the id before posting it to the server. – Oleg Feb 09 '11 at 15:36
  • @Donavan Stanley: By the way in http://www.trirand.com/blog/?page_id=393/feature-request/prefixes-to-rowids/ I suggested to introduce new `idprefix` parameter which could solve exactly the problem which you described. It's a pitty, but the feature request was not implemented in the form which I suggested. – Oleg Feb 09 '11 at 15:39
  • The current problem is that as soon as the user sorts the grid, it makes an ajax call replaces the contents of the grid with the original list. Trying to find a way to disconnect the grid after the initial load. – Donavan Stanley Feb 09 '11 at 15:51
  • @Donavan Stanley: You mean the second grid, where I suggested to use prefixes before ids? Which `datatype` it has? If you use data saved on the server you can make prefix of id on the server side. – Oleg Feb 09 '11 at 15:55
  • @Oleg Actually both grids have the problem. If I remove a row from gridA and put it in gridB. Sorting gridA results in the row showing up in both grids and sorting gridB causes the newly added row to disappear. It's because both grids are set to pull JSON data from a URL. If, after the initial load, I could switch to client side sorting I'd be great but I'm having trouble figuring that bit out. – Donavan Stanley Feb 09 '11 at 16:10
  • @Donavan Stanley: All depend from the implementation on your page. You should post the results of row moving to the server. If you will use `delGridRow` (from the form editing) it will post the information to the server. If you don't want to save the information to the server immediately, you should not have "Refresh" button in the grid and use `sortable:false` on all columns. You can also use `loadonce:true` option instead. In the case you will have **a copy** of the data from the first respond from the server and all sorting refreshing and so on will be done only locally. – Oleg Feb 09 '11 at 16:20
  • loadonce was what I was missing. Strange issue now where I can't sort on the first column after copying a row but can sort on the second column. I'll worry about that further down the road. – Donavan Stanley Feb 09 '11 at 16:28
  • @Donavan Stanley: Why you can't sort on the first column? With `loadonce:true` you should not has such restrictions. – Oleg Feb 09 '11 at 16:35
  • @Oleg turned out I had the wrong value for the name of the first column. – Donavan Stanley Feb 09 '11 at 17:49
  • @Donavan Stanley: OK, it can clear something. Important for sorting is mostly `index` if it exist. If the `index` property not defined in the grid it will be used `name`. In case of local sorting or the usage of `loadonce:true` the `index` property must be the same as the `name`. – Oleg Feb 09 '11 at 18:00