2

I want to get the row id by content of cell in jqGrid (Not by selected row).

Example pic

By PRODUCTID, I can get the row id.

e.g. for PRODUCTID is ABCD, I can get 2.

The column PRODUCTID is unique.

Please give me some advices.

Thanks a lot.

My code sample:

$("#project_jqGrid").jqGrid({
    url: 'project/projectQuery.php',
    mtype: "POST",
    datatype: "json",
    page: 1,
    colModel: [
        {   label : "PRODUCTLINE",
            //sorttype: 'integer',
            name: 'PRODUCTLINE', 
            //key: true, 
            width: 100,
            editable:true,
            editoptions:{readonly:'readonly'}
        },
        {   label : "GPOWNER",
            //sorttype: 'integer',
            name: 'GPOWNER', 
            //key: true, 
            width: 150,
            editable:true,
            editoptions:{readonly:'readonly'}
        },
        {   label : "PRODUCTID",
            //sorttype: 'integer',
            name: 'PRODUCTID', 
            key: true, 
            width: 100,
            editable:true,
            editoptions:{readonly:'readonly'}
        },
    ],
    loadComplete: function() {

        $.ajax({
           dataType: 'json',
           url : "project/projectDifferQuery.php", // your php file
           type : "GET", // type of the HTTP request
           success : function(data){

              // I can get PRODUCTID from mysql database
              // I want to get rowid to change cells color by PRODUCTID
              // ........

              // Change Cells Color(I need to get '5' by position of PRODUCTID)
              //$('#project_jqGrid').jqGrid('setCell',5,"GPOWNER","",{'background-color':'#FF4545'});

           }
        });

    },
    loadonce: true,
    viewrecords: true,
    width: 'auto',
    height: 'auto',
    rowNum: 20,
    pager: "#project_jqGridPager"//,

});

> Versions: - jqGrid 5.1.1

clemens
  • 16,716
  • 11
  • 50
  • 65
Neishil
  • 57
  • 1
  • 8

2 Answers2

0

If the ProductID is unique and the grid contains ProductID as the column name in colModel, then it's recommended to add key: true to the column definition. It forces jqGrid to use the value from ProductID as the rowid.

It's important to understand that the code of jqGrid require to set unique id attribute to every row (<tr> element) of jqGrid. See here. Thus the input data of jqGrid have to contain rowid information. There are many alternative formats for the input data of jqGrid. In the most common way, the input data should contain id property. If your input data uses ProductID as the unique id of the row, then you can add the option jsonReader: { id: "ProductID" } to inform jqGrid about that. In that case you will not need to include ProductID as the column in colModel.

martieva
  • 131
  • 2
  • 11
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks Oleg. But I do not know what you mean. Could you give me some advices? Thanks. – Neishil Aug 03 '17 at 06:22
  • @Leo: You are welcome! You should append the text of your question with the **JavaScript code**, which you use to create jqGrid and an example of JSON data. The JavaScript code should contain `colModel` parameter. One from the `colModel` elements corresponds `ProductID` column. You should add `key: true` to the column. – Oleg Aug 03 '17 at 07:51
  • Thank for your reply. I add code sample and modify some description. I mainly need to get the rowid by content of cell (The colName is PRODUCTID). – Neishil Aug 03 '17 at 08:30
  • @Leo: You should add `key: true` only in one column `PRODUCTID`. After that you can examine `id` values of the rows, which are rowid. You can use Developer Tools of Chrome/IE . See [the picture](https://free-jqgrid.github.io/getting-started/index.html#grid-internal-div). Moreover I don't understand the code of `loadComplete`. It seems that you should use `cellattr` to set the background color. See [here](https://stackoverflow.com/a/12180842/315935) for example. One can set individual `class` or `style` attribute values on the column `GPOWNER` using `cellattr`. – Oleg Aug 03 '17 at 09:11
  • Thanks your comment. I thought it was complicated by myself... Finally, I rethink it. I finally used this **var cellid = $('td:contains(' + _PRODUCTID_ + ')').closest('tr').attr('id');** to get rowid. _PRODUCTID_ is variable. But I have another question. Could I select a specific _ColName_ to use _td:contains_ ? – Neishil Aug 08 '17 at 01:18
-1

It is little difficult to understand what you want to get - I think you mean rowIndex, so here are some methods which can help.

Methods

getGridRowById( string rowid)

Return the row with id = rowid as document object

getInd(string rowid, [boolean rowcontent])

Returns the index of the row in the grid table specified by grid id row - rowid. If rowcontent is set to true it returns the row document object

If you have the row as document object you can get the index and id. Suppose the rowdata is the document row, then

rowdata.rowIndex is the index

rowdata.id is the id

Tony Tomov
  • 3,122
  • 1
  • 11
  • 18
  • Thanks Tony. I have RowData but I don't know RowID. Because I want to change cell color, I have to get RowID by RowData. Could you give me some advices. Thanks. – Neishil Aug 02 '17 at 08:59
  • What is rowData - javaScript object (array) or document object. If it is document object then the id is RowData.id, if this is JavaScript object you should know which field is the id, then the id will be RowData.ProductID or RowData['ProductID'] – Tony Tomov Aug 02 '17 at 09:22
  • Sorry... My explanation is not clear. I mean the RowData is content of cell. Please refer to "Example pic". I mean the RowData is "ABCD". Now, I know "ABCD" but I want to know its row id (In this case row id is 2). If content of cell is "TEST123", row id is 1. – Neishil Aug 02 '17 at 09:33
  • How you get this value "ABCD"? From where you get it with wich method or event?. I can recommend you a way using getCol method, but I'm sure ther is much easer way to do his if you explain me from where you get this – Tony Tomov Aug 02 '17 at 09:48
  • I get value from mysql database. I decide which cell to change color based on the value of the database. – Neishil Aug 02 '17 at 10:15
  • @Leo, I really want to help. Can you please explain me with simple words what is you goal? Explain what you want to do: By example after creating the grid under some conditions I want to change the color of the cell(s) in grid - to do this I... - (please end this ) – Tony Tomov Aug 03 '17 at 09:26
  • Thank you for your help! Sorry I reply late. I have a table from mysql. I want to change the color of the cell(_GPOWNER_) if _PRODUCTID_ exist in this table. So I need to get rowid of _PRODUCTID_ to change color of cells. – Neishil Aug 08 '17 at 06:37
  • In this case as explained in other comments it is recommened to use cellattr event defined in colModel and do not use the loadComplete. More about cellattr event you can find [here](http://www.guriddo.net/documentation/guriddo/javascript/user-guide/basic-grid/#colmodel-options) – Tony Tomov Aug 08 '17 at 07:59
  • Thanks so much. I'll try this way. – Neishil Aug 09 '17 at 06:34