javascript 如何从动态创建的文本中获取值

wqsoz72f  于 2023-06-20  发布在  Java
关注(0)|答案(1)|浏览(140)

我使用xml样式获取json obj,如下所示:

"template": {
"xmlui": {
  "vbox": {
    "hbox": {
      "label": {
        "@value": "",
        "@attributes": {
          "value": "SOME VALUE"
        }
      },
      "textField": {
        "@value": "",
        "@attributes": {
          "width": "325",
          "name": "PLAT",
          "value": "IP",
          "maxLength": "80",
          "enabled": "true",
          "interactive": "false",
          "required": "true"
        }
      }
    },
    "tbox": {
      "vbox": [
        {
          "label": {
            "@value": "",
            "@attributes": {
              "value": "SOME MORE VALUE"
            }
          },
          "dropDownList": {
            "item": [
              {
                "@value": "",
                "@attributes": {
                  "value": "NO",
                  "text": "NO"
                }
              },
              {
                "@value": "",
                "@attributes": {
                  "value": "YES",
                  "text": "YES"
                }
              }
            ],
            "@attributes": {
              "width": "200",
              "name": "ISP_PROIZV",
              "value": "NO",
              "maxLength": "8",
              "enabled": "true",
              "interactive": "false",
              "required": "true"
            }
          }
        },
...

我试着在屏幕上查看这些项目

handleMyData(keyIn, data){
//console.log(keyIn, typeof data, data.hasOwnProperty('@attributes'), tableItems.length);

let fields = ['hbox', 'tbox', 'vbox'];
if (typeof data == 'object' && data.hasOwnProperty('@attributes') && fields.indexOf(keyIn) == -1) {
  if (keyIn == 'label') {
    tableItems.push(
      <Text>{data['@attributes'].value}</Text>
    );
  } else if (keyIn == 'textField') {
    //this.setState({text: data['@attributes'].value});
    tableItems.push(
      <TextInput value={this.state.text}
                 placeholder={data['@attributes'].tip}
                 disabled={data['@attributes'].enabled == 'false'}
                 onChangeText={(text) => this.setState({text})}
                 ref= {(el) => { this.text = el; }}
      />
    );
  } else if (keyIn == 'date') {
    if (Platform.OS == 'ios') {
      tableItems.push(
        <DatePickerIOS
          date={moment(this.state.date)}
          onDateChange={this.handleOnDateChange}
        />
      );
    } else {
      tableItems.push(
        <DatePicker
          onComplete={this.handleOnDateChange}
          enabled={data['@attributes'].enabled == 'true'}
          value={data['@attributes'].value}
        />
      );
    }
 } else if (keyIn == 'dropDownList') {
    this.pickerRender(data, data['@attributes'].value);
}
  for (let key in data) {
  let value = data[key];
  //console.log(key, value instanceof Array);
  if (value instanceof Array) {
    for (let i = 0; i < value.length; i++) {
      this.handleMyData(key, value[i]);
    }
  } else if (typeof value === 'object' && value !== null) {
    this.handleMyData(key, value);
  }
}
},

pickerRender(data){
let pickerItems = [];
if (data.item instanceof Array) {
  for (let i = 0; i < data.item.length; i++) {
    let next = data.item[i];
    pickerItems.push(
      <Picker.Item
        key={'key' + i}
        label={next['@attributes'].text}
        value={next['@attributes'].value}
      />);
  }
} else {
  pickerItems = this.pickerPush(data);
}
tableItems.push(
  <Picker
    onValueChange={(value) => this.handleOnPickerChange(value)}
    selectedValue={this.state.pickerSelect}
    mode="dialog"
  >{pickerItems}
  </Picker>);
},

我如何从这个元素中获取值并放入1个对象?我试图添加参考键,但可能是我犯了一个错误。

kxeu7u2r

kxeu7u2r1#

pushInOpt(name, text){
let obj = {
  name: name,
  value: text
};
let flag=false;
if (options.length == 0){
  options.push(obj);
}else {
  for (let i = 0; i < options.length; i++) {
    if (options[i].name == name) {
      options[i].value = text;
      flag=true;
    }
  }
  if (flag==false){
    options.push(obj);
  }
}   
},

我自己解决的,只是在onEndEditing、onValueChange等参数中增加了推送功能

相关问题