如何通过选择行检索数据库数据
How to retrieve Database data from selecting rows
如何从表中实际检索选定行的主键(model.ID) ?我想使相同的情况下,如果单行被选中,按钮a B C被启用,但如果多行被选中,只有按钮C被启用,a和B将被禁用。我把我的代码贴在下面。
$(document).ready(function () {
var table = $('#LineTables').DataTable();
$('#LineTables tbody').on('click', 'tr', function () {
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
}
else {
table.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
}
});
});
});
<table id="LineTables" class="display dataTable">
<thead>
<tr>
<th>@Html.DisplayNameFor(model => model.Priority)</th>
<th>@Html.DisplayNameFor(model => model.ProductionOrderNumber)</th>
<th>@Html.DisplayNameFor(model => model.SalesOrder.Product.ProductGroup.Name)</th>
<th>@Html.DisplayNameFor(model => model.SalesOrder.Product.ProductName)</th>
<th>@Html.DisplayNameFor(model => model.Quantity)</th>
<th>@Html.DisplayNameFor(model => model.SalesOrder.Product.Pole)</th>
<th>@Html.DisplayNameFor(model => model.SalesOrder.Product.Amperage)</th>
<th>@Html.DisplayNameFor(model => model.SalesOrder.SalesOrderType1.Name)</th>
<th>@Html.DisplayNameFor(model => model.SalesOrder.Market)</th>
<th>@Html.DisplayNameFor(model => model.ProductionOrderStatu.Name)</th>
<th>@Html.DisplayNameFor(model => model.SalesOrder.SalesOrderNumber)</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Priority)</td>
<td>@Html.DisplayFor(modelItem => item.ProductionOrderNumber)</td>
<td>@Html.DisplayFor(modelItem => item.SalesOrder.Product.ProductGroup.Name)</td>
<td>@Html.DisplayFor(modelItem => item.SalesOrder.Product.ProductName)</td>
<td>@Html.DisplayFor(modelItem => item.Quantity)</td>
<td>@Html.DisplayFor(modelItem => item.SalesOrder.Product.Pole)</td>
<td>@Html.DisplayFor(modelItem => item.SalesOrder.Product.Amperage)</td>
<td>@Html.DisplayFor(modelItem => item.SalesOrder.SalesOrderType1.Name)</td>
<td>@Html.DisplayFor(modelItem => item.SalesOrder.Market)</td>
<td>@Html.DisplayFor(modelItem => item.ProductionOrderStatu.Name)</td>
<td>@Html.DisplayFor(modelItem => item.SalesOrder.SalesOrderNumber)</td>
<td>
@Html.ActionLink("New Barcode", "Barcode", new { id = item.ID }, new { @class = "barcode btnic btnicon" })
</td>
</tr>
} </tbody>
</table>
@Html.ActionLink("A", "Index", "A", .......... , new { @class = "btn btn-default btn-ms" })
@Html.ActionLink("B", "Index", "B", .......... , new { @class = "btn btn-default btn-ms" })
@Html.ActionLink("C", "Index", "C", .......... , new { @class = "btn btn-default btn-ms" })
基于上面的注释:
首先,你不能使用@Html.ActionLink(..)
按钮(这些是在服务器端呈现的,你不知道在这一点上所选择的ID) -使用html <button id="edit">Edit</button>
。坐在那排。单击功能,测试所选行数,并根据需要启用/禁用按钮
var selectedRows = $(this).closest('tbody').children('tr')
.filter('.selected').length;
if (selectedRows = 1) {
// enable edit and details buttons
} else {
// disable edit and details buttons
}
然后在每个按钮的点击事件中(显示编辑按钮)
$('#edit').click(function() {
var ID = $(this).find('input[type="hidden"]').val(); // as per first answer
window.location.href = '/yourController/edit/' + ID;
return false;
});
删除按钮将稍微复杂一点-一种方法是循环选中的行,获取ID并使用AJAX将ID发布到您的' delete '操作方法,然后在成功函数中,从表中删除行,例如(未测试)
var url = "@Url.Action("Delete")"; // your delete action
$('#delete').click(function() {
$('#LineTables').children('tbody').children('tr')
.filter('.selected').each(function) {
var row = $(this);
var ID = row.find('input[type="hidden"]').val();
$.post(url, { ID = ID }, function() {
row.remove();
});
相关文章:
- 在php和mysql中选择选项.一旦选择了选项,就列出我的sql数据库数据
- AJAX,用于显示数据库数据
- 如何在 JavaScript 函数中使用 MySQL 数据库数据
- JavaScript 中数据库数据的未终止字符串文字
- 内容可更改 来自数据库数据的下拉列表
- 访问数据库数据类型错误
- 使用 Phonegap 检索数据库数据
- 引导数据库数据切换在同时使用两个标签时不使用 href #home 选项卡
- 如何在 ASP.NET 中动态地将数据库数据添加到 Jquery 控件
- 在数据库数据更改时向所有活动会话发送响应
- 如何使用php在mysql数据库数据更改上自动发送SMS
- 是否可以使用客户端代码检索数据库数据
- 将数据库数据检索到JSON-移动登录表单
- PHP/MySQL-数据库数据的复选框,提交2个参数
- 动态表单字段和数据库数据检索
- 在原生脚本应用程序中显示sqlite数据库数据有问题
- 如何在 JavaScript 中访问从 Recordset 中检索到的数据库数据
- PHP脚本返回整个网页,而不是数据库数据
- 如何重写jQuery日历的删除,以便我也可以删除我的数据库数据
- 为amstockcharts使用数据库数据