현재 내 페이지에 추가 기능을 구현하려고합니다. 현재 추가 기능은 이미 작동하지만 새로 추가 된 행을 표에 표시하려면 페이지를 새로 고쳐야합니다. 이것은 내가 내 테이블을 생성하는 방법이다jQuery - MVC 4를 사용하여 테이블 행 추가
:
아래에 내 코드를 참조하십시오 현재
<table class="webGrid" id="ProductsTable">
<tr>
<td><strong><span>ProductID</span></strong></td>
<td><strong><span>ProductName</span></strong></td>
<td><strong><span>Price</span></strong></td>
<td colspan="2"><strong><span>Action</span></strong></td>
</tr>
@foreach (var item in repository.GetAllProducts(ViewData["BranchCode"].ToString()))
{
<tr>
<td><span class="ProductID">@item.ProductID</span></td>
<td><span class="ProductName">@item.ProductName</span></td>
<td><span class="Price">@item.Price</span></td>
<td>@Html.ActionLink("Edit", "Edit", new { RecordID = item.RecordID }, new { @class = "editLink" })</td>
<td>@Html.ActionLink("Delete", "Delete", new { RecordID = item.RecordID }, new { @class = "editLink" })</td>
</tr>
}
, 편집 및 삭제는 이미 잘 작동하고 있습니다. 지금은 거의 내가 사용하고있는 편집 JQuery와 동일하다 추가 기능을 구현하기 위해 노력하고
function update(data) {
if (data.Success == true) {
var parent = linkObj.closest("tr");
parent.find(".ProductID").html(data.Object.ProductID);
parent.find(".ProductName").html(data.Object.ProductName);
parent.find(".Price").html(data.Object.Price);
}
else {
$("#update-message").html(data.ErrorMessage);
$("#update-message").show();
}
}
: 아래는 내가 편집을하는 방법이다. .append
메서드를 성공적으로 사용하지 못했습니다.
편집 :
나는 추가 아래의 코드를 사용하여 시도했다. 하지만 아무 것도하지 않는 것 같습니다.
<script type="text/x-template" id="rowTemplate">
<tr><td><input type="text" id="txtName_$Counter" value="" /></td></tr>
</script>
난 그냥 온라인이 솔루션을 발견하지만 난이 일을 할 수 없습니다 : 다음과 같은
가function add(data) {
if (data.Success == true) {
var rowTemplate = $("#rowTemplate").html();
var tbl = document.getElementById("ProductsTable");
var counter = $("#ProductsTable tr").length;
data.Counter = counter;
$("#ProductsTable").append(applyTemplate(rowTemplate, data));
}
else {
$("#update-message").html(data.ErrorMessage);
$("#update-message").show();
}
}
function applyTemplate(template, data) {
var str = template;
if ($.isPlainObject(data)) {
$.each(data, function (index, value) {
var find = new RegExp("\\$1" + index, "ig");
str = str.replace(/\$/g, "\$1");
str = str.replace(find, value);
});
}
return str;
}
그것은 행 템플릿을 사용한다 : 아니면 내가 뭔가 잘못하고 있어요 . 나는 또한 (난 단지 내가 가지고있는 편집 JQuery와 기반으로 만든) 아래의 코드를 시도 :
function add(data) {
if (data.Success == true) {
var parent = linkObj.closest("tr");
parent.find(".ProductID").append(data.Object.ProductID);
parent.find(".ProductName").append(data.Object.ProductName);
parent.find(".Price").append(data.Object.Price);
}
else {
$("#update-message").html(data.ErrorMessage);
$("#update-message").show();
}
}
편집 :
을 내 jQuery를이처럼 보이는 방법은 지금이다
function add(data) {
if (data.Success == true) {
data = { Counter: 3 };
$("#rowTemplate").tmpl(data).appendTo("#ProductsTable");
$('#updateDialog').dialog('close');
}
else {
$("#update-message").html(data.ErrorMessage);
$("#update-message").show();
}
}
이 내 템플릿입니다 :
<script type="text/x-template" id="rowTemplate">
<tr>
<td><span class="ProductID"></span></td>
<td><span class="ProductName"></span></td>
<td><span class="Price"></span></td>
<td>@Html.ActionLink("Edit", "_EditProduct", new { @class = "editLink" })</td>
<td>@Html.ActionLink("Delete", "_DeleteProduct", new { @class = "editLink" })</td>
</tr>
</script>
행을 추가하는 jQuery 작업을 도와 준 Sir @Muhammad Adeel Zahid에게 감사드립니다. 그러나, 그것은 단지 내 테이블에 새로운 행을 추가합니다. 지금 필요한 것은 jQuery의 add function
에있는 data
객체의 값을 추가하는 것입니다.
나는 THIS LINK에서 튜토리얼을 시도했지만 제대로 작동하지 않는 것 같습니다. 내 코드는 다음과 같습니다.
function add(data) {
if (data.Success == true) {
var prod = $.map(data.results, function (obj, index) {
return {
ProductID: obj.text,
ProductName: obj.text,
Price: obj.text
};
});
prod = { Counter: 3 };
$("#rowTemplate").tmpl(prod).appendTo("#ProductsTable");
$('#updateDialog').dialog('close');
}
else {
$("#update-message").html(data.ErrorMessage);
$("#update-message").show();
}
}
도움을 주셔서 감사합니다.
추가 기능을 위해 어떤 시도를 게시 할 수 있습니까? 그것이 작동하지 않으면 중요하지 않습니다, 그것은 우리에게 시작할 곳을 줄 것입니다. –
안녕하세요. 응답 해 주셔서 감사합니다. 위의 편집 내용을 참조하십시오. :) – Smiley
오류가 있습니까? 화재 경보기 나 크롬 콘솔로 확인할 수 있습니다. –