我有两个选择下拉菜单,其中第二个选择中的选项取决于第一个选择中选择的选项。
目前,我正在尝试弄清楚应该以何种方式从服务器返回数据,这取决于我设置过滤器的方式。
对于使用多选下拉列表过滤数据结构的最佳实践,我将不胜感激。
以防万一我正在使用当前稳定版本的 AngularJS (v1.3.15) 开发/测试的人对此感兴趣。
$scope.optionObjs = [
{
id: 1, name: 'option 1', desc: '',
elements: [
{ id: 9, name: 'option 11', desc: '', },
{ id: 10, name: 'option 12', desc: '', },
{ id: 11, name: 'option 13', desc: '', },
{ id: 12, name: 'option 14', desc: '', },
{ id: 13, name: 'option 15', desc: '', },
],
},
];
我希望在我的 html 中像下面的示例那样使用 Angular 过滤器,但这似乎没有用。
<select data-ng-model="firstSelect" data-ng-options="option.name for option in optionObjs"></select>
<select data-ng-model="secondSelect" data-ng-options="option.elements.name for option in optionObjs | filter:firstSelect"></select>
好吧,这对我来说太傻了。要使上述数据结构符合我的要求,只需简单更改为第二个选择的 html(无需自定义过滤器功能)。
<select data-ng-model="firstSelect" data-ng-options="option.name for option in optionObjs"></select>
<select data-ng-model="secondSelect" data-ng-options="option.name for option in firstSelect.elements"></select>
就是这样。哦!!!
$scope.optionObjs = [
{ id: 1, parent: null, name: 'option 1', desc: '', },
{ id: 9, parent: 1, name: 'option 11', desc: '', },
{ id: 10, parent: 1, name: 'option 12', desc: '', },
{ id: 11, parent: 1, name: 'option 13', desc: '', },
{ id: 12, parent: 1, name: 'option 14', desc: '', },
{ id: 13, parent: 1, name: 'option 15', desc: '', },
];
继续这个例子,我将不得不为第一个选择编写一个过滤器,以仅显示那些没有父引用的选项。
<select data-ng-model="firstSelect" data-ng-options="option.name for option in optionObjs | filter:parent=null"></select>
<select data-ng-model="secondSelect" data-ng-options="option.elements.name for option in optionObjs | filter:firstSelect"></select>
考虑到@adamjld 的建议并使用 Angular filter 进行更多实验,我提出了以下 html 更新以配合上述数据结构(不需要自定义过滤器功能).
<select data-ng-model="firstSelect" data-ng-options="option.name for option in optionObjs | filter : { parent: null, }"></select>
<select data-ng-model="secondSelect" data-ng-options="option.elements.name for option in optionObjs | filter : { parent: firstSelect.id, }"></select>
虽然这是一个简单得多的解决方案,但在初始加载时存在一个小问题。因为我没有将 firstSelect 初始化为我的 Controller 中的作用域对象,所以第二个选择框允许用户选择他们想要的任何选项。但是,一旦在第一个选择框中选择了一个选项,第二个选择框选项就会被过滤,并且只显示与(父级)firstSelect.id 对应的选项。
为了防止人们开始提示我根本没有使用搜索,这里有一些引用资料:
angular filter
ng-repeat :filter by single field
最佳答案
我认为这会满足您的需求。我编辑了数据以显示它工作得更好一些。我将您的数据分成两个数组,一个父数组和一个子数组。
$scope.parentOptionObjs = [{
id: 1,
name: 'option 1',
desc: ''
}, {
id: 2,
name: 'option 2',
desc: ''
}];
$scope.childOptionObjs = [{
parent: 1,
id: 9,
name: 'option 11',
desc: ''
}, {
parent: 1,
id: 10,
name: 'option 12',
desc: ''
}, {
parent: 1,
id: 11,
name: 'option 13',
desc: ''
}, {
parent: 2,
id: 12,
name: 'option 14',
desc: ''
}, {
parent: 2,
id: 13,
name: 'option 15',
desc: ''
}];
});
子项现在使用以下过滤器按父项的 ID 进行过滤。
app.filter('secondDropdown', function () {
return function (secondSelect, firstSelect) {
var filtered = [];
if (firstSelect === null) {
return filtered;
}
angular.forEach(secondSelect, function (s2) {
if (s2.parent == firstSelect) {
filtered.push(s2);
}
});
return filtered;
};
});
关于javascript - 显示基于 Angular 中另一个选择的选项列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30638183/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb
是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?