2014-06-27 71 views
0

如何從我的表中實際檢索選定行的主鍵(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" }) 
+0

你想在哪裏找回ID?在行點擊事件? –

+0

@StephenMuecke是的。 – user3758689

回答

0

基於以上評論:

首先,你不能使用@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; 
}); 

該刪除禁用按鈕按鈕會更復雜一點 - 一種方法是循環選定的行,獲取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(); 
    });