私はJavaScriptにはとても慣れていて、私と一緒に裸でいます。私は、ダミーのJSONデータをdivテーブルに取り込もうとしていましたが、私はそれを行うように見えません。私がしたいことは、ドロップダウンボックスの選択に応じて特定のデータを表示することです。また、アイテムが選択されたときに新しいドロップダウンボックスで新しい行を作成する必要があります。それを行う最良の方法は何かアドバイスをお願いします。私はAngularで必要なものに近いものを作成することができますが、私はそれを純粋なJavaScriptで必要とします。前もって感謝します!divテーブルとドロップダウンボックスにJSONとJavascriptを挿入するにはどうすればよいですか?
-3
A
答えて
0
あなたはJSONオブジェクト
var data = [
{
"line": "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.",
"author": "Brian W. Kernighan",
"num" : ["1","2","3"]
},
{
"line": "Walking on water and developing software from a specification are easy if both are frozen.",
"author": "Edward V Berard",
"num" : ["5","0","15"]
},
{
"line": "It always takes longer than you expect, even when you take into account Hofstadter's Law.",
"author": "Hofstadter's Law",
"num" : ["15","222","301"]
}];
を持っていて、テーブルの行のそれぞれのドロップダウン要素にテーブルとNUMに上記のJSONオブジェクト内のすべての著者を移入したいデータであるとします。次に、populateHTML()関数は、次の図に示すように、オブジェクトをtableに、numをtable-rowのそれぞれのドロップダウン要素に挿入します。
function populateHTML(data) {
if (typeof(data) == 'object') {
document.write('<table>');
for (var i in data) {
document.write('<tr><td>'+data[i].author+'</td><td><select>');
for(var j in data[i].num){
document.write('<option>' +data[i].num[j]+ '</option>');
}
document.write('</select></td></tr>');
}
document.write('</tr></table>');
} else {
document.write(' => ' + data);
}
}
0
これは、次のコードで実現することができます。また、ここでの例を確認することができます。http://skillcram.com/JS_DivTable.htm
<script type="text/javascript" >
function populateTable() {
var tableData = {
products : [
{"id": 100,"name":"Laptop", "qty":1,"status": ["Delivered","Damaged","Missing"]},
{"id": 200,"name":"Monitor", "qty":2,"status":["Refused","Partial"]}
]
}
var tableBody = document.getElementsByClassName("divTableBody");
for (i in tableData.products){
tableBody[0].innerHTML += "<div class=\"divTableRow\"> " +
"<div class=\"divTableRowCell\">"+ tableData.products[i].id +" </div> " +
"<div class=\"divTableRowCell\">"+ tableData.products[i].name +" </div> " +
"<div class=\"divTableRowCell\">"+ tableData.products[i].qty +" </div> "+
"<div class=\"divTableRowCell\">"+ getSelectHTMl(tableData.products[i].status) +
" </div> "+
"</div>";
}
}
function getSelectHTMl(status) {
selectHTMlStr = "<select> ";
for (j in status){
selectHTMlStr +=
"<option value=\""+ status[j]+ "\" id=\"itemStatus\" >"+status[j]+ " </option>" ;
}
selectHTMlStr += "</select>" ;
return selectHTMlStr;
}
</script>
関連する問題
- 1. 一括挿入のようなテーブルに値を挿入するにはどうすればよいですか?
- 2. javascriptを使用してdiv/spanタグをテキストエリアに挿入するにはどうすればよいですか?
- 3. jsonデータを別のmysqlテーブルに挿入するにはどうすればよいですか?
- 4. テーブル内のレコードごとに新しい行を挿入するにはどうすればよいですか?
- 5. JavaScriptを生成したテーブルにヘッダーを挿入するにはどうすればよいですか?
- 6. ブランクデータがMySQLテーブルに挿入されないようにするにはどうすればよいですか?
- 7. テーブルに日付と画像を挿入するにはどうすればよいですか?
- 8. 元の日をHiveのパーティションとしてテーブルに挿入するにはどうすればよいですか?
- 9. 2つのdivの間にdivを挿入するにはどうすればよいですか?
- 10. クロムエクステンションにロードされたiframeにCSSとJavascriptを挿入するにはどうすればいいですか?
- 11. テーブル1からテーブル2にデータを挿入するにはどうすればよいですか?
- 12. あるテーブルの情報をLINKテーブルに挿入するにはどうすればよいですか?
- 13. 複数のテーブルの値を別のテーブルに挿入するにはどうすればよいですか?
- 14. テーブル内の値をルックアップして別のテーブルに挿入するにはどうすればよいですか?
- 15. フォーム入力フィールドをテーブルに挿入するにはどうすればよいですか?
- 16. テーブルから別のテーブルに新しいデータを挿入するにはどうすればいいですか?
- 17. ランダムに生成された数値をテーブルに挿入するにはどうすればよいですか?
- 18. divでダイナミック幅を適用するにはどうすればいいですか?CSSとJavaScriptによるホバー:
- 19. JSFでドロップダウンボックスを作成するにはどうすればよいですか?
- 20. MySQLでは、値が同じテーブルに存在しないという条件でテーブルに挿入するにはどうすればよいですか?
- 21. IE8にスタイルを挿入するにはどうすればよいですか?
- 22. アクティビティをフラグメントに挿入するにはどうすればよいですか?
- 23. コールバックをインテントに挿入するにはどうすればよいですか?
- 24. PDFをXcodeに挿入するにはどうすればよいですか?
- 25. JPanelをJLabelに挿入するにはどうすればよいですか?
- 26. ノードをGPathResultに挿入するにはどうすればよいですか?
- 27. ドロップダウンリストにエラーメッセージを挿入するにはどうすればよいですか?
- 28. ラベルテキストにハイパーリンクを挿入するにはどうすればよいですか?
- 29. インメモリユーザプロバイダをサービスに挿入するにはどうすればよいですか?
- 30. ViewContainerRefをサービスに挿入するにはどうすればよいですか?
を[尋ねる] [MCVE]が含まれ、読むためにあなたの質問を編集してください。 – evolutionxbox
ようこそ!あなたのコードを共有できますか?それは、あなたがどんなふるまいをしたいのかを理解するのに役立ちます。 – Rajesh
@Rajesh私はdivテーブルの構造を含めました。含まれているIDは私の計画どおりに動作しなかったデータを表示しようとしたものです。助けてくれてありがとう ! – danielradst