簡體   English   中英

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

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

我在嘗試使用Struts2 jQuery插件中的Doubleselect元素時遇到了一些(我希望)問題。 我遵循了示例,沒有問題,當我添加新記錄(將新記錄添加到數據庫)時,行為與預期的一樣,但是當我嘗試編輯現有記錄時,第二選擇不加載正在存儲的記錄的值編輯。

有什么幫助嗎?

代碼和配置如下:

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>

動作碼

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;
}

行動宣言

<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
@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

問題解決了。 似乎jquery-plugin可以正常工作,像往常一樣,錯誤出在鍵盤和椅子之間。

我忘記為第二個選擇加載數據列表,因此jquery無法選擇合適的值。

因此,解決方案是確保在用戶編輯元素時加載第二個列表(proyectos)。

我在JSP上添加了一個標志(cliente)作為隱藏元素,並從main操作中預加載了它,因此我可以從Ajax Action中檢查是否有必要填充第二個列表。

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