新建table新增行或者在当前table中新增行

<body class="no-skin">
<table class="table table-bordered table-condensed" style="width:98%">
<tr>
<td>
维修/保养单号
</td>
<td>
@ViewData["morder"]
</td>
<td>
设备编号
</td>
<td>
@ViewData["equipid"]
</td>
<td>
工厂
</td>
<td>
<input class="form-control" id="plant" name="plant" placeholder="" readonly="readonly" type="text" value="@ViewData["plant"]">
</td>
</tr>
</table>

<table id="tb1" class="table table-bordered table-condensed select_tableY">
<thead>
<tr>
<th style="width: 30px">
配件编号
</th>
<th style="width: 50px">
配件描述
</th>
<th style="width: 30px">
保修期
</th>
<th style="width: 30px">
配件消耗量
</th>
<th style="width: 30px">
单位
</th>
<th style="width: 60px">
序列号
</th>
<th style="width: 30px">
具体更换位置
</th>
<th style="width: 30px">
保修日期
</th>
<th style="width: 30px">
选项
</th>
</tr>
</thead>
<tbody id="spdetailss">
@foreach (var item in ViewBag.DList)
{
<tr>
<td><label class="control-label" id="SparePartNo" name="SparePartNo">@Html.Raw(item.SparePartNo)</label></td>
<td><label class="control-label" id="SPDesc" name="SPDesc">@Html.Raw(item.SPDesc)</label> </td>
<td><label class="control-label" id="WarrantyPeriod" name="WarrantyPeriod">@Html.Raw(item.WarrantyPeriod + " " + item.WarrantyPeriodUnit)</label></td>
<td><input class="form-control" id="SparePartQty" name="SparePartQty" placeholder="" readonly="readonly" type="text" value="@Html.Raw(item.SparePartQty)"></td>
<td><label class="control-label" id="Unit" name="Unit">@Html.Raw(item.Unit)</label></td>
<td style="width: 80px">
<div style="position: relative;">
<input id="Spareno" name="Spareno" placeholder="" type="text" value="@Html.Raw(item.Spareno)" style="width:60px"> <a href="javascript:void(0)" class="autono_option btn btn-xs btn-info">派号</a>
</div>
</td>
<td><input class="form-control" id="Remark" name="Remark" placeholder="" type="text" value="@Html.Raw(item.Remark)"> </td>
@*<td><textarea class="form-control" name="Remark">@Html.Raw(item.Remark)</textarea></td>*@
<td><input class="form-control date-picker" data-date-format="yyyy-mm-dd" data-val="true" id="WarrantyDate" name="WarrantyDate" type="text" value=""></td>
<td>
@*<a href="javascript:void(0)" class="delete_option">删除</a>*@
<a href="javascript:void(0)" class="select_option">选择</a>
<a href="javascript:void(0)" class="return_option">退仓</a>


</td>
</tr>
}


</tbody>
</table>
<table id="tb2" class="table table-bordered table-condensed select_table">
<tbody>
<tr class="choosen">
<th style="width: 30px">
配件编号
</th>
<th style="width: 50px">
配件描述
</th>
<th style="width: 30px">
保修期
</th>
<th style="width: 30px">
配件消耗量
</th>
<th style="width: 30px">
单位
</th>
<th style="width: 30px">
序列号
</th>
<th style="width: 30px">
具体更换位置
</th>
<th style="width: 30px">
保修日期
</th>
<th style="width: 30px">
选项
</th>
</tr>
</tbody>
</table>
<td class="bg-info">
<button id="btn_submit" type="button" class="btn btn-primary btn-lg" style="display:block;margin:0 auto" onclick="SaveSp();">保存</button>
</td>
</body>
@*}*@
<link rel="stylesheet" href="~/Content/ace/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/Content/ace/css/font-awesome.min.css" />
<link rel="stylesheet" href="~/Content/ace/css/ace-fonts.css" />
<link rel="stylesheet" href="~/Content/ace/css/ace.min.css" id="main-ace-style" />
<link href="~/Content/ace/css/jquery.gritter.css" rel="stylesheet" />
<link href="~/Content/ace/css/bootstrap-dialog.css" rel="stylesheet" />
<link href="~/Content/ace/css/ace-extenssion.css" rel="stylesheet" />
@*<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Nunito" />*@
<!--[if lte IE 9]>
<link rel="stylesheet" href="~/Content/ace/css/ace-part2.min.css" />
<![endif]-->
<link rel="stylesheet" href="~/Content/ace/css/ace-skins.min.css" />
<link rel="stylesheet" href="~/Content/ace/css/ace-rtl.min.css" />
<!-- current site -->
<link href="~/Content/Site.css" rel="stylesheet" />
<script type="text/javascript">
var addobj;
$(function () {
$(".select_option").each(function (index) {
$(this).click(function () {
$(this).attr("data-index", index)
$(this).html("删除");
$(this).addClass('btn_delete')
//$(this).parents("tr").find(".return_option").remove()
// console.log($(this).parents("tr"))
$(".choosen").after($(this).parents("tr").clone());
var cloneobj = $(this).parents("tr").clone();
addobj = cloneobj;
cloneobj = cloneobj.find(".return_option").remove().html(); //复制行并删除一个按钮
$(this).parents("tr").after(addobj);
$("#tb2").find(".delete_option").remove()
$(this).html("已选");
//$(this).append(' <a href="javascript:void(0)" class="return_option">退仓</a>')
$(this).removeClass('btn_delete')
$('.date-picker').datepicker({
format: "yyyy-mm-dd",
weekStart: 1,
todayBtn: 1,
autoclose: 1,
todayHighlight: 1,
forceParse: true
});
})
})

$(".delete_option").each(function (index) {
$(this).click(function () {
$(this).parents("tr").remove()
})
})

$(".autono_option").each(function (index) {
$(this).click(function () {
debugger;
var plant = $('#plant').val();
var bb = $(this).parents("tr").find("label[name = 'SparePartNo']").text();
$.ajax({
url: '/Home/GetAutoNO?Plant=' + $('#plant').val() + '&SPItemNo=' + $(this).parents("tr").find("label[name = 'SparePartNo']").text(),
@* content: "/@(ViewContext.RouteData.Values["controller"])/SPadd?morder=" + $(this).attr("data-key") + "&mainwc=" + $(this).attr("data-mainwc") + "&equipid=" + $(this).attr("data-equipid") + "&plant=" + $(this).attr("data-plnt"),*@
dataType: "json",
success: function (result) {
$("#Spareno").val(result.NoAuto);
$("#btn_noauto").attr("disabled", true);
}
})
})
})

})

$(function () {
$("body").on("click", ".btn_delete", function () {
debugger;
var index = Number($(this).attr("data-index")) + 1
console.log(index)
console.log($(".select_table tr").eq(index).clone().html())
$(".select_tableY tr").eq(index).find(".select_option").html('选择')
$(this).parents("tr").remove()
})
})

function SaveSp() {
var result = []
$('#tb1 tbody tr').each(function (i, o) {
var thisItem = $(this);
result.push({
MOrder: $(o).find("label[name='SPDesc']").val(),
Plant: $("#Plant").val(),
SparePartNo: $(o).find("label[name='SparePartNo']").text(), //$(o).find("input[name='MOrder']").val()
WarrantyDate: $(o).find("input[name='WarrantyDate']").val(),
SparePartQty: $(o).find("input[name='SparePartQty']").val(),
SPDesc: $(o).find("label[name='SPDesc']").text(),
Unit: $(o).find("label[name='Unit']").text(),
WarrantyPeriod: $(o).find("label[name='WarrantyPeriod']").text().split(" ")[0],
WarrantyPeriodUnit: $(o).find("label[name='WarrantyPeriod']").text().split(" ")[1],
Spareno: $(o).find("input[name='Spareno']").val(),
Remark: $(o).find("input[name='Remark']").val(),
})
});

$('#tb2 tr').each(function (i, o) {
result.push({
MOrder: $(o).find("label[name='SPDesc']").val(),
Plant: $("#Plant").val(),
SparePartNo: $(o).find("label[name='SPDesc']").text(), //$(o).find("input[name='MOrder']").val()
WarrantyDate: $(o).find("input[name='WarrantyDate']").val(),
SparePartQty: $(o).find("input[name='SparePartQty']").val(),
SPDesc: $(o).find("label[name='SPDesc']").text(),
Unit: $(o).find("label[name='Unit']").text(),
WarrantyPeriod: $(o).find("label[name='WarrantyPeriod']").text().split(" ")[0],
WarrantyPeriodUnit: $(o).find("label[name='WarrantyPeriod']").text().split(" ")[1],
Spareno: $(o).find("input[name='Spareno']").val(),
Remark: $(o).find("input[name='Remark']").val(),
})
});

$.ajax({
//url: "/MiProjectDetail/Save" ,
url: "@Url.Action("SPOAaddSave", "EM")",
data: { Sp: JSON.stringify(result), mainwc: "", equipid: "" },
type: "Post",
dataType: 'json',
success: function (data) {
debugger;
if (data.Success) {
layer.msg('success');
window.parent.location.reload();
var index = parent.layer.getFrameIndex(window.name);
parent.layer.close(index);

} else {
debugger;
var aa = data.Errors[0];
layer.msg(aa);
}
}
});
}
</script>

猜你喜欢

转载自www.cnblogs.com/Depingblogs/p/13201144.html