如何在文本区域内渲染漂亮的json数据React js?

时间:2016-06-28 12:27:12

标签: javascript jquery json reactjs

我是新来回应js.I在textarea中渲染漂亮的json数据时遇到问题。我不知道哪个部分出错了 我希望我的prettyjson在textarea里面渲染像这样

"email":"xxxx@x.com",
"email":"yyyy@y.com",
.....

这是我的代码 但是我在textarea里面没有任何东西

/**
 * Created by arfo on 6/26/2016.
 */
var React =require('react');
var api = require('../utils');


var Bulkmail = React.createClass({
    getInitialState:function () {
      return{
          default:10,
          data:[],
          color:'#58FA58'

      }
    },
    componentDidMount:function () {
        api.getemail(this.state.default).then(function (response) {
           this.setState({
               data:response

           })
        }.bind(this))
    },
    onSubmit:function (e) {
      e.preventDefault();
      console.log(this.refs.text.value.trim());


    },

    onChange:function (e) {
     e.preventDefault();
        //console.log(this.refs.text.value.trim())
        var data = this.refs.text.value.trim();
        if(isNaN(data)){
            this.setState({
                color:'#FE2E2E'
            })
        }else{
            this.setState({
                color:'#58FA58'
            })
        }
    },
    render:function () {
        console.log(this.state.data);
        var results = this.state.data;
        return(
           <div className="bodybox">
               <div className="box">
                  <div className="upc">
                      <p>Generate Bulk Email</p>
                      <form onSubmit={this.onSubmit}>
                      <input onChange={this.onChange} type="text" style={{border:'1px solid '+this.state.color}}  ref="text" defaultValue={this.state.default} placeholder="Enter Number"/>
                       <button>Get Data</button>
                      </form>
                      <div className="result">
                          <ul>
                              {this.state.data.map(function (data) {
                                  return  <li key={data.id}>{data.email}</li>
                              })}

                          </ul>
                      </div>

                  </div>
                   <div className="tdown">

                       <p>Json Format</p>

                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                       <textarea defaultValue={this.state.data.map(function(data) {
                           return JSON.stringify(data.email)
                       })}  >
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                       </textarea>
                   </div>
               </div>
           </div>
        )
    }
});

module.exports = Bulkmail ;

3 个答案:

答案 0 :(得分:1)

 <textarea value={this.state.data.map(e=>JSON.stringify(e))}  defaultValue="val" />

结果 {"email":"some@mail"},{"email":"some@mail"},{"email":"some@mail"}

let value = this.state.data.map(e=>JSON.stringify(e).replace(/{|}/g,''));

<textarea value={value}  defaultValue="val" />

结果 "email" : "xx@yy.y", "email" : "some@mail", "email" : "some@mail"

 let value = this.state.data.map(e=>JSON.stringify(e).replace(/{|}/g,'')).join(',\n');

 <textarea value={value}  defaultValue="val" />

结果 "email" : "xx@yy.y", "email" : "some@mail", "email" : "some@mail"

在HTML中,值是通过子设置的。在React中,您应该使用值。

答案 1 :(得分:0)

/**
 * Created by arfo on 6/26/2016.
 */
var React =require('react');
var api = require('../utils');


var Bulkmail = React.createClass({
    getInitialState:function () {
      return{
          default:10,
          data:[],
          color:'#58FA58'

      }
    },
    componentDidMount:function () {
        api.getemail(this.state.default).then(function (response) {
           this.setState({
               data:response

           })
        }.bind(this))
    },
    onSubmit:function (e) {
      e.preventDefault();
      console.log(this.refs.text.value.trim());


    },

    onChange:function (e) {
     e.preventDefault();
        //console.log(this.refs.text.value.trim())
        var data = this.refs.text.value.trim();
        if(isNaN(data)){
            this.setState({
                color:'#FE2E2E'
            })
        }else{
            this.setState({
                color:'#58FA58'
            })
        }
    },

    getEmailValue:function(){
    return this.state.data.map(function(data) {
                           return JSON.stringify(data.email)
                       }).join('\n');
},
    render:function () {
        console.log(this.state.data);
        var results = this.state.data;
        return(
           <div className="bodybox">
               <div className="box">
                  <div className="upc">
                      <p>Generate Bulk Email</p>
                      <form onSubmit={this.onSubmit}>
                      <input onChange={this.onChange} type="text" style={{border:'1px solid '+this.state.color}}  ref="text" defaultValue={this.state.default} placeholder="Enter Number"/>
                       <button>Get Data</button>
                      </form>
                      <div className="result">
                          <ul>
                              {this.state.data.map(function (data) {
                                  return  <li key={data.id}>{data.email}</li>
                              })}

                          </ul>
                      </div>

                  </div>
                   <div className="tdown">

                       <p>Json Format</p>


                       <textarea value={getEmailValue()}

                       </textarea>
                   </div>
               </div>
           </div>
        )
    }
});

答案 2 :(得分:-1)

无需使用困难的正则表达式,我们可以使用JSON.stringify(object,undefined,2)中的功能从JSON中获得精美呈现的字符串。

var obj={"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}


var pretty = JSON.stringify(obj, undefined, 2);

var ugly = document.getElementById('myTextArea').value; document.getElementById('myTextArea').value = pretty;