<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
  <channel>
    <title>samuel_bai</title>
    <description></description>
    <link>http://samuel-bai.javaeye.com</link>
    <language>UTF-8</language>
    <copyright>Copyright 2003-2008, JavaEye.com</copyright>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <generator>JavaEye - 做最棒的软件开发交流社区</generator>
      <item>
        <title>分享：多层级联下拉框 :Tapestry组件</title>
        <author>samuel_bai</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://samuel-bai.javaeye.com">samuel_bai</a>&nbsp;
          链接：<a href="http://samuel-bai.javaeye.com/blog/23302" style="color:red;">http://samuel-bai.javaeye.com/blog/23302</a>&nbsp;
          发表时间: 2005年08月06日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          下面是我写的tapestry组件的源代码，欢迎大家使用<br />它实现了类似根据所选省过滤其下级市，选市过滤县的功能，大家只要正确构造idynamicpropertyselection就可以实现N级的关联下拉框操作，欢迎大家使用<br /><br />[code:1]public abstract class DynamicPropertySelection extends AbstractFormComponent &#123;<br /><br />    public abstract DynamicPropertySelection getParent&#40;&#41;;<br /><br />    public abstract IDynamicPropertySelectionModel getModel&#40;&#41;;<br /><br />    public abstract Object getValue&#40;&#41;;<br /><br />    public abstract void setValue&#40;Object value&#41;;<br /><br />    public abstract boolean isDisabled&#40;&#41;;<br /><br />    private String selfId = null;<br /><br />    private IScript script = null;<br /><br />    protected void finishLoad&#40;&#41; &#123;<br />        IEngine engine = getPage&#40;&#41;.getEngine&#40;&#41;;<br />        IScriptSource source = engine.getScriptSource&#40;&#41;;<br />        IResourceLocation specLocation = getSpecification&#40;&#41;.getLocation&#40;&#41;<br />                .getResourceLocation&#40;&#41;;<br /><br />        String scriptPath = &quot;DynamicPropertySelection.script&quot;;<br />        IResourceLocation scriptLocation = specLocation<br />                .getRelativeLocation&#40;scriptPath&#41;;<br />        script = source.getScript&#40;scriptLocation&#41;;<br />    &#125;<br /><br />    protected void renderComponent&#40;IMarkupWriter writer,<br />            IRequestCycle requestCycle&#41; &#123;<br />        IForm form = getForm&#40;requestCycle&#41;;<br />        this.selfId = form.getElementId&#40;this&#41;;<br /><br />        if &#40;form.isRewinding&#40;&#41;&#41; &#123;<br />            if &#40;isDisabled&#40;&#41;&#41; &#123;<br />                return;<br />            &#125;<br />            String optionValue = requestCycle.getRequestContext&#40;&#41;.getParameter&#40;<br />                    selfId&#41;;<br />            Object value = optionValue;<br />            setValue&#40;value&#41;;<br />            return;<br />        &#125;<br /><br />        writer.begin&#40;&quot;select&quot;&#41;;<br />        writer.attribute&#40;&quot;name&quot;, selfId&#41;;<br />        if &#40;isDisabled&#40;&#41;&#41; &#123;<br />            writer.attribute&#40;&quot;disabled&quot;, true&#41;;<br />        &#125;<br />        renderInformalParameters&#40;writer, requestCycle&#41;;<br />        if &#40;getParent&#40;&#41; == null&#41; &#123;<br />            writer.println&#40;&#41;;<br />            String key = &#40;String&#41; getModel&#40;&#41;.getKeys&#40;&#41;.get&#40;0&#41;;<br />            IPropertySelectionModel propertySelectionModel = getModel&#40;&#41;<br />                    .getPropertySelectionModel&#40;key&#41;;<br />            for &#40;int i = 0; i &lt; propertySelectionModel.getOptionCount&#40;&#41;; i++&#41; &#123;<br />                Object option = propertySelectionModel.getOption&#40;i&#41;;<br /><br />                writer.begin&#40;&quot;option&quot;&#41;;<br />                writer.attribute&#40;&quot;value&quot;, propertySelectionModel.getValue&#40;i&#41;&#41;;<br /><br />                if &#40;propertySelectionModel.getValue&#40;i&#41;.equals&#40;getValue&#40;&#41;&#41;&#41; &#123;<br />                    writer.attribute&#40;&quot;selected&quot;, &quot;selected&quot;&#41;;<br />                &#125;<br />                writer.print&#40;propertySelectionModel.getLabel&#40;i&#41;&#41;;<br />                writer.end&#40;&#41;;<br />                writer.println&#40;&#41;;<br />            &#125;<br />        &#125;<br />        writer.end&#40;&#41;;<br /><br />        Body body = Body.get&#40;requestCycle&#41;;<br /><br />        Map symbols = new HashMap&#40;&#41;;<br />        symbols.put&#40;&quot;dps&quot;, this&#41;;<br />        script.execute&#40;requestCycle, body, symbols&#41;;<br />    &#125;<br /><br />    public String getParentId&#40;&#41; &#123;<br />        return getParent&#40;&#41; == null ? null &#58; getParent&#40;&#41;.getId&#40;&#41;;<br />    &#125;<br /><br />    public String getSelfId&#40;&#41; &#123;<br />        return this.selfId;<br />    &#125;<br /><br />    public String getSelfModelName&#40;&#41; &#123;<br />        return getSelfId&#40;&#41; + &quot;_selectModels&quot;;<br />    &#125;<br /><br />    public String getLessChar&#40;&#41; &#123;<br />        return &quot;&lt;&quot;;<br />    &#125;<br /><br />    public String getScriptModel&#40;&#41; &#123;<br />        StringBuffer stringBuffer = new StringBuffer&#40;&#41;;<br />        String selectModelsName = getSelfModelName&#40;&#41;;<br />        stringBuffer.append&#40;&quot;var &quot;&#41;.append&#40;selectModelsName&#41;.append&#40;<br />                &quot; = new Array&#40;&quot;&#41;.append&#40;getModel&#40;&#41;.getPropertySelectionCount&#40;&#41;&#41;<br />                .append&#40;&quot;&#41;;\n&quot;&#41;;<br />        Iterator iter = getModel&#40;&#41;.getKeys&#40;&#41;.iterator&#40;&#41;;<br />        int j = 0;<br />        while &#40;iter.hasNext&#40;&#41;&#41; &#123;<br />            String key = &#40;String&#41; iter.next&#40;&#41;;<br />            IPropertySelectionModel propertySelectionModel = getModel&#40;&#41;<br />                    .getPropertySelectionModel&#40;key&#41;;<br />            stringBuffer.append&#40;&quot;var optionModels = new Array&#40;&quot;&#41;.append&#40;<br />                    propertySelectionModel.getOptionCount&#40;&#41;&#41;.append&#40;&quot;&#41;;\n&quot;&#41;;<br />            for &#40;int i = 0; i &lt; propertySelectionModel.getOptionCount&#40;&#41;; i++&#41; &#123;<br />                stringBuffer.append&#40;&quot;optionModels&#91;&quot;&#41;.append&#40;i&#41;.append&#40;<br />                        &quot;&#93; = new OptionModel&#40;'&quot;&#41;.append&#40;<br />                        propertySelectionModel.getValue&#40;i&#41;&#41;.append&#40;&quot;','&quot;&#41;<br />                        .append&#40;propertySelectionModel.getLabel&#40;i&#41;&#41;.append&#40;<br />                                &quot;'&#41;;\n&quot;&#41;;<br />            &#125;<br />            stringBuffer.append&#40;selectModelsName&#41;.append&#40;&quot;&#91;&quot;&#41;.append&#40;j++&#41;<br />                    .append&#40;&quot;&#93; = new SelectModel&#40;'&quot;&#41;.append&#40;key&#41;.append&#40;<br />                            &quot;',optionModels&#41;;\n&quot;&#41;;<br />        &#125;<br /><br />        return stringBuffer.toString&#40;&#41;;<br />    &#125;<br /><br />&#125;[/code:1]<br />js文件<br />[code:1]function OptionModel&#40;value,text&#41; &#123;<br />	this.value = value;<br />	this.text = text;<br />&#125;<br />function SelectModel&#40;key,optionModels&#41; &#123;<br />	this.key = key;<br />	this.optionModels = optionModels;<br />&#125;<br />function updateSelectOptions&#40;selectObject,optionModels,selected&#41; &#123;<br />	while&#40;selectObject.length &gt; 0&#41; &#123;<br />		selectObject.remove&#40;selectObject.length - 1&#41;;<br />	&#125;<br />	for&#40;var i = 0 ;i &lt; optionModels.length ; i ++&#41; &#123;<br />		var oOption = document.createElement&#40;&quot;OPTION&quot;&#41;;<br />		oOption.value = optionModels&#91;i&#93;.value;<br />		oOption.text = optionModels&#91;i&#93;.text;<br />		if&#40;oOption.value == selected&#41; &#123;<br />			oOption.selected = true;<br />		&#125;<br />		selectObject.add&#40;oOption&#41;;<br />	&#125;<br />	selectObject.fireEvent&#40;&quot;onchange&quot;&#41;;<br />&#125;[/code:1]<br />script文件<br />[code:1]<br />&lt;script&gt;<br /><br /><br />&lt;include-script resource-path=&quot;DynamicPropertySelection.js&quot;/&gt;<br /><br />&lt;input-symbol key=&quot;dps&quot; class=&quot;DynamicPropertySelection&quot; required=&quot;yes&quot;/&gt;                   <br />         <br />&lt;set key=&quot;form&quot; expression=&quot;dps.form.name&quot;/&gt;<br />&lt;set key=&quot;parentId&quot; expression=&quot;dps.parentId&quot;/&gt;<br />&lt;set key=&quot;selfId&quot; expression=&quot;dps.selfId&quot;/&gt;       <br />&lt;set key=&quot;selfValue&quot; expression=&quot;dps.value&quot;/&gt;<br />&lt;set key=&quot;selfModelName&quot; expression=&quot;dps.selfModelName&quot;/&gt;<br />&lt;set key=&quot;lessChar&quot; expression=&quot;dps.lessChar&quot;/&gt;  <br />     <br />&lt;let key=&quot;parentSelect&quot;&gt;<br />     document.$&#123;form&#125;.$&#123;parentId&#125;<br />&lt;/let&gt;<br /><br />&lt;let key=&quot;selfSelect&quot;&gt;<br />     document.$&#123;form&#125;.$&#123;selfId&#125;<br />&lt;/let&gt;<br />                   <br />&lt;body&gt;<br />$&#123;dps.scriptModel&#125;     	   <br />&lt;if expression=&quot;parentId != null&quot;&gt;<br />   function cmd_$&#123;parentId&#125;_onchange&#40;&#41;&#123;  <br />	    $&#123;parentSelect&#125;.onchange = cmd_update_$&#123;selfId&#125;;<br />	    cmd_update_$&#123;selfId&#125;&#40;&#41;;       <br />   &#125;         <br />   function cmd_update_$&#123;selfId&#125;&#40;&#41;&#123;<br />  	 var parentValue = $&#123;parentSelect&#125;&#40;$&#123;parentSelect&#125;.selectedIndex&#41;.value;<br />  	 for&#40;var i = 0 ; i $&#123;lessChar&#125; $&#123;selfModelName&#125;.length; i ++&#41; &#123;<br />  	 	var key = $&#123;selfModelName&#125;&#91;i&#93;.key;<br />  	 	if&#40;key == parentValue&#41; &#123;  	 	<br />		     updateSelectOptions&#40;$&#123;selfSelect&#125;,$&#123;selfModelName&#125;&#91;i&#93;.optionModels, '$&#123;selfValue&#125;'&#41;;    	 		<br />		     break;<br />  	 	&#125;<br />  	 &#125;<br />  &#125;<br />&lt;/if&gt;   <br />&lt;/body&gt;<br /><br />&lt;initialization&gt;<br />&lt;if expression=&quot;parentId != null&quot;&gt;   <br />    cmd_$&#123;parentId&#125;_onchange&#40;&#41;;<br />&lt;/if&gt;  <br />&lt;/initialization&gt;<br /><br />&lt;/script&gt;[/code:1]<br /><br />[code:1]public interface IDynamicPropertySelectionModel &#123;<br /><br />    public List getKeys&#40;&#41;;<br /><br />    public int getPropertySelectionCount&#40;&#41;;<br /><br />    public IPropertySelectionModel getPropertySelectionModel&#40;String key&#41;;<br /><br />&#125;[/code:1]
          <br/>
          <span style="color:red;">
            <a href="http://samuel-bai.javaeye.com/blog/23302#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Sat, 06 Aug 2005 19:14:19 +0800</pubDate>
        <link>http://samuel-bai.javaeye.com/blog/23302</link>
        <guid>http://samuel-bai.javaeye.com/blog/23302</guid>
      </item>
  </channel>
</rss>