处理多个下拉选择的更好方法
我有一个包含多个下拉菜单的菜单。我添加了代码,但目前它完全驻留在代码隐藏文件中。我想使用任何设计模式以简单且整洁的方式处理各种选择。
报告生成标准准备如下:
报告类型下拉选项包括:
- 按类型划分的方案
- 计划方面
- 按地区划分
- 按块
- 全部
默认情况下,仅启用第一个DropDown。从此下拉菜单中选择一个选项,即可启用相应的下拉菜单。
不仅如此,当从任何这些 DropDowns 或 Scheme 中选择项目时,
DropDown。Scheme
、District
和 Block
DropDowns 的值也会发生变化使用 AJAX 输入
它经常涉及各种 SQL 查询和启用/禁用 DropDowns。我现在的代码因许多 IF
和 EndIfs
而变得混乱。
我想知道是否可以使用观察者模式或任何使用类的方法来简化此操作。有什么方法可以使下拉菜单的多重选择和填充变得易于管理和简单吗?
在下面进行了编辑以明确要求
让我进一步澄清。
第一个 DropDown 是关键的 DropDown,页面打开时默认启用。默认情况下禁用所有其他下拉菜单。但这并不意味着 Cascading DropDown 是正确的选择,因为子 DropDown 的选择是随机的。
整个计划是以易于理解的形式简化每个 DropDown 的代码。根据选择选择正确的查询涉及许多 If 和 ElseIf。
例如:用户从报告类型主下拉列表中选择地区报告
。在本例中,启用了三个子下拉菜单,即。
Scheme Type
Scheme
District
如果用户从方案类型列表中选择“全部”,则所有类别中的所有类型的方案都会填充到方案下拉列表中。
如果用户从以下选项中选择特定的方案类型:城市、农村或其他,方案下拉菜单将过滤方案的名称。
现在,Scheme DropDown 也有一个选项 ALL。用户可以选择全部或选择任何特定方案。
区也一样。如果选择ALL,则Scheme DropDown中的方案将采用所有地区的所有方案,但如果选择特定地区,则Scheme DropDown必须填写该地区的过滤方案。
请注意,在这种情况下,我们现在以相反的顺序移动,因为 District DropDown 再次过滤了计划 DropDown。
这同样适用于块下拉菜单。
除了所选选项之外,还有多种条件需要检查。假设用户没有选择任何选项或者用户选择了全部。
我想用每个 DropDown 的名称创建单独的类。这些类应该不断听到 DropDown 中任何更改的通知(观察者)。
我想我能够澄清。
I have a menu with multiple DropDowns. I added code but at present, it entirely resides in the code-behind file. I want to use any design pattern to handle in an easy and uncluttered way the variety of choices.
The report generation criteria is prepared as follows:
Report Type DropDown options include:
- Scheme Type-wise
- Scheme-wise
- District-wise
- Block-wise
- All
By default, only the first DropDown is enabled. Selecting an option from this DropDown, enables the respective DropDowns.
Not only this, values of Scheme
, District
and Block
DropDowns also change when an item is selected from any of these DropDowns or Scheme Type
DropDown using AJAX.
It involves a variety of SQL queries and Enabling/Disabling DropDowns often. My present code has become cluttered with many IF
and EndIfs
.
I want to know whether Observer pattern
or any approach using Classes
be used to simplify this operation. Any way to make this multiple selections and filling of DropDowns manageable and simple?
Edited below to clear requirements
Let me clarify further.
The first DropDown is the key DropDown which is enabled by default when the page opens. All the other DropDowns are disabled by default. But this does not mean that Cascading DropDown is the correct choice because, the selection from the child DropDowns is random.
The whole plan is to simplify the code in an understandable form for each DropDown. There are many Ifs and ElseIfs involved to pick the correct query depending upon the selection.
For example: The user selects, District-wise report
from the Report Type primary DropDown. In this case, three child DropDowns are enabled, viz.
Scheme Type
Scheme
District
If user selects "ALL" from Scheme Types List, all types of schemes in all categories gets filled in the Scheme DropDown.
If user selects a particular Scheme Type from the options: Urban, Rural or Other, the Scheme DropDown filters the name of the schemes.
Now, Scheme DropDown also has an option ALL. The user can select ALL or pick any particular scheme.
Same is with District. If ALL is selected, schemes in the Scheme DropDown takes all schemes in all districts, but if a particular district is selected, the Scheme DropDown must fill the filtered schemes of this district.
Please note that in this case, we are now moving in a reverse order because District DropDown is again filtering the Scheme DropDown.
The same is applicable with the Block DropDown.
There are variety of conditions to be checked other the selected option. Suppose the user didn't select any option or the user selects ALL.
I want to create separate classes with the names of each DropDown. These classes should keep hearing notifications (Observer) for any changes in the DropDown.
I guess I was able to clarify.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 AJAX Control Toolkit 是符合您要求的解决方案。
在AJAX控件工具包中,有CascadingDropDown控件
标签语法:
然后您需要为其创建一个 Web 服务和一些 Web 方法,其方法签名如下:
UPDATE
您已经使用 if-Else-If 做了类似的事情,答案是基于假设给出的,并且纯粹是实现的示例。
希望我的回答对您有所帮助
谢谢并问候
严厉的贝德
Using AJAX Control Toolkit is the solution that matches your requirements.
In AJAX Control Toolkit, there is CascadingDropDown Control
Tag Syntax:
And then you need to create an Web Service and few Web Method(s) for it having following method signature,
UPDATE
You have do something like this using if-Else-If, Answer is given on assumptions and purely an example to the implementations.
I hope that my answer helps you
Thanks and Regards
Harsh Baid
既然看起来你可以使用 Javascript 完成所有这些客户端操作,为什么你要如此频繁地查询数据库呢?您应该保留数据客户端和服务器端。您可以调用所有相关的下拉数据,对其进行缓存,然后从客户端调用 WebMethod,该 WebMethod 将返回包含您需要的数据的 JSON。对于服务器端对象持久性,您应该查看 Entity Framework 4.1,其中每个对象都是表的表示。
我会使用 jQuery 和 Knockout JS 或仅使用 jQuery,使用 JSON 将数据保存在客户端,无需往返于 sql 服务器。
这是 Knockout JS 实际应用的一个很好的例子。
http://knockoutjs.com/examples/cartEditor.html
当然,除非数据变化非常频繁。
Since it seems you can do all of this client side using Javascript why are you querying your database so much? You should persist the data client side and server side. You could call all related drop-down data, cache it and from the client side call a WebMethod that would return a JSON with the data you need. For server side object persistence you should check out the Entity Framework 4.1 where each object is a representation of a table.
I would use jQuery with Knockout JS or just jQuery for it, persisting the data on the client side using JSON, there is no need to go back and forward to the sql server.
Here's a nice example of Knockout JS in action.
http://knockoutjs.com/examples/cartEditor.html
Of course unless that data changes extremely frequently.
不完全是您正在寻找的内容,但您可能想查看我如何使用 jQuery 解决 mvc 中的类似问题。使用 web 方法 而不是 mvc 操作。
这是一个由 3 部分组成的故事,代码可在 bitbucket 上找到。您可以从第 2 部分开始,因为首先是创建演示应用程序并设置数据库。
http://blog. goranobradovic.com/2011/06/asp-net-mvc3-app-part-2-ajax-cascading-dropdown/
有两个下拉菜单的工作示例,但我对 3 和 4 使用相同的解决方案,没有问题。
更新
由于您有可用的源代码,我将仅在此处放置要更改的相关行。防止多个 ajax 调用的调整是将第一个选择中的选项值放入 id(或将 id 放入无关紧要的值),或者如果您无法更改 id 和值,则可以在依赖下拉列表中使用某些属性,但我会保留例如,很简单。然后,在主下拉列表的更改事件中,您检查是否需要加载目标:
如果您不想使用 id,则可以轻松更改此设置以使用 select 的某些自定义属性。
更新2
我刚刚看到你更新了问题。正如我所看到的,在某些情况下您需要刷新所有依赖的下拉列表。为此,您需要拥有需要在最后一个主选择选项的值中刷新的所有下拉列表的 id-s,即用“,”分隔,这样您就可以检查其是否有
val().split(", ")
等于依赖下拉列表的 id,或者您可以在 dependentend 中包含属性,其中包含需要刷新的所有选项的值。您在我的代码中看到 selectFromAjax 的选项是下拉列表中的选定值和主字段的名称。由于您可能需要在此处包含一些值以由服务器代码处理,因此我建议您使用第二种方式,即 - 将自定义属性添加到 dependentend 下拉列表中,您将在其中放置相关的 master 中选择选项的值对于该字段和依赖项应在选择时加载。你明白我的建议吗?
Not exactly what you are looking for, but you may want to check out how I resolved similar issue in mvc with jQuery. Same can be achieved in webforms using web methods instead of mvc actions.
It is a story in 3 parts, and code is available on bitbucket. You may start form part 2 as first is creation of demo app and setup of database.
http://blog.goranobradovic.com/2011/06/asp-net-mvc3-app-part-2-ajax-cascading-dropdown/
There is working example for 2 dropdowns, but I used same solution for 3 and 4 without problems.
UPDATE
As you have source available, I will put only relevant lines to change here. The adjustment to prevent multiple ajax calls would be putting values of options in first select to id's (or id's to values it does not matter), or you could use some attribute in dependent dropdown if you cannot change ids and values, but I will keep it simple for example. Then, in change event of master dropdown you check if target needs to be loaded:
You can easily change this if to use some custom attribute of select if you don't want use id.
UPDATE 2
I just saw that you updated question. As I see, there is a case where you need to refresh all dependent dropdowns. For this you need to have id-s of all dropdowns which need to be refreshed in last master select option's value, separeted i.e. with ",", so you can check if any of its
val().split(",")
is equal to id of dependent dropdown, or you can have attribute in dependend which contain values of all options for which it needs to refresh. You see in my code that options for selectFromAjax are selected value in dropdown and name of master field. As you probably need to have some your value in here to be handled by server code, I suggest that you use second way, that is - add custom attribute to dependend dropdown, in which you will put values of select options from master which are relevant for that field and dependent should load when selected.Do you understand what I suggest?
如果我正确理解您的范例,实际上,您有一个下拉菜单(报告类型)来确定是否按方案类型、方案、地区或街区(或以上所有)进行选择。如果您没有“全部”选项,我建议要么只有两个下拉菜单(一个用于报告类型,另一个其标签更改以匹配),或者消除“报告类型”下拉菜单并在每个其他按钮旁边放置一个单选按钮以选择您想要的。当包含“全部”选项时,您可能不会使事情变得更加复杂;例如,您可以再添加一个单选按钮并启用所有四个下拉列表。
但是,您询问观察者模式。从 GoF 来看,观察者模式在以下情况下很有用:
我不完全确定这些情况是否适用于此。第二种情况与您的问题有一些相似之处,但您确实知道需要更改什么以及如何更改。如果您要做的唯一更新是按报告类型进行更新,则只需启用或禁用右侧下拉菜单即可。然而,你说其他下拉菜单相互影响,可能是在“全部”选项中。即使在这种情况下,我也不确定观察者模式本身是最有帮助的。由于您使用 SQL 填充下拉列表,我猜测您可能会使用许多不同的存储过程(或临时查询),具体取决于您需要的参数。我可能建议每个下拉列表只包含一个查询,明智地使用
NULL
。例如,要从其他值填充 Block,您可能需要:在不知道您的数据库的情况下,我不知道具体什么会起作用,但想法是您只需为您不知道的任何内容传递
NULL
还已经选择了。这也许可以稍微简化你的代码。如果这不能回答您的问题,请告诉我我可以澄清什么。
If I'm understanding your paradigm correctly, really, you have one drop-down (Report Type) that determines whether to select by Scheme Type, Scheme, District, or Block (or all of the above). If you did not have the "All" option, I would suggest either having only two drop-downs (one for Report Type and one whose label changes to match) or eliminating the Report Type drop-down and putting a radio button next to each of the others to select which you want. When including the "All" option, you might not to complicate things much more; for instance, you could add one more radio button and enable all four drop-down lists.
However, you ask about the Observer Pattern. From GoF, the Observer Pattern is useful when:
I'm not entirely sure any of these situations applies here. The second situation has some similarities to your problem, but you do know what needs to be changed and how. If the only updating you are doing is by Report Type, it's just a matter of enabling or disabling the right drop-down menu. However, you say that the other drop-downs affect each other, probably in the "All" option. Even in this case, I'm not sure the Observer Pattern per se is the most helpful. Since you are populating the drop-downs using SQL, I'm guessing you might be using a number of different stored procedures (or ad-hoc queries) depending which parameters you need. What I might suggest instead is to have only one query per drop-down, making judicious use of
NULL
. For example, to populate Block from the other values, you might have:Without knowing your database, I do not know exactly what will work, but the idea is that you just pass
NULL
for anything you do not yet have selected. This could perhaps simplify your code a bit.If this doesn't answer your question, let me know what I can clarify.
有一个简单的方法可以实现这一目标,请遵循以下方法:
首先,只需添加一个 AjaxUpdatePanel...然后在下拉列表中将 AutoPostBack 属性设置为 true,
进一步只需在 OnSelectedIndexChanged 事件上添加处理程序即可启用第二个下拉菜单。
There is a Simple Way to Achieve this, follow this approach:
First of All, just add an AjaxUpdatePanel... and then in drop down set AutoPostBack property to true,
further just add the handler on OnSelectedIndexChanged event to enable second drop down..