3

I am using "json" to pull data from db. How can I get all value for a particular column.

I want to get all value/full set of value for "PrimarySkill" column irrespective of paging.

var texts = $("#listTableSupply").jqGrid('getCol', 'PrimarySkill');

This code only giving me a subset of "PrimarySkill" i.e. giving me the value those are in current page.

I want full set value.

enter image description here

Avijit
  • 1,219
  • 2
  • 15
  • 28

1 Answers1

7

If you have pure server side grid (with datatype: "xml" or datatype: "json" and you don't use loadonce: true) then jqGrid have no information about the data of other pages as the current page.

If you use local grid or remote grid where the server returns all data at once (loadonce: true are used) then the data are saved in internal _index and data parameters of jqGrid. So you can get the data using

var mydata = $("#listTableSupply").jqGrid("getGridParam", "data"),
    myPrimarySkill = $.map(mydata, function (item) { return item.PrimarySkill; });

alert (JSON.stringify(myPrimarySkill));

If you need to have the data in the format {id:rowid, value:cellvalue} (like getCol with true as the second parameter) then the code could be like the following

var mydata = $grid.jqGrid("getGridParam", "data"),
    ids = $grid.jqGrid("getGridParam", "_index"),
    myPrimarySkillWithIds = $.map(ids, function (index, key) {
        return { id: key, value: mydata[index].PrimarySkill };
    });

alert (JSON.stringify(myPrimarySkillWithIds));
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Your answers are very perfect and very descriptive. I am using local grid which fetches data from server in "json" with loadonce: true. – Avijit Nov 18 '14 at 10:30
  • @Oleg: thanks, your answer is great. However, I have 1 question. I read your answer [here](http://stackoverflow.com/questions/5328072/can-jqgrid-support-dropdowns-in-the-toolbar-filter-fields/5329014). If I want to make `getUniqueNames` function working with this (get data from all pages), how can I re-write the `getUniqueNames` so that it still accept `columnName` as a parameter? – Triet Doan Jul 25 '15 at 09:57
  • @Forte_201092: You need just get `$grid.jqGrid("getGridParam", "data")` and get `columnName` property of all data instead of `grid.jqGrid('getCol',columnName)` used in `getUniqueNames`. – Oleg Jul 25 '15 at 10:08
  • @Oleg: `getUniqueNames = function(columnName)`, in this function, `columnName` is a string passed in to know which column to get data. But because it's a string, we cannot write something like `item.columnName` to get data, right? I just want to make the function generic. As your code above, we only get the column named `PrimarySkill`. I want the column name to be passed as a variable. – Triet Doan Jul 26 '15 at 01:36
  • @Forte_201092: You need just make a loop or to use `jQuery.map` for example. If you have problems with implementation you can ask separate question where you describe the problem exactly. I would post the corresponding `getUniqueNames` in my answer. – Oleg Jul 26 '15 at 09:51
  • any suggestion if the data are not from local data? – Adiyat Mubarak Sep 30 '15 at 04:32
  • @AdiyatMubarak: If the data are not local then **have jqGrid the information only about the current page**. It send request to the server (to `url`) on changing of the page number or changing of the page size. So one have to make **separate custom Ajax request to the server** to get the data for the column from all pages. – Oleg Sep 30 '15 at 05:18
  • @AnhTriet - would you mind posting your solution for getting this to work with the `getUniqueNames` function? I'm stuck with the same problem – FastTrack Nov 11 '15 at 14:48
  • @FastTrack: yes, it's easy. Instead of writing `item.columnName`, we use `item['columnName']`. Pass the string name in the bracket. For more information, you can look at [this answer](http://stackoverflow.com/questions/4255472/javascript-object-access-variable-property-by-name-as-string) – Triet Doan Nov 11 '15 at 14:54
  • @AnhTriet thanks! I tried that by doing `var texts = $.map(mydata, function (item) { return item['columnName']; });` however `texts` is always of length `0`. Even though `columnName` is a varialble (one of the parameters of `getUniqueNames`) when I change `columnName` to an actual column name, it works great! Did you have the same trouble? – FastTrack Nov 11 '15 at 15:12
  • @FastTrack: sorry, I didn't go that way, so I didn't have that problem :) – Triet Doan Nov 11 '15 at 15:14
  • @AnhTriet no worries, I got it corrected... I just had to remove the single quotes inside the brackets (can't believe I didn't notice that earlier!). So it looks like `item[columnName]`. Thanks for your help! And thanks also to @Oleg for the initial answer. – FastTrack Nov 11 '15 at 15:21