2011-08-11 86 views

回答

4

這對很多人誰開始使用jqGrid的通病。如果您使用ID複製填充網格,則會出現這種奇怪的選擇行爲。所以瞭解jqGrid將如何使用您輸入的ID會非常重要。

jqGrid使用內部HTML標記來顯示網格。它使用<table>,<tbody>(表格的主體 - 沒有列標題),<tr>(表格的行)和<td>(表格的表格)的任何網格。下面的HTML片段可能代表網格例如

<table> 
    <tbody> 
     <tr><td>16</td><td>A11</td><td>Add</td></tr> 
     <tr><td>1</td><td>Chart</td><td>Edit</td></tr> 
     <tr><td>1</td><td>Chart</td><td>Delete</td></tr> 
     <tr><td>1</td><td>Chart</td><td>View</td></tr> 
     <tr><td>15</td><td>Manage</td><td>Edit</td></tr> 
     <tr><td>16</td><td>A11</td><td>View</td></tr> 
     <tr><td>15</td><td>Manage</td><td>Delete</td></tr> 
    </tbody> 
</table> 

的jqGrid的設計是爲了讓人們可以有快速接入電網的任何行。爲了實現快速訪問,需要爲每個<tr>(表格行)分配一個唯一標識。您可以在jqGrid使用的最多methodsevents中找到rowid參數。該行的id應爲,表示表中該行的集合中的行。在這種情況下電網就會像下面

<table> 
    <tbody> 
     <tr id="a"><td>16</td><td>A11</td><td>Add</td></tr> 
     <tr id="b"><td>1</td><td>Chart</td><td>Edit</td></tr> 
     <tr id="c"><td>1</td><td>Chart</td><td>Delete</td></tr> 
     <tr id="abc"><td>1</td><td>Chart</td><td>View</td></tr> 
     <tr id="1"><td>15</td><td>Manage</td><td>Edit</td></tr> 
     <tr id="2"><td>16</td><td>A11</td><td>View</td></tr> 
     <tr id="35"><td>15</td><td>Manage</td><td>Delete</td></tr> 
    </tbody> 
</table> 

根據HTML規範的任何HTML元素的ID必須是在頁面上獨一無二的。如果分配重複的ID對行喜歡這裏例如

<table> 
    <tbody> 
     <tr id="16"><td>16</td><td>A11</td><td>Add</td></tr> 
     <tr id="1"><td>1</td><td>Chart</td><td>Edit</td></tr> 
     <tr id="1"><td>1</td><td>Chart</td><td>Delete</td></tr> 
     <tr id="1"><td>1</td><td>Chart</td><td>View</td></tr> 
     <tr id="15"><td>15</td><td>Manage</td><td>Edit</td></tr> 
     <tr id="16"><td>16</td><td>A11</td><td>View</td></tr> 
     <tr id="15"><td>15</td><td>Manage</td><td>Delete</td></tr> 
    </tbody> 
</table> 

表可仍顯示在大多數Web瀏覽器,但與電網的工作可能是非常有問題的。例如,如果選擇這種網格的最後一行,相應的jqGrid代碼將找出15作爲當前行的ID,它將使用$("#15").addClass("ui-state-highlight")來突出顯示當前行。而不是代碼將選擇(將類「ui-state-highlight」)添加到第一行,其中id =「15」。

所以你應該非常小心,如果你填充你的JSON數據用作jqGrid輸入。例如可以表示網格中的以下數據 包含:

{ 
    "total": 1, 
    "page": 1, 
    "records": 7, 
    "rows": [ 
     { "id": "16", "cell": ["16", "A11", "Add"] }, 
     { "id": "1", "cell": ["1", "Chart", "Add"] }, 
     { "id": "1", "cell": ["1", "Chart", "Delete"] }, 
     { "id": "1", "cell": ["1", "Chart", "View"] }, 
     { "id": "15", "cell": ["15", "Manage", "Delete"] }, 
     { "id": "16", "cell": ["16", "A11", "View"] }, 
     { "id": "15", "cell": ["15", "Manage", "Edit"] } 
    ] 
} 

的JSON數據可以被固定到

{ 
    "total": 1, 
    "page": 1, 
    "records": 7, 
    "rows": [ 
     { "id": "1", "cell": ["16", "A11", "Add"] }, 
     { "id": "2", "cell": ["1", "Chart", "Add"] }, 
     { "id": "3", "cell": ["1", "Chart", "Delete"] }, 
     { "id": "4", "cell": ["1", "Chart", "View"] }, 
     { "id": "5", "cell": ["15", "Manage", "Delete"] }, 
     { "id": "6", "cell": ["16", "A11", "View"] }, 
     { "id": "7", "cell": ["15", "Manage", "Edit"] } 
    ] 
} 

{ 
    "total": 1, 
    "page": 1, 
    "records": 7, 
    "rows": [ 
     { "id": "16_Add", "cell": ["16", "A11", "Add"] }, 
     { "id": "1_Add", "cell": ["1", "Chart", "Add"] }, 
     { "id": "1_Delete", "cell": ["1", "Chart", "Delete"] }, 
     { "id": "1_View", "cell": ["1", "Chart", "View"] }, 
     { "id": "15_Delete", "cell": ["15", "Manage", "Delete"] }, 
     { "id": "16_View", "cell": ["16", "A11", "View"] }, 
     { "id": "15_Edit", "cell": ["15", "Manage", "Edit"] } 
    ] 
} 

例如。然後,世界(包括電網)將會變好。

+0

感謝您的好評。它爲我工作。 – Saad

+0

@Saad:不客氣! – Oleg