2014-01-30 31 views
2

我想從asp.net mvc5中的ajax源創建一個jQuery數據表。 我想添加一個額外的列進行編輯和刪除,這不在我的模型類或ajax數據源中。對於編輯和刪除功能,我需要Id列的值,我沒有在我的數據表中顯示。如何從ajax數據源在jQuery數據表中獲取mRender()函數中的隱藏列值

這裏是我的模型類:

public class Country 
{ 
    public int Id { get; set; } 
    [Required(ErrorMessage = "Country Code Name Must not be empty")] 
    public String Code { get; set; } 
    [Required(ErrorMessage = "Country Name Must not be empty")] 
    public String Name { get; set; } 
    [Required(ErrorMessage = "Template Name Must not be empty")] 
    public String Template { get; set; } 
    [Required(ErrorMessage = "SPViews Name Must not be empty")] 
    public String SPViews { get; set; } 
} 

這裏是我的控制器:

public ActionResult GetAll(JQueryDataTableParamModel param) 
    { 
     var countryList = _repository.GetAll().ToList(); 
     var filteredCountry = (from e in countryList 
           where (param.sSearch == null || e.Name.ToLower().Contains(param.sSearch.ToLower())) 
           select e).ToList(); 
     var result = from country in filteredCountry.Skip(param.iDisplayStart).Take(param.iDisplayLength) 
        select new[] { country.Id,country.Code, country.Name, country.Template, country.SPViews }; 

     return Json(new 
     { 
      sEcho = param.sEcho, 
      iTotalRecords = countryList.Count(), 
      iTotalDisplayRecords = filteredCountry.Count, 
      aaData = result 
     }, JsonRequestBehavior.AllowGet); 
    } 

這裏是我的HTML表格:

<table id="countryListTable" class="table table-condensed"> 
<thead> 
    <tr> 
     <th>Id</th> 
     <th>Code</th> 
     <th>Name</th> 
     <th>Template</th> 
     <th>SPViews</th> 
     <th>&nbsp;</th> 
    </tr> 
</thead> 
<tbody> 
</tbody> 
</table> 

,最後這裏是我的jQuery代碼:

 var countryTable = $("#countryListTable").dataTable({ 
      "bServerSide": true, 
      "bProcessing": true, 
      "sAjaxSource": "/Country/GetAll", 
      "aoColumns": [ 
       null, 
       null, 
       null, 
       null, 
       {  // fifth column (Edit link) 
        "mData": "Id", 
        "bSearchable": false, 
        "bSortable": false, 
        "mRender": function (nRow, aData) { 
         //need to get the Id column value 
         return '<a class="glyphicon glyphicon-pencil" href="/Country/Edit/">Edit</a><a class="glyphicon glyphicon-remove" href="/Country/Delete/">Delete</a>'; 
        } 
       } 
      ] 
     }); 

任何幫助,將不勝感激。 問候:)

回答

2

@ mg1075感謝您的回覆。功能似乎是deprecated。我沒有嘗試你的解決方案,但我使用mRender函數以另一種方式修復了它。 因此,這裏是我的解決方案:

 countryTable = $("#countryListTable").dataTable({ 
      "bServerSide": true, 
      "bProcessing": true, 
      "sAjaxSource": "/Country/GetAll", 
      "aoColumns": [ 
       { "bVisible": false }, 
        null, 
        null, 
        null, 
        null, 
        { 
         mData: 0,//The Id column 
         "bSearchable": false, 
         "bSortable": false, 
         mRender: function (data, type, row) { 

          return '<a class="glyphicon glyphicon-pencil" onclick="editCountry(\'/Country/Edit/' + data + '\');return false;">Edit</a><a class="glyphicon glyphicon-remove" onclick="deleteCountry(\'/Country/Delete/' + data + '\');return false;">Delete</a>'; 
         } 
        }], 

     }); 

我覺得這兩個方法應該是完美的

3

首先,我會嘗試使用aoColumnDefs而非aoColumns
由於每數據表文件:

http://datatables.net/usage/columns

aoColumnDefs:該數組,您可以定位一個特定的列, 多列,或所有列,利用在各 對象的aTargets屬性該數組(請注意,aColumnDefs是在 DataTables 1.7中引入的)。由於aoColumnDefs數組可以具有任意長度,所以創建表時可以提供極大的靈活性, 可以針對您特別需要的列 。

接下來,我不能完全告訴你打算如何使用Id的編輯和刪除鏈接, 但這裏的Id附加到url

"aoColumnDefs": [ 
     { "mData": "Code ", "aTargets": [ 0 ] }, 
     { "mData": "Name", "aTargets": [ 1 ] }, 
     { "mData": "Template", "aTargets": [ 2 ] }, 
     { "mData": "SPViews", "aTargets": [ 3 ] },    
     { "mData": "Id", "aTargets": [ 4 ], 
      "mRender": function (data, type, full) { 
       return '<a class="glyphicon glyphicon-pencil" href="/Country/Edit/' + data + '">Edit</a><a class="glyphicon glyphicon-remove" href="/Country/Delete/' + data + '">Delete</a>'; 
      } 
     }, 
     { "bSortable": false, "aTargets": [ 4 ] } 
    ], 

...這裏的Id顯示爲data- attribute其價值,你可以訪問,例如,使用jQuery:

"aoColumnDefs": [ 
     { "mData": "Code ", "aTargets": [ 0 ] }, 
     { "mData": "Name", "aTargets": [ 1 ] }, 
     { "mData": "Template", "aTargets": [ 2 ] }, 
     { "mData": "SPViews", "aTargets": [ 3 ] },    
     { "mData": "Id", "aTargets": [ 4 ], 
      "mRender": function (data, type, full) { 
       return '<a class="glyphicon glyphicon-pencil" data-countryid="' + data +'" href="/Country/Edit/">Edit</a><a class="glyphicon glyphicon-remove" data-countryid="' + data +'" href="/Country/Delete/">Delete</a>'; 
      } 
     }, 
     { "bSortable": false, "aTargets": [ 4 ] } 
    ], 
相關問題