JQuery通過AJAX從後台擷取資訊顯示在表格上並支援行選中_jquery

來源:互聯網
上載者:User

不想用Easyui的樣式,但是想要他的表格功能,本來一開始是要到網上找相關外掛程式的,但是沒找到就開始自己寫,沒想到這麼簡單。

後台代碼:(這個不重要)

public ActionResult GetDictTypes(){  var data = from a in dbo.DictTypes        select new DictTypeListViewModel        {          ID = a.ID,          Name = a.Name,          LastChangeUser = a.LastChangeUser,          LastChangeDate = a.LastChangeDate,          Remark = a.Remark        };  return Json(data.ToList());}

頁面代碼:

<table class="table" id="DictTypeTable"> <thead>  <tr>   <th>ID</th>   <th>標題</th>   <th>簡介</th>  </tr> </thead> <tbody class="sel"></tbody></table>

javascript代碼:(需要在 $(document).ready(function ($){ } 裡調用)

function ShowDictType() {  $('#DictTypeTable').children('tbody').empty();  $.ajax({    url: GetDictTypes_URL,    type: 'post',    dataType: 'json'  })   .done(function (data) {     var tbody = "";     $.each(data, function (index, el) {       var tr = "<tr>";       tr += "<td>" + el.ID + "</td>";       tr += "<td>" + el.Name + "</td>";       tr += "<td>" + el.Remark + "</td>";       tr += "</tr>";       tbody += tr;     });     $('#DictTypeTable').children('tbody').append(tbody);     BindDictTypeTableEvent();//這裡是綁定事件   })   .fail(function () {     alert("Err");   });}

要在表格產生之後再綁定事件:

function BindDictTypeTableEvent() {  $('#DictTypeTable tbody.sel').children('tr').click(function (event) {    $(this).siblings('tr').removeClass('active');//刪除其他行的選中效果    $(this).addClass('active');//增加選中效果    var id = $(this).children('td:eq(0)').text();//擷取ID    ShowDictData(id);//作業碼,這裡是顯示另一個表格式資料  });}

最後這裡是擷取選中條目ID的代碼:

function GetTypeTableSelectId() {  return $('#DictTypeTable tbody.sel tr.active td:eq(0)').text();}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.