如何通过选择行检索数据库数据

How to retrieve Database data from selecting rows

本文关键字:数据库 数据 检索 何通过 选择      更新时间:2023-09-26

如何从表中实际检索选定行的主键(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();
    });