asp.net mvc jquery tab -
i looking @ here create dynamic tab in asp.net mvc using jquery.
i have add button in view call jquery function go url via ajax , return partialview uses
html.begincollectionitem()
so far i've done increase counter tabs (ex: employee1, employee2), , can bring partial view , render in tab content, happens information entered in employee1 tab content directly copied , regenerated in employee2 content. (ex: fullname in employee1 content appeared in employee2 content) how can remove previous tab content data , generate new one?
and when clicked on add button, partial view content added vertically horizontally. seemed weird.
so view:
button type="button" class="btn btn-primary" id="add">add</button> <div id="tabs"> <ul> </ul> <div id="content"> <div id="tabs-1"></div> </div> </div> <script> $(document).ready(function () { var tabcounter = 1; $('#add').on('click', function () { $.ajax({ url: '/employee/addnewemployee', }).success(function (partialview) { addtab(partialview); }); }); function addtab(partialview) { var id = "employee" + tabcounter; var title = "employee" + tabcounter; $('#content').append("<div id='" + id + "'><p>" + partialview + "</p></div>"); $('#tabs').tabs("add", id, title); tabcounter++; } }); </script> controller:
public actionresult addnewemployee() { return partialview("_employeepartial"); } employeepartial:
@model test.models.employee <div class="editorrow"> @using (html.begincollectionitem("employees")) { @html.editorfor(m => m.fullname, new { htmlattributes = new { @class = "form-control" } }) </div> }
i solved it. needed @ jquery api documentation more closely.
here code:
<div id="tabs"> <ul> </ul> </div> <script> $(function () { var tabtemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>remove tab</span></li>", tabcounter = 2; var tabs = $("#tabs").tabs(); $('#add').on('click', function (event) { $.ajax({ url: '/application/addnewemployee', }).success(function (partialview) { addtab(partialview); }); event.preventdefault(); }); // actual addtab function: adds new tab using input form above function addtab(partialview) { var label = "tab " + tabcounter, id = "tabs-" + tabcounter, li = $(tabtemplate.replace(/#\{href\}/g, "#" + id).replace(/#\{label\}/g, label)); tabs.find(".ui-tabs-nav").append(li); tabs.append("<div id='" + id + "'><p>" + partialview + "</p></div>"); tabs.tabs("refresh"); tabcounter++; } // close icon: removing tab on click tabs.on("click", "span.ui-icon-close", function () { var panelid = $(this).closest("li").remove().attr("aria-controls"); $("#" + panelid).remove(); tabs.tabs("refresh"); }); tabs.on("keyup", function (event) { if (event.altkey && event.keycode === $.ui.keycode.backspace) { var panelid = tabs.find(".ui-tabs-active").remove().attr("aria-controls"); $("#" + panelid).remove(); tabs.tabs("refresh"); } }); });
Comments
Post a Comment