javascript - JSON data into html table -
i have json response
{ "result_ok": "true", "info_message": "2015-04-24 - recharge - 2.8 \n 2015-04-23 - transfer - 15.0 \n 2015-04-21 - recharge - 3.5 \n 2015-04-15 - recharge - 27.7 \n 2015-04-14 - recharge - 5.0 \n", "client_currency":"eur", "balance_account":109.5 }
and need put "info_message"
data html table below. don't know how go through data of field?
<p class="text-right big"><span class="accountcredit">+ 109.5 eur</span></p> <table class="table cblue5"> <tbody> <tr> <td>2015-04-24</td> <td>recharge</td> <td>2.8</td> </tr> <tr> <td>2015-04-23</td> <td>transfer</td> <td>15.0</td> </tr> ...
write code wish had.
<script> function create_html(json_obj) { return get_header(json_obj) + get_table(json_obj); } function get_header (json_obj) { return '<p class="text-right big"><span class="accountcredit">+ ' + json_obj.balance_account.tostring() + ' ' + json_obj.client_currency + ' ' + '</span></p>' } function get_table (json_obj) { return ' <table class="table cblue5"> <tbody> ' + get_rows(json_obj.info_message) + '</tbody></table>' } function get_rows (info_message) { return info_message.split(/\s*\n\s*/).map(get_single_row).join('\n'); } function get_single_row (message) { var elements = message.split(/\s+\-\s+/) if(elements.length !== 3) return; return '<tr><td>' + elements[0] +'</td><td>' + elements[1] +'</td><td>' + elements[2] +'</td></tr>' } </script> <body > <script> document.write(create_html( {"result_ok":"true", "info_message":"2015-04-24 - recharge - 2.8 \n 2015-04-23 - transfer - 15.0 \n 2015-04-21 - recharge - 3.5 \n 2015-04-15 - recharge - 27.7 \n 2015-04-14 - recharge - 5.0 \n","client_currency":"eur","balance_account":109.5})) </script> </body>
should job.
hth georg
Comments
Post a Comment