2017-05-11 14 views
-1

JSON入力のJavaScriptを使用してHTMLテーブルを作成しようとしていますが、それはうまく機能しません。JavaScriptとJSONを使用したHTMLテーブルの作成

私はHTMLのマーカーを使用しています。これはJavascriptでinnerHTML呼び出しから移入れます:

for (var i = 0; i < json.data.length; i++) { 
    listItem = json.data[i].number + "--" + "(Widget " + json.data[i].widget_id + ") x " + " " + json.data[i].pence_price + " GBP" + json.data[i].number * json.data[i].pence_price + " GBP"; 
    table.push(listItem); 
} 
document.getElementById('updateOrder').innerHTML = table; 

これは私に次のような出力が得られます。

11--Widget 8 x 10GBP 110GBP, 10--Widget 9 x 10GBP 100GBP 

私が欲しいのは以下の通りです:

11--Widget 8 x 10GBP     110GBP 
10--Widget 9 x 10GBP     100GBP 

数、ウィジェットとコストは左揃えになり、合計は右揃えになります。私はまた、別々の注文を別々の行にしたい。

JSONのデータは次のとおりです。

{"id":"9518","order_id":"11380","widget_id":"9","number":"10","pence_price":"12"} 

私はこれで私の髪を引っ張ってきました。どんな助けや指導もいただければ幸いです。

+0

私にはテーブルがありませんe、どちらもフレックスのようなものは見えませんが、宣言がない配列の中に何かを押し込んでいるのが見えます。だから、あなたは連結された文字列のセットを持っています...だから、あなたのテーブルをビルドするにはどうすればいいですか? – Icepickle

+0

あなたは正しいです、私はテーブルにこれを入れたいですが、私はチェリーの特定のJSONコンポーネントを選びたいだけです。ご存じのように、私はJSには特に熟練していないし、一緒に縛られたものをテーブルにすることができれば感謝しています。 PS。これはアンドロイド携帯電話用のコードバで書かれています。 –

+0

行を除いた表形式で、divsを使用するだけであればdivsでyesを使用します。 –

答えて

0

var json = {data:{a: {"id":"9518","order_id":"11380","widget_id":"9","number":"10","pence_price":"12"},b:{"id":"9518","order_id":"11380","widget_id":"9","number":"10","pence_price":"12"},c: {"id":"9518","order_id":"11380","widget_id":"9","number":"10","pence_price":"12"}}} 
 
var i, 
 
    item, 
 
    listItem = ""; 
 
for (i in json.data){ 
 
    item = json.data[i]; 
 
    listItem += "<div class='table'><div>"+item.number+"--"+"(Widget "+item.widget_id+") x "+" "+item.pence_price+" GBP</div><div class='right'>"+item.number*item.pence_price+" GBP</div></div>"; 
 
} 
 
document.getElementById('updateOrder').innerHTML=listItem;
#updateOrder{ 
 
    padding: 2px; 
 
    background: orange; 
 
} 
 
.table{ 
 
    width: 100%; 
 
    background: green; 
 
    display: table; 
 
} 
 
.table > div{ 
 
    display: table-cell; 
 
} 
 
.right{ 
 
    background: tomato; 
 
}
<div id="updateOrder"></div>
を区別するために追加されました

+0

ありがとう、これは治療になりました。 –

0

テーブルを作成したり、スタイリングを追加したり、計算を行う可能性のある少し大きなテンプレートを作成しました。

できるだけ多くのコメントを追加して、コードの内容を説明しました。

あなたはHTMLテーブルを望んで述べたので、それは基本的なtable要素を使用して作成されており、いくつかの基本的なスタイルは、ヘッダ行とフッタ

// the data used in this demo 
 
var data = [{ 
 
    "id": "9518", 
 
    "order_id": "11380", 
 
    "widget_id": "9", 
 
    "number": "10", 
 
    "pence_price": "12" 
 
    }, 
 
    { 
 
    "id": "9518", 
 
    "order_id": "11380", 
 
    "widget_id": "9", 
 
    "number": "10", 
 
    "pence_price": "12" 
 
    } 
 
]; 
 

 
function createTable(target, data, columns) { 
 
    // gets the elements required based on id for the target div 
 
    // and creates the table, thead, tbody & tfoot for the table 
 
    let element = document.getElementById(target), 
 
    table = document.createElement('table'), 
 
    thead = document.createElement('thead'), 
 
    header = document.createElement('tr'), 
 
    tbody = document.createElement('tbody'), 
 
    tfoot = document.createElement('tfoot'); 
 

 
    // creates the header 
 
    columns.forEach(column => { 
 
    // and creates the cells in the header, adding title and class 
 
    let cell = document.createElement('td'); 
 
    cell.innerHTML = column.title; 
 
    cell.className = column.class; 
 
    header.appendChild(cell); 
 
    }); 
 
    thead.appendChild(header); 
 

 
    // totals is used for the totals for the footer 
 
    var totals = {}; 
 
    for (let i = 0; i < data.length; i++) { 
 
    // creates the single rows 
 
    let row = document.createElement('tr'); 
 
    columns.forEach(column => { 
 
     // and for each column creates the cell itself 
 
     let cell = document.createElement('td'); 
 
     let value; 
 
     // checks what to display 
 
     if (column.field) { 
 
     // only a property on the data 
 
     value = data[i][column.field]; 
 
     } else if (column.value) { 
 
     // a function with a callback value 
 
     value = column.value(data[i]) 
 
     } 
 
     // if it should calculate totals, it will do so here 
 
     if (column.calculateTotal) { 
 
     // in case the column is unknown, it's initialized as 0 
 
     // warning: all values will be whole numbers 
 
     totals[column.field] = (totals[column.field] || 0) + parseInt(value); 
 
     } 
 
     // if it has a template, we will replace the %0 with value 
 
     // this template function supports only 1 value to be "templated" 
 
     if (column.template) { 
 
     value = column.template.split('%0').join(value); 
 
     } 
 
     // set the cell value 
 
     cell.innerHTML = value; 
 
     // set the class (used to align, for example) 
 
     cell.className = column.class; 
 
     // add cell to row 
 
     row.appendChild(cell); 
 
    }); 
 
    // add row to tbody 
 
    tbody.appendChild(row); 
 
    } 
 
    // empty object would mean false, so only if totals needed to be calculated 
 
    // would it create the footer here 
 
    if (totals) { 
 
    let row = document.createElement('tr'); 
 
    columns.forEach(column => { 
 
     let cell = document.createElement('td'), value = ''; 
 
     if (column.calculateTotal) { 
 
     value = totals[column.field]; 
 
     if (column.template) { 
 
      // can still use the row template 
 
      value = column.template.split('%0').join(value); 
 
     } 
 
     } 
 
     cell.innerHTML = value; 
 
     cell.className = column.class; 
 
     row.appendChild(cell); 
 
    }); 
 
    tfoot.appendChild(row); 
 
    } 
 
    table.appendChild(thead); 
 
    table.appendChild(tbody); 
 
    table.appendChild(tfoot); 
 
    // set the table on the target element 
 
    // warning, calling create table twice will create 2 tables under eachother 
 
    element.appendChild(table); 
 
} 
 

 
// call the create table, with the: 
 
// - target: id in html -> 'target' 
 
// - data: an array defining your data itself 
 
// - columns: an array of objects describing how the table should look 
 
// - title: header 
 
// - field (optional): which property it should show 
 
// - value (optional): callback function that receives a row and should return a value 
 
// - calculatedValue (optional): bool indicating if columns should be summed 
 
// - template (optional): any text you wish to add to your data? 
 
createTable('target', data, [{ 
 
    title: 'id', 
 
    field: 'id', 
 
    class: 'left' 
 
    }, 
 
    { 
 
    title: 'order', 
 
    field: 'order_id', 
 
    class: 'left' 
 
    }, 
 
    { 
 
    title: 'widget', 
 
    field: 'widget_id', 
 
    class: 'left' 
 
    }, 
 
    { 
 
    title: 'number', 
 
    field: 'number', 
 
    class: 'center' 
 
    }, 
 
    { 
 
    title: 'price', 
 
    field: 'pence_price', 
 
    class: 'right', 
 
    template: '%0 GBP' 
 
    }, 
 
    { 
 
    title: 'total', 
 
    value: (row) => parseInt(row['number']) * parseInt(row['pence_price']), 
 
    class: 'right', 
 
    template: '%0 GBP', 
 
    calculateTotal: true 
 
    } 
 
]);
.left { 
 
    text-align: left; 
 
} 
 
.right { 
 
    text-align: right; 
 
} 
 
thead tr { 
 
    background-color: #777; 
 
} 
 
thead tr td { 
 
    font-weight: bold; 
 
    color: #fff; 
 
} 
 
tfoot tr td { 
 
    font-weight: bold; 
 
} 
 
table td { 
 
    padding: 5px; 
 
    border-bottom: solid #efefef 1px; 
 
}
<div id="target"> 
 
</div>

+0

Icepickleさん、ありがとうございました。これもうまくいきましたが、上記のavrilからの回答は簡単に実装できました。 –

+0

@KevinSullivan Cool :)あなたの答えを見つけだす限り、すべてが良いです:)私はまた、あなたの最初の投稿に基づいて、編集することなく、見た目とHTMLテーブルを使って説明したサンプルコードを作成しました。本当のアヴリルのコードは使いやすいですが – Icepickle

関連する問題