Dynamic creation of table with DOM

前端 未结 9 1062
面向向阳花
面向向阳花 2020-11-28 06:07

Can someone tell me what\'s wrong with this code? I want to create a table with 2 columns and 3 rows, and in the cells I want Text1 and Text2 on every row. This code creates

9条回答
  •  無奈伤痛
    2020-11-28 06:49

    In My example call add function from button click event and then get value from form control's and call function generateTable.
    In generateTable Function check first Table is Generaed or not. If table is undefined then call generateHeader Funtion and Generate Header and then call addToRow function for adding new row in table.

    
    
    
    
    //Call Function From Button click Event var counts = 1; function add(){ var Weightage = $('#Weightage').val(); var ItemName = $('#ItemName option:selected').text(); var ItemNamenum = $('#ItemName').val(); generateTable(Weightage,ItemName,ItemNamenum); $('#Weightage').val(''); $('#ItemName').val(-1); return true; } function generateTable(Weightage,ItemName,ItemNamenum){ var tableHtml = ''; if($("#rDataTable").html() == undefined){ tableHtml = generateHeader(); }else{ tableHtml = $("#rDataTable"); } var temp = $("
    "); var row = addToRow(Weightage,ItemName,ItemNamenum); $(temp).append($(row)); $("#dataGridForItem").html($(tableHtml).append($(temp).html())); } //Generate Header function generateHeader(){ var html = ""; html+=""; html+=""; html+=""; html+=""; html+="
    "+'Sr.No'+""+'Item Name'+""+'Weightage'+"
    "; return html; } //Add New Row function addToRow(Weightage,ItemName,ItemNamenum){ var html=""; html+=""+counts+""; html+=""+ItemName+""; html+=""+Weightage+""; html+=""; counts++; return html; }

提交回复
热议问题