0

I have a jqGrid that is calling a url to fill the data. I am using json as datatype. I need some information for each row that I dont want to show in the grid. So I am bringin a json with more properties than columns. But when I do:

jQuery("#gridId").getRowData("2");

I just can see the data that is asociated with a column. How can I have and query data in json that I dont want to show in the grid.

For example I have in the JSON

{
   Id: 2,
   Name: Pepe,
   Profession: Doctor
}

And I just want to see two columns in the grid, Id and Name, but I want to know the profesion in client side by each row.

Daniel Silva
  • 817
  • 8
  • 16
  • Do you use `loadonce: true` or not? How many total number of rows need be displayed? Which version of jqGrid you use and from which fork ([free jqGrid](https://github.com/free-jqgrid/jqGrid), [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334) or an old jqGrid in version <=4.7)? – Oleg Mar 25 '16 at 18:19
  • @Oleg I am using: * license jqGrid 4.4.4 - jQuery Grid * Copyright (c) 2008, Tony Tomov, tony@trirand.com Did you recommend I should update the version to solve this problem in a better way? I see your recomendations below and I think that should work. I will test it. Thank you very much!! – Daniel Silva Mar 27 '16 at 17:57
  • the version 4.4.4 is really very old and it's dad. If you downloaded it from NuGet then you should uninstall the NuGet package and install it from https://www.nuget.org/packages/free-jqGrid/. Free jqGrid is the fork of jqGrid which I develop. – Oleg Mar 27 '16 at 19:28

2 Answers2

2

The solution would depend on the total number of rows which you need to display. If the number of rows is not large (for example less as 1000 or less as 10000) then the best choice would be return all the data from the server at once. You should add loadonce: true option to inform jqGrid to read all the data and to save it in the internal data and _index parameters. The next important step would be setting the page size not so large, for example 10, 20 or 25. jqGrid will display the first page of data after loading all data from the server. By default jqGrid require that the server return sorted data corresponds to sortname and sortorder parameters, which will be send to the server as sidx and sortd. On the other side one can use additional option forceClientSorting: true to force free jqGrid to sort and filter the data before the first page will be displayed.

If one chosen the described above scenario then one can use additionalProperties option to save additional information for every row of data returned from the server. If you want for example to save the value of Profession property existing in every item of data then you can use additionalProperties: ["Profession"] option. After that you can access the data by id using getLocalRow. The method getLocalRow works much quickly as getRowData because it returns just the reference to internal row object, which was read from the server response. It contains all name properties from colModel and the properties from additionalProperties.

The demo https://jsfiddle.net/OlegKi/g8ffxpv2/2/ demonstrates the approach.

You asked additionally how to set title on some rows based on the content. I'd recommend you to use rowattr to set any attribute of the row (title, class, style and so on) see the answer or to use cellattr (see the answer or this one).

Only if one loads really large data and one can't use loadonce: true option than one can define dummy hidden columns, like {name: "Profession", hidden: true} instead of usage additionalProperties. The way is less effective, but it will work. By the way the value of properties used in additionalProperties could have any type. For example one can include in every item of the main data array of subgrid data in the property (details, subgrid). Hidden columns don't allows to load complex data, because the data hate to be not only leaded, but the saved as string in the hidden column (in <td> elements of the column).

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
1

If you are using Oleg's free-jqgrid and the version is the latest then you would do this:

var theGrid = $("#grid");
var myData = [
{
   "Id": 1,
   "Name": "John",
   "Profession": "Doctor"
},
{
   "Id": 2,
   "Name": "Pepe",
   "Profession": "Doctor"
},
{
   "Id": 2,
   "Name": "Jane",
   "Profession": "Nurse"
}
];


theGrid.jqGrid({
    data : myData,
    colNames: ['Id', 'Name'],
    colModel: [
       {name: 'Id'},
       {name: 'Name'}
       ]
});

var allData = theGrid.jqGrid("getGridParam").data;
console.log(JSON.stringify(allData));

//show Pepe's Profession:
$.each(allData, function(i, v){
    if(v.Name === "Pepe")
    alert(v.Profession);
});

If you are not using Oleg's free-jqgrid I highly recommend you do as you will be much happier in the end and he gives really good support. However if you are not using his grid and you are using version 4.6 of jqgrid you can still use the var allData = theGrid.jqGrid("getGridParam").data; to get all data from grid regardless of what columns you are showing.

Example using free-jqgrid 4.13.1: FIDDLE

Example using jqgrid 4.6: FIDDLE

Added from Oleg's comment: DEMO for json datatype

Devnsyde
  • 1,297
  • 1
  • 13
  • 34
  • 1
    Daniel Silva wrote "I am using json as datatype", but your demos uses `datatype: "local"`, which saves *automatically* all properties of input data. If one loads the data without `loadonce: true`, than one would have to use hidden columns to solve the problem. If one uses `loadonce: true` and free jqGrid, then one can use `additionalProperties` parameter to extend the list of properties, which will be saved locally. Your demo can be modified to https://jsfiddle.net/OlegKi/g8ffxpv2/2/, where Echo service (`url: "/echo/json/"`, `mtype: "POST"`) is used. – Oleg Mar 25 '16 at 22:38
  • @Oleg Oh I missed that _(regarding him stating that he is using json as his datatype)_, my bad, if the answer is not helpful let me know and I will take it down. Just trying to pay it forward for all the time you have helped me out. – Devnsyde Mar 25 '16 at 22:44
  • It's no problem, I just wanted to add that `getRowData` unformats the data from the row of the grid and so it can return only the data from `colModel`. On the other side `data` parameter and `getLocalRow` get data from JavaSctipt objects and it can contains extended data. The original question don't contains full code, thus we have to guess only. I think that your answer should be helpful, and thus I voted it up. – Oleg Mar 25 '16 at 23:03
  • @Oleg and Noctane, thank you both very much for your answers. I really apreciate your time and your insteresting point of views. As Oleg pointed in my case I am using datatype: json, so following what Oleg said I will need to do loadonece:true with "AdditionalProerties" or hide some columsn, I think the second options i maybe better, because if I have a lot of rows is not very eficcient have the loadonce in true. Anyway I will test both them. Oleg if you put your comment as an answer I can mark it as solved, :) – Daniel Silva Mar 27 '16 at 17:55
  • BTW, I thougth the idea of hidden column but I couln't find how to have hidden columns. :( – Daniel Silva Mar 27 '16 at 18:29
  • @DanielSilva: How many rows (approximately) you need to display? 100, 1000, 10000, 1000000, ...? The problem with hidden columns: it take **much more** memory of web browser and it makes the page slowly. Thus I would recommend to use hidden columns *only if the total number of rows is really large* and the usage of `loadonce: true` option would be bad. – Oleg Mar 27 '16 at 19:25
  • @Oleg thank you vey much again!! I changed to use your free-jqGrid and I have used your solution with AdditionalProperties and loadonce in true and that works like a charm. I did it because indeed I won´t have too much rows. I would like to ask you 3 things: 1) With free-jqGrid I am using setRowData with couple of links as value, and the title of rows is getting these links, how can I change that? 2) Could you please put your comment as an answer so I can mark it as solved? 3) I would like to know how to use hidden columns for other cases, do you know where I can read documentation about it? – Daniel Silva Mar 28 '16 at 19:33
  • @DanielSilva Some documentation links: https://github.com/free-jqgrid/jqGrid/wiki and http://free-jqgrid.github.io/jqGrid/ – Devnsyde Mar 29 '16 at 14:26