Commit 313dce37 by dongshufeng

refactor(iesplan): radio is not ok

parent 33cb78ef
5,car1,, 5,car1,,
1000001,源电压,Checkbox, 1000001,源电压,Checkbox,
1000002,A相有功634,Radio, 1000002,A相有功634,Radio,name1:1.0;name2:3;name3:4
1000003,A相无功634,TextField, 1000003,A相无功634,TextField,
1000004,B相有功634,Switch, 1000004,B相有功634,Switch,
1000005,B相无功634,Slider,-100;100;10 1000005,B相无功634,Slider,-100;100;10
......
...@@ -46,9 +46,9 @@ impl QueryWithId { ...@@ -46,9 +46,9 @@ impl QueryWithId {
pub enum ParaType { pub enum ParaType {
// show expression, true expression, false expression // show expression, true expression, false expression
Checkbox, Checkbox,
Radio,
Switch, Switch,
Select(Vec<f64>), Radio(Vec<(String, f64)>),
Select(Vec<(String, f64)>),
// min, max, step // min, max, step
Slider(f64, f64, f64), Slider(f64, f64, f64),
TextField, TextField,
...@@ -81,7 +81,6 @@ pub fn create_parameters(content: &[u8]) -> Parameters { ...@@ -81,7 +81,6 @@ pub fn create_parameters(content: &[u8]) -> Parameters {
let para_type_s = csv_str(&row, 2).unwrap().to_uppercase(); let para_type_s = csv_str(&row, 2).unwrap().to_uppercase();
let para_type = match para_type_s.as_str() { let para_type = match para_type_s.as_str() {
"CHECKBOX" => ParaType::Checkbox, "CHECKBOX" => ParaType::Checkbox,
"RADIO" => ParaType::Radio,
"SWITCH" => ParaType::Switch, "SWITCH" => ParaType::Switch,
"TEXTFIELD" => ParaType::TextField, "TEXTFIELD" => ParaType::TextField,
"SLIDER" => { "SLIDER" => {
...@@ -94,9 +93,13 @@ pub fn create_parameters(content: &[u8]) -> Parameters { ...@@ -94,9 +93,13 @@ pub fn create_parameters(content: &[u8]) -> Parameters {
}, },
"SELECT" => { "SELECT" => {
let v = csv_str(&row, 3).unwrap(); let v = csv_str(&row, 3).unwrap();
let floats = v.split(";") let options = parse_options(v);
.map(|s| s.parse::<f64>().unwrap()).collect(); ParaType::Select(options)
ParaType::Select(floats) }
"RADIO" => {
let v = csv_str(&row, 3).unwrap();
let options = parse_options(v);
ParaType::Radio(options)
} }
_ => ParaType::TextField _ => ParaType::TextField
}; };
...@@ -105,6 +108,19 @@ pub fn create_parameters(content: &[u8]) -> Parameters { ...@@ -105,6 +108,19 @@ pub fn create_parameters(content: &[u8]) -> Parameters {
Parameters { id, name, labels, points, para_types } Parameters { id, name, labels, points, para_types }
} }
fn parse_options(v: &str) -> Vec<(String, f64)> {
let options = v.split(";")
.map(|s| {
let options: Vec<&str> = s.split(":").collect();
if options.len() == 2 {
(options[0].to_string(), options[1].parse::<f64>().unwrap())
} else {
("".to_string(), s.parse::<f64>().unwrap())
}
}).collect();
options
}
pub fn build_tiles(xlsx_bytes: Vec<u8>) -> Option<Tiles> { pub fn build_tiles(xlsx_bytes: Vec<u8>) -> Option<Tiles> {
let (m, n, merge_map, values) = get_first_sheet_merged_cells(xlsx_bytes)?; let (m, n, merge_map, values) = get_first_sheet_merged_cells(xlsx_bytes)?;
let mut class_str = Vec::new(); let mut class_str = Vec::new();
......
...@@ -40,8 +40,7 @@ impl Component for ParaCard { ...@@ -40,8 +40,7 @@ impl Component for ParaCard {
for index in 0..ctx.props().paras.points.len() { for index in 0..ctx.props().paras.points.len() {
let input_type = &ctx.props().paras.para_types[index]; let input_type = &ctx.props().paras.para_types[index];
if ParaType::Checkbox.eq(input_type) if ParaType::Checkbox.eq(input_type)
|| ParaType::Switch.eq(input_type) || ParaType::Switch.eq(input_type) {
|| ParaType::Radio.eq(input_type) {
bools.insert(index, false); bools.insert(index, false);
} else { } else {
floats.insert(index, 0.0); floats.insert(index, 0.0);
...@@ -85,6 +84,7 @@ impl Component for ParaCard { ...@@ -85,6 +84,7 @@ impl Component for ParaCard {
self.do_set_point(ctx, &value, point_id); self.do_set_point(ctx, &value, point_id);
} }
Msg::SetOption(i, value) => { Msg::SetOption(i, value) => {
log::warn!("========================= {i} {value}");
if value == "None" { if value == "None" {
return false; return false;
} }
...@@ -141,17 +141,6 @@ impl ParaCard { ...@@ -141,17 +141,6 @@ impl ParaCard {
</Field> </Field>
} }
} }
ParaType::Radio => {
let checked = self.bools.get(&i).cloned().unwrap_or(false);
html! {
<Field horizontal={true} label={label}>
<Radio update={link.callback(move |_| Msg::SetBool(i, !checked))}
checked_value={"selected"}
value={if checked {"selected"} else {"none"}}>
</Radio>
</Field>
}
}
ParaType::Switch => { ParaType::Switch => {
let checked = self.bools.get(&i).cloned().unwrap_or(false); let checked = self.bools.get(&i).cloned().unwrap_or(false);
html! { html! {
...@@ -179,22 +168,56 @@ impl ParaCard { ...@@ -179,22 +168,56 @@ impl ParaCard {
} }
} }
ParaType::Select(options) => { ParaType::Select(options) => {
let current_v = self.floats.get(&i).cloned().unwrap_or(0.0).to_string(); let current_f = self.floats.get(&i).cloned().unwrap_or(0.0);
html! { let content = (0..options.len()).map(|i| {
<Field horizontal={true} label={label}> let (name, f) = &options[i];
<Select update={link.callback(move |s| Msg::SetOption(i, s))} > let to_show = if name.is_empty() {
{for options.iter().map(|f| { f.to_string()
} else {
name.clone()
};
html! { html! {
<option value={f.to_string()} selected={current_v == f.to_string()}> <option value={f.to_string()} selected={current_f == *f}>
{f.to_string()} {to_show}
</option> </option>
} }
})} }).collect::<Html>();
html! {
<Field horizontal={true} label={label}>
<Select update={link.callback(move |s| Msg::SetOption(i, s))} >
{content}
<option value={"None"}>{"no_selection"}</option> <option value={"None"}>{"no_selection"}</option>
</Select> </Select>
</Field> </Field>
} }
} }
ParaType::Radio(options) => {
let current_f = self.floats.get(&i).cloned().unwrap_or(0.0);
let content = (0..options.len()).map(|i| {
let (name, f) = &options[i];
let to_show = if name.is_empty() {
f.to_string()
} else {
name.clone()
};
let checked_value = if current_f == *f {
f.to_string()
} else {
current_f.to_string()
};
html! {
<Radio update={link.callback(move |s| Msg::SetOption(i, s))}
checked_value={checked_value} value={f.to_string()}>
<span>{to_show}</span>
</Radio>
}
}).collect::<Html>();
html! {
<Field horizontal={true} label={label}>
<div class="radios">{content}</div>
</Field>
}
}
ParaType::TextField => { ParaType::TextField => {
let name = format!("tf_{}", point_id); let name = format!("tf_{}", point_id);
let f = self.floats.get(&i).cloned().unwrap_or(0.0).to_string(); let f = self.floats.get(&i).cloned().unwrap_or(0.0).to_string();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论