tags:

views:

214

answers:

4

Hi,

I'm trying to build a system using jqgrid which does everything on the clientside, i.e. use AJAX to retrieve a JSON object (using C#/.Net) and cache this in a javascript variable and then populate several grids from that cache (addRowData), depending on the data. That's all working very well.

However, I'm having problems discovering how to do some of the more advanced things now.

1) paging/sorting the data with no server interaction. Is this possible? Or does it require that I write custom paging functions (i.e. when the user moves to page 2, get the next 10 records from the cache and update the grid with them). Hopefully there's an automatic way to do this? How about sorting? Read some things that suggest its done server side but maybe they were old articles? Not sure.

2) Allow the users to see controls (mainly radio buttons and datepicker) on each row and have those changes reflected in the cache variable. I looked at the editing functions of jqGrid but this seems to be "click/edit/save". Ideally I'd like the grid's initial display to show, for example, one of the columns displaying pairs of radio buttons on every row and the user can just click the ones they are interested in (i.e. they don't have to explicitly "edit" the row to see/change the radio buttons). This data is updated in the grid and, more importantly, in the cache variable driving the grid. Is there an automatic way of tying controls on each row to an underlying client dataset? How do I create the controls in each cell and relate their value to the cell value?

Hoepfully someone could point me in the right direction?

Thanks for any help you can give,

Bill

+1  A: 

The latest version of jqGrid has what you need, I believe. From http://www.trirand.com/jqgridwiki/doku.php?id=wiki:change#jqgrid_3.7_changes_and_fixes

  • Added local paging, searching, sorting and virtual scrolling. Please see demo New in 3.7
Gregg
Thanks - checking it out now!
jqwha
+1  A: 

All what you want to have in the part 1 of your question can be implemented with jqGrid 3.7.x. You should use both datatype: 'json' and loadonce: true parameters. The server should send all the data back. See http://stackoverflow.com/questions/3169408/jqgrid-setgridparam-datatypelocal/3170652#3170652 as an example.

What about the second part of your question. It seems to me that you try to make jqGrid too complex. I find much easier to set some controls (select/dropdown boxes, checkboxes or radio buttons) outside of the jqGrid (for example on top of the grid). If the user make changes in some of this control you should reload the grid from the server based on the new choosed parameters. See http://stackoverflow.com/questions/2928371/how-to-filter-the-jqgrid-data-not-using-the-built-in-search-filter-box/2928819#2928819 as an example. If you will try to combine this way with the loadonce: true parameter you should understend, that after the first load of grid with loadonce: true the other parameter datatype: 'json' will be changed to datatype: 'local'. So to reload the grid you should additionally set datatype: 'json' every time before reloading the grid.

UPDATED based on comments: Well. If you have some predefined data sets, you want load all from the server and then quick display the grid needed you can just define some dives and place all jqGrids (table and paging div elements) inside the corresponding additional div (one div per jqGrid). You can start loading the data to all this grids at the page loading. You makes parents divs invisible or hidden with respect of jQuery.show() and jQuery.hide() anytime when you need. Divs which should be hidden at the page start can have CSS style display:none.

Another option to create grids dinamically with cached data is following. You can remove a jqGrid with jQuery.remove() and insert a new one <table> and paging <div> element with a method like jQuery.after(). With this way you can construct jqGrid absolutely dynamiclly. If you do so you should take in consideration, that jqGrid create some additional divs over table and paging div elements. So to delete whole jqGrid with id="list" you should remove div with id="gbox_list". Alternative if you place both <table> and paging <div> in a parent div element and you can use jQuery.empty() and jQuery.html() methods on the parent div to remove or insert a new one jqGrid.

Now about displaying of radio buttons inside of jqGrid. This is possible if you will use custom formater. See http://stackoverflow.com/questions/2991267/jqgrid-editable-column-that-always-shows-a-select/2992064#2992064 as example how to display selects (dropdown boxes) inside of jqGrid. By the way I see no advantage to use radio buttons compareing with selects. Radio buttons took only more place on the page and users will have to scroll the page friquently.

Nevertheless I don't recommend you to use complex elements inside of jqGrid cells. I recommend you to demonstrate to your users "Inline Editing" feature of jqGrid. It means if user select a row or make double-click on a row (you can implement one way which prefer your users) the row will be switched in the editing mode with checkboxes, select boxes (dropdown boxes), text inputs and so on. This is a standard way. It have some advantages over "form editing" from the side of users comfort. The more you go away from the standard ways the more problems you could receive in the future. To demonstrate "inline editing" you can open http://trirand.com/blog/jqgrid/jqgrid.html and choose on the tree left "Row Editing" and then "Input Types". As a code example you can use http://stackoverflow.com/questions/2863874/jqgrid-edit-only-certain-rows-for-an-editable-column/2866685#2866685.

UPDATED 2: One more small remark. If the data on the server will be not changed friquently and you want more long time cache there on the client you can consider to use prmNames: { nd:null} parameter of jqGrid especially if you use Internet Explorer. If you do this the last HTTP GET results (inclusive jQuery.ajax requests) will be cached on the client and next ajax rwquests can load data from the local browser cache instead of sending requests to the server. If the server include any information about the allowed caching time in the response (HTTP headers) it will be automatically used on the client in jQuery.ajax.

UPDATED 3 based on the source code posted: You have some errors in the code. Here is the fixed code

var myGrid = $("#mygrid").jqGrid({
    datatype: 'local',
    colModel: [
        { name: 'AID', label: 'Some ID', key: true, width: 100,
          editable: false, sorttype: "int" },
        { name: 'Name', width: 300, editable: false },
        { name: 'Group', width: 100, editable: false },
        { name: 'Info', width: 100, editable: false },
        { name: 'AValue', width: 100, editable: true, edittype: 'text' }
    ],
    pager: '#mypager',
    rowNum: 10,
    rowList: [10, 20, 500],
    viewrecords: true,
    autowidth: true,
    sortname: 'AID',
    sortorder: 'desc'
});
myGrid.jqGrid('navGrid','#mypager',{edit:false,add:false,del:false,search:false});

var mydata = [];
for (var i = 0; i < 100; i++) {
   mydata.push({AID:i,Name:"123",Group:"456",Info:"78",AValue:"8"});
}
myGrid.setGridParam({data: mydata}).trigger("reloadGrid");

You can try it here http://www.ok-soft-gmbh.com/jqGrid/Clientside.htm

UPDATED 4: The same example inclusive the client side editing is here http://www.ok-soft-gmbh.com/jqGrid/ClientsideEditing.htm. It is based on the http://www.trirand.com/blog/?page_id=393/help/losing-edited-cell-data-after-paging/ and http://stackoverflow.com/questions/3229141/losing-edited-cell-data-after-paging/.

Oleg
Thanks Oleg.The examples I've seen take data passed from the server and page/sort/etc. No problem. However, I would like to take data from the server and store it in a separate "cache" variable on the client. Then I wish to populate several grids on the page e.g. Grid1 has all cache rows where cacheRow.Array.Type = 1, Grid2 has "Type=2" rows, etc. Really I want to have complete, clientside control over adding and removing data from each grid. It seems that the grids don't "page" correctly when I do this - each grid shows all its rows! Must be missing something simple I think.
jqwha
I don't think I explained the second part very well.Say the dataset is [{ID:1,Name:'Bob',Gender:'Male'},{ID:1,Name:'Hilary',Gender:'Female'},...]My users want to see this with a column under "Gender" that has two radio buttons in each cell, 'Male' and 'Female'. They can then simply look at the list, click the correct radio, then save the data.My problems are:1) how to get the radio buttons into each cell. The users don't want to click "edit" on each row and would like to see the data as radios.2) binding the radios to the 'Gender' column in the client array. Hope you can help?
jqwha
oops. Should be [{ID:1,Name:'Bob',Gender:'Male'},{ID:2,Name:'Hilary',Gender:'Female'},...]I'm sure you know what I meant :)
jqwha
Oleg. You are, quite simply, a diamond! Thanks for looking at this.Actually, I think I will try to convince the users to go for the inline approach. I would have a harder time if they had a lot of "bulk" editing to do but that's not the case so I think we're OK.Thanks again!
jqwha
You welcome! I tried different approaches and find inline editing is the most comfortable for the user. By the way if you will sometime need some custom controls in the editing mode (works for both inline and form editing) it is also possible. Look at http://stackoverflow.com/questions/3054811/add-multiple-input-elements-in-a-custom-edit-type-field/3055626#3055626. Good luck!
Oleg
Hmmm...I just can't get paging/sorting to work. Just want to check - if I use addJSONData to add rows to the grid, will sorting/paging work on the data in 3.7? Or do I have to use the "data: ..." to bind it to a url/dataset before this will work?
jqwha
The usage of `addJSONData` is independent from the sorting/paging. For sorting and paging is important which `datatype` the jqGrid has at the moment of paging or sorting. If is is not 'local', that for correct paring and sorting is responsible your server application. In general if you have a problem with codding you should post the corresponding javascript code which has the problem.
Oleg
By the way I don't understand why you don't use `setGridParam` with 'data' parameter and use old `addRowData` instead. Description of 'data' parameter on http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options explicitly recommend replacing of `addRowData` to `setGridParam`.
Oleg
I posted some code below - should give an idea how wrong I'm doing it :)I did have a version that used addRowData but it seemed much faster to use addJSONData?Perhaps I have misunderstood the purpose of the "data" param? Is this where data is coming FROM (i.e. it LOADS the grid) or where it is STORED (i.e. when I load the grid with addJSONData, any variable specified in "data" will RECEIVE the data I add?Confused! :)
jqwha
OMG. And again. OMG. Thanks Oleg. I worked through the differences between our code and, although you have used better methods in several places, I can't believe that my version went from "not working" to "working fine" after I changed "dataType" to "datatype"!!! I can't believe it. Subtle error but it's been the cause of most of my problems over the last few days and dozens of attempts at local processing! Thanks for the better method of adding grid data too. Looks awesome.
jqwha
So is the live! Sometimes one can spend a lot of time in searching some easy bug. So having a working example is always very good. One can sequential transform one example to another permanently verify that it works. Usage of "dataType" instead of "datatype" is atypical error from people who worked before a lot with jQuery.ajax.
Oleg
A: 

My code is below. InitGridTestBulkLoad runs after the grid load is complete and the first page of 10 records display correctly. Paging controls indicate there I'm on page 1 of 10.

However, if I hit "Next Page" I see a brief "Loading" box but nothing changes. Also, if I click the first column heading to sort, nothing happens.

Grid def and code to store the "cached" data and load it into the grid is:

jQuery(document).ready(function() {
    jQuery("#mygrid").jqGrid({
        dataType: "local",
        data: {},
        colNames: ['AID', 'Name', 'Group', 'Info', 'AValue'],
        colModel: [
                        { name: 'AID', label: 'Some ID', index: 'SomeID', width: 100, editable: false, sorttype: "int" },
                        { name: 'Name', label: 'Name', index: 'Name', width: 300, editable: false },
                        { name: 'Group', label: 'Group', index: 'Group', width: 100, editable: false },
                        { name: 'Info', label: 'Info', index: 'Info', width: 100, editable: false },
                        { name: 'AValue', label: 'AValue', index: 'AValue', width: 100, editable: true, edittype: 'text' }
                      ],
        pager: '#mypager',
        rowNum: 10,
        rowList: [10, 20, 500],
        viewrecords: true,
        loadonce: true,
        autowidth: true,
        sortname: 'AID',
        sortorder: 'desc'
    });

});

var oJR = {};
oJR.rows = new Array();
function InitGridTestBulkLoad() {
    oJR.total = 100;
    oJR.page = 1;
    oJR.records = 100;
    for (var i = 0; i < 100; i++) {
        var s = i.toString();
        oJR.rows[i] = {};
        oJR.rows[i].id = i;
        oJR.rows[i].cell = [s, "123", "456", "78", "8"];
    }

    var mg = $("#mygrid");
    mg[0].addJSONData(oJR);
}
jqwha
A: 

Thanks Update 3. It worked, but to the extend that editing is not performing. I added Edit to your example result: -Submit is working -When paging, saved data is lost.

my editing code is somethig like this (as addition to yours):

...
codpager: '#mypager', 
rowNum: 10, 
rowList: [10, 20, 500], 
viewrecords: true, 
autowidth: true, 
sortname: 'AID', 
sortorder: 'desc', 
editurl: 'server.php' //added for editing to your code. this is dummy existing url
}); 
myGrid.jqGrid('navGrid','#mypager',{edit:true,add:false,del:false,search:false}); 
var mydata = []; e here
...

Another interesting problem is when I ran the same page in IIS application.(same code) -On Submit (of edit form): error, 'Method is not allowed'

Any idea for this two problems? Thanks, OKL

okl
Hi - I'm seeing the edit problem too! Did you resolve it?
jqwha