Using a drop-down list in an editable grid column using Struts2-jQuery-grid plugin

眉间皱痕 提交于 2019-12-05 11:36:59
jmail

In Struts 2, the HTML drop down list can be rendered via <s:select> tag. To auto select a default value for a drop down list, just declared a “value” attribute in the tag, and set the default value accordingly.

Example:

A Java list to generate the select options for the drop down box.

//...
public class SelectAction extends ActionSupport {

    private List<String> searchEngine;
    private String yourSearchEngine;

    //set default value
    public String getDefaultSearchEngine() {
        return "yahoo.com";
    }

    public SelectAction() { 
        searchEngine = new ArrayList<String>();
        searchEngine.add("google.com");
        searchEngine.add("bing.com");
        searchEngine.add("yahoo.com");
        searchEngine.add("baidu.com");
    }
    //...
}

tag to render the HTML drop down box. The value=”defaultSearchEngine” will call the corresponds Action class getDefaultSearchEngine() method to return a default search engine value.

Many Actions share common concerns. Some Actions need input validated. Other Actions may need a file upload to be pre-processed. Another Action might need protection from a double submit. Many Actions need drop-down lists and other controls pre-populated before the page displays.

<s:select label="What's your favor search engine" 
    headerKey="-1" headerValue="Select Search Engines"
    list="searchEngine" 
    name="yourSearchEngine" 
    value="defaultSearchEngine" />

In this example, the drop down box will auto select the “yahoo.com” as the default option.

Add Struts2 jQuery Plugin Tag lib to your JSP

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>

Enable jQuery Grid Plugin in your Head Tag

<sj:head jqueryui="true" jquerytheme="redmond" />

update:

Specify an Edit Url in your JSP

<s:url var="editurl" action="edit-grid-entry"/>

And enable Edit by setting following attributes in your JSP

 <sjg:grid ... editurl="%{editurl}" ...>

then define which Column should be editable

<sjg:gridColumn ...
  editable="true" 
  edittype="<type>" 
  editoptions="{<options>}"
  editrules="{<rules>}"
... />

Example for an Edit Options:

<sjg:gridColumn 
name="country" 
index="country" 
title="Country" 
editable="true" 
edittype="select" 
editoptions="{value:'France:France;USA:USA;Australia:Australia;Norway:Norway;Spain:Spain'}"/>

Example for an Edit Rules:

<sjg:gridColumn name="creditLimit"
              index="creditLimit"
              title="Credit Limit" 
              editable="true"
              editrules="{
                             number: true,
                             required: true,
                             minValue : 100.0,
                             maxValue : 10000.0
                         }"
              formatter="currency"/>

update:1

<select name="catid" size="15" id="dataURL" multiple="multiple">    
    <option value="1 - One ">1 - One </option> 
    <option value="2 - Two">2 - Two</option> 
    <option value="3 - Three">3 - Three</option> 
    <option value="4 - Four">4 - Four</option> 
    <option value="5 - Five">5 - Five</option> 
</select> 

Regarding to the documentation connected with gridColumn and editoptions property:

dataUrl

This option is valid only for elements of type select - i.e., edittype:select and should be the URL to get the AJAX data for the select element. The data is obtained via an AJAX call and should be a valid HTML select element with the desired options One…. You can use option group.

your action has to return valid select element. That means, that your Product.jsp result need to look like this:

<%@ taglib prefix="s" uri="/struts-tags"%>
<s:select list="categories" key="id" value="name"/>

Reference: http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!