欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JQuery通過(guò)AJAX從后臺(tái)獲取信息顯示在表格上并支持行選中

 更新時(shí)間:2015年09月15日 17:53:59   投稿:mrr  
這篇文章主要介紹了JQuery通過(guò)AJAX從后臺(tái)獲取信息顯示在表格上并支持行選中的相關(guān)資料,需要的朋友可以參考下

不想用Easyui的樣式,但是想要他的表格功能,本來(lái)一開(kāi)始是要到網(wǎng)上找相關(guān)插件的,但是沒(méi)找到就開(kāi)始自己寫,沒(méi)想到這么簡(jiǎn)單。

后臺(tái)代碼:(這個(gè)不重要)

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

頁(yè)面代碼:

<table class="table" id="DictTypeTable">
 <thead>
  <tr>
   <th>ID</th>
   <th>標(biāo)題</th>
   <th>簡(jiǎn)介</th>
  </tr>
 </thead>
 <tbody class="sel"></tbody>
</table>

javascript代碼:(需要在 $(document).ready(function ($){ } 里調(diào)用)

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);//操作代碼,這里是顯示另一個(gè)表格數(shù)據(jù)
  });
}

最后這里是獲取選中條目ID的代碼:

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

相關(guān)文章

  • Jquery替換已存在于element上的event的方法

    Jquery替換已存在于element上的event的方法

    如何替換獲去除那些已存在于某個(gè)element上的事件呢? 需要的朋友可以參考下面的文章。
    2010-03-03
  • jQuery實(shí)現(xiàn)按比例縮放圖片的方法

    jQuery實(shí)現(xiàn)按比例縮放圖片的方法

    這篇文章主要介紹了jQuery實(shí)現(xiàn)按比例縮放圖片的方法,涉及jQuery針對(duì)頁(yè)面元素的獲取、屬性運(yùn)算、設(shè)置等相關(guān)操作技巧,需要的朋友可以參考下
    2017-04-04
  • 最新評(píng)論