Tapestry省市级联

来源:互联网 发布:淘宝刷好评会被判刑吗 编辑:程序博客网 时间:2024/06/06 18:19
 面是我写的tapestry组件的源代码,欢迎大家使用
它实现了类似根据所选省过滤其下级市,选市过滤县的功能,大家只要正确构造idynamicpropertyselection就可以实现N级的关联下拉框操作,欢迎大家使用

java代码: 

public abstract class DynamicPropertySelection extends AbstractFormComponent {

    public abstract DynamicPropertySelection getParent();

    public abstract IDynamicPropertySelectionModel getModel();

    public abstract Object getValue();

    public abstract void setValue(Object value);

    public abstract boolean isDisabled();

    private String selfId = null;

    private IScript script = null;

    protected void finishLoad() {
        IEngine engine = getPage().getEngine();
        IScriptSource source = engine.getScriptSource();
        IResourceLocation specLocation = getSpecification().getLocation()
                .getResourceLocation();

        String scriptPath = "DynamicPropertySelection.script";
        IResourceLocation scriptLocation = specLocation
                .getRelativeLocation(scriptPath);
        script = source.getScript(scriptLocation);
    }

    protected void renderComponent(IMarkupWriter writer,
            IRequestCycle requestCycle) {
        IForm form = getForm(requestCycle);
        this.selfId = form.getElementId(this);

        if (form.isRewinding()) {
            if (isDisabled()) {
                return;
            }
            String optionValue = requestCycle.getRequestContext().getParameter(
                    selfId);
            Object value = optionValue;
            setValue(value);
            return;
        }

        writer.begin("select";
        writer.attribute("name", selfId);
        if (isDisabled()) {
            writer.attribute("disabled", true);
        }
        renderInformalParameters(writer, requestCycle);
        if (getParent() == null) {
            writer.println();
            String key = (String) getModel().getKeys().get(0);
            IPropertySelectionModel propertySelectionModel = getModel()
                    .getPropertySelectionModel(key);
            for (int i = 0; i < propertySelectionModel.getOptionCount(); i++) {
                Object option = propertySelectionModel.getOption(i);

                writer.begin("option";
                writer.attribute("value", propertySelectionModel.getValue(i));

                if (propertySelectionModel.getValue(i).equals(getValue())) {
                    writer.attribute("selected", "selected";
                }
                writer.print(propertySelectionModel.getLabel(i));
                writer.end();
                writer.println();
            }
        }
        writer.end();

        Body body = Body.get(requestCycle);

        Map symbols = new HashMap();
        symbols.put("dps", this);
        script.execute(requestCycle, body, symbols);
    }

    public String getParentId() {
        return getParent() == null ? null : getParent().getId();
    }

    public String getSelfId() {
        return this.selfId;
    }

    public String getSelfModelName() {
        return getSelfId() + "_selectModels";
    }

    public String getLessChar() {
        return "<";
    }

    public String getScriptModel() {
        StringBuffer stringBuffer = new StringBuffer();
        String selectModelsName = getSelfModelName();
        stringBuffer.append("var ".append(selectModelsName).append(
                " = new Array(".append(getModel().getPropertySelectionCount())
                .append(";/n";
        Iterator iter = getModel().getKeys().iterator();
        int j = 0;
        while (iter.hasNext()) {
            String key = (String) iter.next();
            IPropertySelectionModel propertySelectionModel = getModel()
                    .getPropertySelectionModel(key);
            stringBuffer.append("var optionModels = new Array(".append(
                    propertySelectionModel.getOptionCount()).append(";/n";
            for (int i = 0; i < propertySelectionModel.getOptionCount(); i++) {
                stringBuffer.append("optionModels[".append(i).append(
                        "] = new OptionModel('".append(
                        propertySelectionModel.getValue(i)).append("','"
                        .append(propertySelectionModel.getLabel(i)).append(
                                "');/n";
            }
            stringBuffer.append(selectModelsName).append("[".append(j++)
                    .append("] = new SelectModel('".append(key).append(
                            "',optionModels);/n";
        }

        return stringBuffer.toString();
    }

}


js文件
java代码: 

function OptionModel(value,text) {
        this.value = value;
        this.text = text;
}
function SelectModel(key,optionModels) {
        this.key = key;
        this.optionModels = optionModels;
}
function updateSelectOptions(selectObject,optionModels,selected) {
        while(selectObject.length > 0) {
                selectObject.remove(selectObject.length - 1);
        }
        for(var i = 0 ;i < optionModels.length ; i ++) {
                var oOption = document.createElement("OPTION";
                oOption.value = optionModels[i].value;
                oOption.text = optionModels[i].text;
                if(oOption.value == selected) {
                        oOption.selected = true;
                }
                selectObject.add(oOption);
        }
        selectObject.fireEvent("onchange";
}


script文件
java代码: 





<include-script resource-path="DynamicPropertySelection.js"/>

<input-symbol key="dps" class="DynamicPropertySelection" required="yes"/>                   
         
<set key="form" expression="dps.form.name"/>
<set key="parentId" expression="dps.parentId"/>
<set key="selfId" expression="dps.selfId"/>       
<set key="selfValue" expression="dps.value"/>
<set key="selfModelName" expression="dps.selfModelName"/>
<set key="lessChar" expression="dps.lessChar"/> 
     
<let key="parentSelect">
     document.${form}.${parentId}
</let>

<let key="selfSelect">
     document.${form}.${selfId}
</let>
                   

${dps.scriptModel}                
<if expression="parentId != null">
   function cmd_${parentId}_onchange(){ 
            ${parentSelect}.onchange = cmd_update_${selfId};
            cmd_update_${selfId}();       
   }         
   function cmd_update_${selfId}(){
          var parentValue = ${parentSelect}(${parentSelect}.selectedIndex).value;
          for(var i = 0 ; i ${lessChar} ${selfModelName}.length; i ++) {
                  var key = ${selfModelName}[i].key;
                  if(key == parentValue) {                 
                     updateSelectOptions(${selfSelect},${selfModelName}[i].optionModels, '${selfValue}');                           
                     break;
                  }
          }
  }
</if>   


<initialization>
<if expression="parentId != null">   
    cmd_${parentId}_onchange();
</if> 
</initialization>





java代码: 

public interface IDynamicPropertySelectionModel {

    public List getKeys();

    public int getPropertySelectionCount();

    public IPropertySelectionModel getPropertySelectionModel(String key);

}
loading...
原创粉丝点击