0

I am developing a web application using JSP & Servlet (IDE: Eclipse, Database: Oracle10). I am using jqGrid to display records in tabular format.

I want to get values for combobox in jqGrid from servlet, so far I have done following.

I am accessing the array passed by Servlet in JSP scriplet.

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

Following is my 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});

But I am getting an exception in for loop line where I am assigning the editoptions to the combobox. Please let me know is there any mistake in code.

My another question is that is there any better way of assigning values to combobox in jqGrid from servlet (without using scriplet)?

Bhushan
  • 6,151
  • 13
  • 58
  • 91

2 Answers2

1

You should use dataUrl instead of value inside of editoptions to get values for combobox from the server. If the server return JSON data instead of HTML fragment you can use buildSelect to convert server response from dataUrl to the format which jqGrid need. The exact implementation could depend on the version of jqGrid which you use. You can use ajaxSelectOptions to change type or dataType of the corresponding Ajax request from default "html" to "json" (see here). See here an example of buildSelect implementation.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
0

To add combobox to jqgrid wih dynamic data..

I have done it using following way.

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);
}
Bhushan
  • 6,151
  • 13
  • 58
  • 91