This article mainly shares with you jQuery to implement a GridView-like editing, updating, canceling and deleting function. In the project, we encountered such a requirement. When the user clicks edit, a row is dynamically generated under the clicked row, and the edit button changes. If it is disabled, the newly generated row has update and cancel buttons. Click the "Cancel" button to delete the dynamically generated row and restore the edit button status. The editor will share the example code with you below, let’s take a look.
Let’s first take a look at the following real-time effect demonstration:
When the user clicks edit, a row is dynamically generated under the clicked row. The edit button becomes disabled.
The newly generated row has update and cancel buttons. Click the "Cancel" button to delete the dynamically generated row. Edit button status restored.
There is nothing special about the update and delete button functions.
The ASP.NET MVC view html code is as follows, ordinary table table, ordinary html tag:
Deleted button function:
$('.Delete').click(function () { var flag = confirm('你确认是否删除记录?'); if (flag) { var tr = $(this).closest('tr'); var obj = {}; obj.Ltc_nbr = tr.find('.SelectSingle').val(); $.ajax({ type: 'POST', url: "/Highway/LandTransportationCityDelete", dataType: 'json', data: JSON.stringify(obj), contentType: 'application/json; charset=utf-8', success: function (data, textStatus) { if (data.Success) { window.location.href = data.RedirectUrl; } else { alert(data.ExceptionMessage); return; } }, error: function (xhr, status, error) { alert("An error occurred: " + status + "nError: " + error); } }); } return false; });
The edit button function needs to dynamically generate a new line. Process the html tags of each field:
##
$('.Edit').click(function (e) { var tr = $(this).closest('tr') var row = $('<tr>'); row.append($('<td><input class="city_key" type="hidden" value="' + tr.find('.SelectSingle').val() + '" /></td>')); row.append($('<td></td>')); $selectCity = $('<select />').attr({ name: 'city', class: 'selectcity' }); $("<option></option>", { value: "", text: "" }).appendTo($selectCity); $.getJSON("/Highway/GetCities", function (data) { $.each(data, function (i, item) { if (item.City_nbr == tr.find('.city_key').val()) { $("<option></option>", { value: item.City_nbr, text: item.City_Name,selected :"selected" }).appendTo($selectCity); } else { $("<option></option>", { value: item.City_nbr, text: item.City_Name}).appendTo($selectCity); } }) }); row.append($('<td></td>').append($selectCity)); row.append($('<td></td>')); row.append($('<td></td>')); row.append($('<td></td>')); $cb = $('<input/>').attr({ type: 'checkbox', class: 'ckbIsActived', checked: tr.find('.ckbIsActived').is(':checked') == true ? 'true' : '' }); row.append($('<td></td>').append($cb)); var $btnUpdate = $('<input/>').attr({ type: 'button', class: 'Update', value: '更新' }); row.append($('<td style="width:40px;"></td>').append($btnUpdate)); var $btnCancel = $('<input/>').attr({ type: 'button', class: 'Cancel', value: '取消' }); row.append($('<td style="width:40px;"></td>').append($btnCancel)); tr.after(row); $(this).attr("disabled", "disabled"); });
$('table.city-list').delegate('.Update', 'click', function (event) { var tr = $(this).closest("tr"); var obj = {}; obj.Ltc_nbr = tr.find('.city_key').val(); obj.City_nbr = tr.find('.selectcity').val(); obj.IsActived = tr.find('.ckbIsActived').is(':checked'); $.ajax({ type: 'POST', url: "/Highway/LandTransportationCityUpdate", dataType: 'json', data: JSON.stringify(obj), contentType: 'application/json; charset=utf-8', success: function (data, textStatus) { if (data.Success) { alert("陆运城市更新成功。"); window.location.href = data.RedirectUrl; } else { alert(data.ExceptionMessage); return; } }, error: function (xhr, status, error) { alert("An error occurred: " + status + "nError: " + error); } }); });
$('table.city-list').delegate('.Cancel', 'click', function (event) { var tr = $(this).closest("tr"); tr.prev().find('.Edit').removeAttr('disabled'); tr.remove(); });
Using bootstrap modal+gridview pop-up box effect to implement an example tutorial
Realizing the automatic scrolling function of GridView
How to implement addition, deletion and modification of DataGridView?
The above is the detailed content of jQuery implements functional examples similar to GridView. For more information, please follow other related articles on the PHP Chinese website!