簡體   English   中英

如何從選擇行中檢索數據庫數據

[英]How to retrieve Database data from selecting rows

我實際上如何從表中檢索所選行的主鍵(model.ID)。 我想做相同的情況,如果選擇了單行,則啟用按鈕ABC,但如果選擇了多行,則僅啟用按鈕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> 在row.click函數中,測試選定的行數並根據需要啟用/禁用按鈕

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;
});

Delete按鈕會稍微復雜一些-一種方法是循環選擇的行,獲取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();
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM