使用Struts2-jQuery-grid插件在可编辑的网格列中使用下拉列表 [英] Using a drop-down list in an editable grid column using Struts2-jQuery-grid plugin

查看:108
本文介绍了使用Struts2-jQuery-grid插件在可编辑的网格列中使用下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用

I'm trying to populate a drop down list in a grid column using the Struts2-jQuery-grid-3.7.0 plugin as follows.

<s:url id="dataURL" action="CategoryList" namespace="/admin_side"/>

<sjg:gridColumn name="subCategory.category.catName" 
                index="subCategory.category.catName" 
                edittype="select"
                searchtype="select" 
                formoptions="{label:'Select'}" 
                surl="%{dataURL}" 
                editoptions="{dataUrl : '%{dataURL}'}"
                editrules="{required: true}" 
                title="Category" width="200" 
                sortable="true" search="true" 
                editable="true" sorttype="text"/>

CategoryList动作所映射的动作如下.

And the action where the CategoryList action is mapped is as follows.

@Namespace("/admin_side")
@ResultPath("/WEB-INF/content")
@ParentPackage(value="struts-default")
public final class CategoryList extends ActionSupport implements Serializable {
    @Autowired
    private final transient Service service = null;
    private List<Category>categories = new ArrayList<Category>();

    private static final long serialVersionUID = 1L;

    public List<Category> getCategories() {
        return categories;
    }

    @Action(value = "CategoryList",
            results = {
                @Result(name = ActionSupport.SUCCESS, location = "Product.jsp"),
                @Result(name = ActionSupport.INPUT, location = "Product.jsp")},
            interceptorRefs = {
                @InterceptorRef(value = "defaultStack", params = {"validation.validateAnnotatedMethodOnly", "true", "validation.excludeMethods", "load"})})
    public String load() throws Exception {
        System.out.println("load called...");
        categories = service.getCatgeoryList();
        return ActionSupport.SUCCESS;
    }
}

单击网格上的给定编辑链接时,将执行load()方法,并从数据库中加载类别列表.

When a given edit link on the grid is clicked, the load() method is executed where a list of categories is loaded from the database.

但是,网格中的列表在编辑模式下(单击编辑链接时)不显示任何内容.我找不到可以证明这种情况的相对示例.

The list in the grid however, displays nothing in edit mode (when the edit link is clicked). I cannot find relative examples that may demonstrate this kind of thing.

特别是如何填充此下拉列表,如何分别使用catName属性和使用catId(属于Long类型的)属性来赋予此下拉标签(而列表中的category具有许多其他属性)?

How to populate this drop down list especially, how to give this drop down labels using the catName property and values using the catId (of Long type) property separately (while category in the list has many other attributes)?

我找不到将java.util.List<E>映射到<sjg:grid>的相关示例.

I don't find relevant examples to map a java.util.List<E> to <sjg:grid>.

subCategory.category.catNameProduct实体的嵌套属性.

subCategory.category.catName is a nested property of Product entity.

在这种情况下,即使在填充列表之后,也应注意,此列的显示值为catName(字符串类型的类别名称).但是,要设置为Product实例的所选项目的值应为catId(类型为Long的类别ID),由于此列的名称为subCategory.category.catName,因此似乎不太可能.

In this case, even after populating the list, it should also be noted that the display value of this column is catName (category name of type String). The value of the selected item however, to be set to an instance of Product should be catId (category id of type Long) which doesn't seem possible as the name of this column is subCategory.category.catName.

直观地讲,如果我可以正确地设想列表已被填充,则catId(subCategory.category.catId)将被映射到catName(subCategory.category.catName),这是错误的.

Intuitively, catId (subCategory.category.catId) would be mapped to catName (subCategory.category.catName) which would be wrong, if I could envision correctly as if the list were already populated.

推荐答案

在Struts 2中,可以通过<s:select>标签呈现HTML下拉列表.要为下拉列表自动选择默认值,只需在标记中声明value属性,然后相应地设置默认值即可.

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:

一个Java列表,用于为下拉框生成选择选项.

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");
    }
    //...
}

标记以呈现HTML下拉框. value=defaultSearchEngine将调用相应的Action类getDefaultSearchEngine()方法以返回默认搜索引擎值.

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" />

在此示例中,下拉框将自动选择"yahoo.com"作为默认选项.

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

将Struts2 jQuery插件标签库添加到您的JSP

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"%>

在您的Head Tag中启用jQuery Grid插件

Enable jQuery Grid Plugin in your Head Tag

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

更新:

update:

在您的JSP中指定一个编辑网址

Specify an Edit Url in your JSP

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

并通过在JSP中设置以下属性来启用编辑"

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"/>

更新:1

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> 

这篇关于使用Struts2-jQuery-grid插件在可编辑的网格列中使用下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆