This is what i render
<select>
<option>1</option>
<option>2</option>
</select>
On selecting any of the options from the dropdown.I must render another dropdown list next to it.
<select>
<option>1</option>
<option>2</option>
</select>
<select>
<option>1.1</option>
<option>1.2</option>
</select>
then on selecting options from the second dropdown list.I must render input field of type text next to it.
我如何在 React 中实现它?
var React = require('react');
var ReactDOM = require('react-dom');
var View = React.createClass({
getInitialState: function() {
return {
value: '------'
}
},
handleChange: function(event){
this.setState({value: event.target.value});
this.getFields(event.target.value);
},
handleClick : function(){
},
render : function(){
return (<div>
<p>
<i className="plusSymbol fa fa-minus-circle"></i>
<select onChange={this.handleChange} value={this.state.value} className="js-example-basic-single">
<option value="">------</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</p>
<p>
<i onClick={this.handleClick} className="plusSymbol fa fa-plus-circle"></i>
<span>Add a new condition</span>
</p>
</div>);
}});
module.exports = View;
So my view should look like this [Dropdown][Dropdown][Text Field]
最佳答案
以下示例应该为您指明正确的方向...
var Hello = React.createClass({
getDefaultProps: function () {
return {
fieldMap: {
"1": [
{ value: "1.1", label: "1.1" },
{ value: "1.2", label: "1.2" }
],
"2": [
{ value: "2.1", label: "2.1" },
{ value: "2.2", label: "2.2" }
]
}
}
},
getInitialState: function() {
return {
firstValue: '',
secondValue: '',
thirdValue: ''
}
},
getOptions: function (key) {
if (!this.props.fieldMap[key]) {
return null;
}
return this.props.fieldMap[key].map(function (el, i) {
return <option key={i} value={el.value}>{el.label}</option>
});
},
handleFirstLevelChange: function (event) {
this.setState({
firstValue: event.target.value,
secondValue: '',
thirdValue: ''
});
},
handleSecondLevelChange: function (event) {
this.setState({
secondValue: event.target.value,
thirdValue: ''
});
},
handleThirdLevelChange: function (event) {
this.setState({
thirdValue: event.target.value
});
},
getSecondLevelField: function () {
if (!this.state.firstValue) {
return null;
}
return (
<select onChange={this.handleSecondLevelChange} value={this.state.secondValue}>
<option value="">---</option>
{this.getOptions(this.state.firstValue)}
</select>
)
},
getThirdLevelField: function () {
if (!this.state.secondValue) {
return null;
}
return (
<input type="text" onChange={this.handleThirdLevelChange} value={this.state.thirdValue} />
)
},
render: function() {
return (
<div>
<select onChange={this.handleFirstLevelChange} value={this.state.firstValue}>
<option value="">---</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
{this.getSecondLevelField()}
{this.getThirdLevelField()}
</div>
);
}
});
关于javascript - 在 Reactjs 中创建依赖字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35031718/
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案
我正在尝试找出如何为我的Ruby项目创建一种“无类DSL”,类似于在Cucumber步骤定义文件中定义步骤定义或在Sinatra应用程序中定义路由。例如,我想要一个文件,其中调用了我的所有DSL函数:#sample.rbwhen_string_matches/hello(.+)/do|name|call_another_method(name)end我认为用我的项目特有的一堆方法污染全局(内核)命名空间是一种不好的做法。因此方法when_string_matches和call_another_method将在我的库中定义,并且sample.rb文件将以某种方式在我的DSL方法的上下文中
有这些railscast。http://railscasts.com/episodes/218-making-generators-in-rails-3有了这个,你就会知道如何创建样式表和脚手架生成器。http://railscasts.com/episodes/216-generators-in-rails-3通过这个,您可以了解如何添加一些文件来修改脚手架View。我想把两者结合起来。我想创建一个生成器,它也可以创建脚手架View。有点像RyanBates漂亮的生成器或web_app_themegem(https://github.com/pilu/web-app-theme)。我
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Rubysyntaxquestion:Rational(a,b)andRational.new!(a,b)我正在阅读ruby镐书,我对创建有理数的语法感到困惑。Rational(3,4)*Rational(1,2)产生=>3/8为什么Rational不需要new方法(我还注意到例如我可以在没有new方法的情况下创建字符串)?
假设我有一个在Ruby中看起来像这样的哈希:{:ie0=>"Hi",:ex0=>"Hey",:eg0=>"Howdy",:ie1=>"Hello",:ex1=>"Greetings",:eg1=>"Goodday"}有什么好的方法可以将它变成如下内容:{"0"=>{"ie"=>"Hi","ex"=>"Hey","eg"=>"Howdy"},"1"=>{"ie"=>"Hello","ex"=>"Greetings","eg"=>"Goodday"}} 最佳答案 您要求一个好的方法来做到这一点,所以答案是:一种您或同事可以在六个月后理解