2013-01-17 24 views
0

我正在開發一個使用JSP的Web應用程序& Servlet(IDE:Eclipse,Database:Oracle10)。 我正在使用jqGrid以表格格式顯示記錄。jqGrid組合框:如何動態添加值

我想從servlet中獲得組合框jqGrid的值,到目前爲止我已經完成了以下操作。

我正在訪問由JSP scriplet中的Servlet傳遞的數組。

<% 
    String[] stageIDs = (String[])request.getAttribute("combo"); 
%> 

以下是我colModel:

jQuery("#list10_d").jqGrid({ 
      height: "100%", 
      url:'ProtocolJGridServChild?q=2&action=protAction', 
      datatype: "xml", 
colNames:['Sr. No.','PID', 'SID'], 
colModel:[{name:'srNo',index:'srNo', width:35,sortable:true}, 
{name:'PID',index:'PID', width:100,sortable:false,editable:true,hidden:true}, 
{name:'SID',index:'SID', width:100, sortable:false, editable:true, edittype:"select",editoptions:{value:<%for(int i=0;i<stageIDs.length;i++)%><%="ID:"+ stageIDs[i]+";"%>}} 
], 
      rowNum:2, 
      rowList:[2,4,6], 
      pager: '#pager10_d', 
      sortname: 'PID', 
      viewrecords: true, 
      sortorder: "asc", 
      multiselect: true, 
      editurl: "MyServletName", 
      caption:"CRM_PROT_ACTIONS", 
}).navGrid('#pager10_d',{edit:true,add:true,del:true}); 

但我在for loop line得到一個例外,我分配editoptions到ComboBox。請讓我知道代碼中是否有任何錯誤。

我的另一個問題是,有沒有更好的方法來從servlet中分配值到jqGrid中的組合框(不使用scriplet)?

回答

1

你應該使用dataUrl而不是value裏面的editoptions來從服務器獲取組合框的值。如果服務器返回JSON數據而不是HTML片段,則可以使用buildSelect將服務器響應從dataUrl轉換爲jqGrid所需的格式。確切的實現可能取決於您使用的jqGrid的版本。您可以使用ajaxSelectOptions將相應Ajax請求的typedataType從默認「html」更改爲"json"(請參閱here)。請參閱here實施例buildSelect

+0

感謝(再次)..它的工作原理採用dataurl ... – Bhushan

+0

@Bhushan:歡迎您! – Oleg

0

要添加組合框的jqGrid王氏動態數據..

我曾嘗試使用以下方式來完成它。

jQuery("#list10_d").jqGrid({ 
     height: "100%", 
     url:'ProtocolJGridServChild?q=2&action=protAction', 
     datatype: "xml", 
colNames:['Sr. No.','PID', 'SID'], 
colModel:[{name:'srNo',index:'srNo', width:35,sortable:true}, 
{name:'PID',index:'PID', width:100,sortable:false,editable:true,hidden:true}, 
{name:'SID',index:'SID', width:100,sortable:false,editable:true,edittype:"select",editoptions: {dataUrl: 'MyServletURL?action=comboSID'}}, 
], 
     rowNum:2, 
     rowList:[2,4,6], 
     pager: '#pager10_d', 
     sortname: 'PID', 
     viewrecords: true, 
     sortorder: "asc", 
     multiselect: true, 
     editurl: "MyServletName", 
     caption:"CRM_PROT_ACTIONS", 
}).navGrid('#pager10_d',{edit:true,add:true,del:true}); 

的Servlet

if(request.getParameter("action").equalsIgnoreCase("comboSID")) 
{ 
     String s[][] = select.getData("select SID from TABLE_NAME_HERE where PID='"+ param +"'"); //returns 2D array 
     StringBuffer strBuf = new StringBuffer(); 
     strBuf.append("<select>"); 
     for(int i=0;i<s.length;i++) 
     { 
      strBuf.append("<option>"); 
      strBuf.append(s[i][0]); 
      strBuf.append("</option>"); 
     } 
     strBuf.append("</select>"); 
     out.println(strBuf); 
} 
相關問題