Click here to Skip to main content
15,881,866 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need load an editable table from jquery ajax call. Let user edit/update it, and then bulk insert/update to database. In this table, there is a column called Type. It's a dropdown list. Loaded by original value, but can let user change it. How should I add the dropdown from ajax call.

success: function (returndata)
{
    if (returndata.ok) {
        var tbl = "<table id='tbl' border='1' class='table table-hover'><thead><tr style='text-align:center;'>"
        tbl += "<th>Name</th><th>Hours</th><th>Comment</th><th>Type</th></thead></table>"
        $("#Table").append(tbl);
        $.each(returndata.data, function (index, itemData) {
            var tbody = "<tr style='text-align:center;'><td><input id='txtName" +itemData.Id+"' style='width:70px;' type='text' value='"+itemData.Name+"'></td>"
            tbody +="<td><input id='txtHours"+itemData.Id+"' style='width:40px;' type='text' value='"+itemData.Hours+"'></td>"
            tbody +="<td><input id='txtComent"+itemData.Id+"' type='text' value='"+itemData.Comment+"'></td>"
            tbody +="<td><select id='ddlType"+itemData.Id+"'><option value=''>Select Type</option></td>"
        });
    }
    else {
        window.alert(' error : ' + returndata.message);
    }
}
Posted
Updated 15-Jul-15 11:38am
v3

If you have only one item for the dropdown, then why to render it as a dropdown? Rather just show a textbox. It would be good.
 
Share this answer
 
I created another global variable s and load s with all options. When load the table, I load all options first and then replace the selecteditem add "selected="selected" . Now it works. This may not be best practice. Please give me your suggestion.

JavaScript
var TypeOptions = @Html.Raw(Json.Encode(ViewData["Type"]));
var s='<option value="">Select Type</option>';

for (var e=0; e < TypeOptions.length; e++)
{
    var lt =  TypeOptions[e];
    s += '<option value="'+lt["TypeId"]+'">'+lt["Type"]+' </option>';
}

success: function (returndata)
{
    if (returndata.ok) {
        var tbl = "<table id="tbl" border="1" class="table table-hover"><thead><table><tbody><tr style="text-align:center;">"
        tbl += "<th>Name</th><th>Hours</th><th>Comment</th><th>Type</th></tr></tbody></table></thead>"
        $("#Table").append(tbl);
        $.each(returndata.data, function (index, itemData) {
            var tbody = "<tr style="text-align:center;"><td><input id='txtName"+itemData.Id+"' style='width:70px;' type='text' value='"+itemData.Name+"'></td>"
            tbody +="<td><input id='txtHours"+itemData.Id+"' style='width:40px;' type='text' value='"+itemData.Hours+"'></td>"
            tbody +="<td><input id='txtComent"+itemData.Id+"' type='text' value='"+itemData.Comment+"'></td>"
            tbody +="<td><select id='ddlType"+itemData.Id+"'>"+ s +"</select></td></tr>"
            tbody = tbody.replace('<option value="'+itemData.TypeId+'">', '<option selected="selected" value="'+itemData.TypeId+'">');  
        });
        tbl += tbody+"</table>"
    }
    else {
        window.alert(' error : ' + returndata.message);
    }
}
 
Share this answer
 
I have more than one item for the drop. They are coming from database table. And I don't want to hardcode the html to it. Can I make another call in side of "success: function "?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900