简体   繁体   English

Struts2 jQuery插件Doubleselect第二个下拉列表的初始值

[英]Struts2 jQuery Plugin Doubleselect initial value for second dropdown

I have a little (i hope) problem trying to use Doubleselect element from Struts2 jQuery plugin. 我在尝试使用Struts2 jQuery插件中的Doubleselect元素时遇到了一些(我希望)问题。 I have followed the sample without problems, and the behaviour is as expected when I add a new record (add new record to database), but when i try to edit an existing one the second select does not load the stored value for the record being edited. 我遵循了示例,没有问题,当我添加新记录(将新记录添加到数据库)时,行为与预期的一样,但是当我尝试编辑现有记录时,第二选择不加载正在存储的记录的值编辑。

Any help? 有什么帮助吗?

Code and configuration follows: 代码和配置如下:

JSP code JSP代码

<s:form id="ingresoForm" action="saveIngreso" method="post" validate="true" cssClass="well form-horizontal">
    <s:hidden key="ingreso.id"/>
    <s:hidden key="cliente" id="cliente"/>

    <div class="type-text">
            <label for="cliente">Cliente: </label>
            <s:url var="remoteurl" action="ajax/clienteProyectoSelectSource"/>
            <sj:select
                href="%{remoteurl}"
                id="clienteSelect"
                onChangeTopics="reloadsecondlist"
                name="ingreso.cliente.id"
                list="clientes"
                listKey="id"
                listValue="nombre"
                emptyOption="false"
                headerKey="-10"
                headerValue="Por favor seleccione un cliente"/>
        </div>
        <div class="type-text">
            <label for="Proyecto">Proyecto: </label>
            <sj:select
                href="%{remoteurl}"
                id="proyectoSelect"
                formIds="ingresoForm"
                reloadTopics="reloadsecondlist"
                name="ingreso.proyecto.id" 
                list="proyectos"
                listKey="id"
                listValue="nombre"
                emptyOption="false"
            />
        </div>

Action Code 动作码

public class ClienteProyectoSelectSourceAjaxAction extends BaseAction {

private List<Cliente> clientes;
private List<Proyecto> proyectos;
private String cliente;
private GenericManager<Cliente, Long> clienteManager;


@Override
public String execute() {

    clientes = clienteManager.getAll();

    if (cliente != null && cliente.length() > 0 && !cliente.equals("-10")) {
        proyectos = clienteManager.get(new Long(cliente)).getProyectos();
    }
    return Action.SUCCESS;
}

Action Declaration 行动宣言

<package name="example" extends="json-default"  namespace="/ajax">
    <action name="clienteProyectoSelectSource" class="com.queres.smtm.webapp.action.ajax.ClienteProyectoSelectSourceAjaxAction">
        <result type="json"/>
    </action>
</package>

Ingreso entity (model) Ingreso实体(模型)

@Entity
@Table(name = "ingreso")
public class Ingreso extends BaseObject {

// Campos comunes
private Long id;
private TipoIngreso tipo;
private String observaciones;
private BigDecimal importe;
private BigDecimal tipoIVA;
private Date fechaPrevistaCobro;
private Date fechaEfectivaCobro;

// Campos para facturas
private String numeroFactura;
private Cliente cliente;
private Proyecto proyecto;
private TipoServicio servicio;
private Date fechaEmision;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}


@Enumerated(EnumType.STRING)
public TipoIngreso getTipo() {
    return tipo;
}

public void setTipo(TipoIngreso tipo) {
    this.tipo = tipo;
}

public String getObservaciones() {
    return observaciones;
}

public void setObservaciones(String observaciones) {
    this.observaciones = observaciones;
}

public BigDecimal getImporte() {
    return importe;
}

public void setImporte(BigDecimal importe) {
    this.importe = importe;
}

public BigDecimal getTipoIVA() {
    return tipoIVA;
}

public void setTipoIVA(BigDecimal tipoIVA) {
    this.tipoIVA = tipoIVA;
}

@Temporal(javax.persistence.TemporalType.DATE)
@Field
public Date getFechaPrevistaCobro() {
    return fechaPrevistaCobro;
}

public void setFechaPrevistaCobro(Date fechaPrevistaCobro) {
    this.fechaPrevistaCobro = fechaPrevistaCobro;
}

@Temporal(javax.persistence.TemporalType.DATE)
@Field
public Date getFechaEfectivaCobro() {
    return fechaEfectivaCobro;
}

public void setFechaEfectivaCobro(Date fechaEfectivaCobro) {
    this.fechaEfectivaCobro = fechaEfectivaCobro;
}

public String getNumeroFactura() {
    return numeroFactura;
}

public void setNumeroFactura(String numeroFactura) {
    this.numeroFactura = numeroFactura;
}

@ManyToOne
public Cliente getCliente() {
    return cliente;
}

public void setCliente(Cliente cliente) {
    this.cliente = cliente;
}

@ManyToOne
public Proyecto getProyecto() {
    return proyecto;
}

public void setProyecto(Proyecto proyecto) {
    this.proyecto = proyecto;
}

@Enumerated(EnumType.STRING)
public TipoServicio getServicio() {
    return servicio;
}

public void setServicio(TipoServicio servicio) {
    this.servicio = servicio;
}

@Temporal(javax.persistence.TemporalType.DATE)
@Field
public Date getFechaEmision() {
    return fechaEmision;
}

public void setFechaEmision(Date fechaEmision) {
    this.fechaEmision = fechaEmision;
}

@Override
public int hashCode() {
    int hash = 3;
    hash = 43 * hash + (this.numeroFactura != null ? this.numeroFactura.hashCode() : 0);
    hash = 43 * hash + (this.fechaEmision != null ? this.fechaEmision.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Ingreso other = (Ingreso) obj;
    if ((this.numeroFactura == null) ? (other.numeroFactura != null) : !this.numeroFactura.equals(other.numeroFactura)) {
        return false;
    }
    if (this.fechaEmision != other.fechaEmision && (this.fechaEmision == null || !this.fechaEmision.equals(other.fechaEmision))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "Ingreso{" + "id=" + id + ", tipo=" + tipo + ", observaciones=" + observaciones + ", importe=" + importe + ", tipoIVA=" + tipoIVA + ", fechaPrevistaCobro=" + fechaPrevistaCobro + ", fechaEfectivaCobro=" + fechaEfectivaCobro + ", numeroFactura=" + numeroFactura + ", cliente=" + cliente + ", proyecto=" + proyecto + ", servicio=" + servicio + ", fechaEmision=" + fechaEmision + '}';
}

} }

Thx in advance 提前Thx

Problem solved. 问题解决了。 It seems that jquery-plugin work perfectly, as usual the error was between the keyboard and the chair... 似乎jquery-plugin可以正常工作,像往常一样,错误出在键盘和椅子之间。

I forgot to load the data list for the second select, so jquery was unable to select the aproppiate value. 我忘记为第二个选择加载数据列表,因此jquery无法选择合适的值。

So, the solution was to ensure that the second list (proyectos) was loaded when the user edits an element. 因此,解决方案是确保在用户编辑元素时加载第二个列表(proyectos)。

I add a flag (cliente) as a hidden element on JSP and preloaded it from the main action, so I can check from the Ajax Action if it is necessary to populate the second list. 我在JSP上添加了一个标志(cliente)作为隐藏元素,并从main操作中预加载了它,因此我可以从Ajax Action中检查是否有必要填充第二个列表。

Ingreso Action (main action for the view) Ingreso Action(视图的主要操作)

public class IngresoAction extends BaseAction implements Preparable {

private String cliente;

public String edit() {
    if (id != null) {
        ingreso = ingresoManager.get(id);
        cliente = Long.toString(ingreso.getCliente().getId());

    } else {
        ingreso = new Ingreso();
    }

    return SUCCESS;
}

public String getCliente() {
    return cliente;
}

public void setCliente(String cliente) {
    this.cliente = cliente;
}

<...>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM