2017-08-02 28 views
1

我想通過jqGrid中的單元格內容(不是通過選定的行)獲取行ID。如何獲得按行的行ID在jqGrid中的數據(不是通過所選的行)

Example pic

通過PRODUCTID,我可以得到該行的ID。

例如對於PRODUCTIDABCD,我可以得到2.

PRODUCTID是唯一的。

請給我一些建議。

非常感謝。

我的代碼示例:

$("#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"//, 

}); 

>版本: - 5.1.1的jqGrid

回答

0

是在ProductID是獨一無二的,該網格包含ProductID作爲列名稱colModel,則建議將key: true添加到列定義中。它強制jqGrid使用ProductID的值作爲rowid。

理解的jqGrid的代碼需要獨特id屬性設置爲jqGrid的的每一行(<tr>元素)是很重要的。見here。因此,jqGrid 的輸入數據必須包含rowid信息的。 jqGrid的輸入數據有很多可選的格式。以最常見的方式,輸入數據應該包含id屬性。如果您的輸入數據使用ProductID作爲行的唯一ID,那麼您可以添加選項jsonReader: { id: "ProductID" }以通知jqGrid。在的情況下,您將不需要包含ProductID作爲colModel中的列。

+0

謝謝奧列格。但我不知道你的意思。你能給我一些建議嗎?謝謝。 – Leo

+0

@Leo:不客氣!您應該將問題的文本附加到用於創建jqGrid的** JavaScript代碼**和JSON數據的示例中。 JavaScript代碼應該包含'colModel'參數。來自'colModel'元素的一個對應於'ProductID'列。你應該在列中加上'key:true'。 – Oleg

+0

感謝您的回覆。我添加代碼示例並修改一些說明。我主要需要通過單元格的內容獲取rowid(colName是PRODUCTID)。 – Leo

0

這也就不難理解你想要得到的東西 - 我想你指的rowIndex,所以這裏有一些可以幫助的方法。

方法

getGridRowById(串ROWID)

返回使用id = rowid的作爲文檔對象行

getInd(串ROWID,[布爾rowcontent])

返回由grid id row - rowid指定的網格表中行的索引。如果將rowcontent設置爲true,則返回行文檔對象

如果將行作爲文檔對象,則可以獲取索引和標識。假設rowData是一個文檔排,然後

rowdata.rowIndex是該指數

rowdata.id是id

+0

謝謝託尼。我有RowData但我不知道RowID。因爲我想更改單元格顏色,所以我必須通過RowData獲取RowID。你能給我一些建議嗎?謝謝。 – Leo

+0

什麼是rowData - javaScript對象(數組)或文檔對象。如果它是文檔對象,那麼id是RowData.id,如果這是JavaScript對象,你應該知道哪個字段是id,那麼id將是RowData.ProductID或RowData ['ProductID'] –

+0

對不起...我的解釋是不清楚。我的意思是RowData是單元格的內容。請參閱「示例圖片」。我的意思是RowData是「ABCD」。現在,我知道「ABCD」,但我想知道它的行ID(在這種情況下,行ID是2)。如果單元格的內容爲「TEST123」,則行ID爲1. – Leo

相關問題