JQuery通过AJAX从后台获取信息显示在表格上并支持行选中
作者:bea
不想用Easyui的样式,但是想要他的表格功能,本来一开始是要到网上找相关插件的,但是没找到就开始自己写,没想到这么简单。 后台代码:(这个不重要) public ActionResult GetDictTypes(){ var data = from a in dbo.DictTypes select new DictTypeListViewModel { ID = a.ID, Name = a.Nam
不想用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();
}
有用 | 无用
后台代码:(这个不重要)
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();
}
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- JQuery实现级联下拉框效果实例讲解
- JS+CSS实现精美的二级导航效果代码
- jQuery实现仿腾讯迷你首页选项卡效果代码
- jQuery实现仿美橙互联两级导航菜单效果完整实例
- JS实现无限级网页折叠菜单(类似树形菜单)效果代码
- jQuery实现折叠、展开的菜单组效果代码
- ANGULARJS中使用JQUERY分页控件
- js实现tab切换效果实例
- JS实现自动固定顶部的悬浮菜单栏效果
- jQuery图片轮播滚动切换代码分享
- JS实现光滑展开合拢的菜单效果代码
- jQuery实现两款有动画功能的导航菜单代码
- javascript中的previousSibling和nextSibling的正确用法
- Javascript实现商品秒杀倒计时(时间与服务器时间同步)
- JS+CSS实现的经典tab选项卡效果代码
- 使用JavaScript脚本无法直接改变Asp.net中Checkbox控件的Enable属性的解决方法
- jQuery实现的超酷苹果风格图标滑出菜单效果代码
- jQuery实现的简单折叠菜单(折叠面板)效果代码
- JS实现不规则TAB选项卡效果代码