bootstrap Table插件使用demo
最近研究bootstrap,它僅提供視覺效果,對于數(shù)據(jù)列表之類的并未涉及,網(wǎng)上找了一下,找到一個Table插件。
名為bootstrapTable。
官方地址:http://bootstrap-table.wenzhixin.net.cn/examples/
github:https://github.com/wenzhixin/bootstrap-table
因為英文差,研究了半天,做了一個demo,將就看
HTML:
<table class="table" id="dataShow" >
<thead>
<tr>
<th data-checkbox="true">選擇</th>
<th data-field="rkey">供應(yīng)商名稱</th>
<th data-field="rkey">供應(yīng)商編碼</th>
<th data-field="name">物料編碼</th>
<th data-field="sex">申請類型</th>
<th data-field="birthdayString">試用申請編碼</th>
<th data-field="age">試用狀態(tài)</th>
<th data-field="age">廠別</th>
<th data-field="age">審批狀態(tài)</th>
<th data-field="birthday">申請時間</th>
<th data-field="age">試用結(jié)果</th>
</tr>
</thead>
</table>
JS:
var currPageIndex = 0;
var currLimit = 10;
$(function () {
$("#dataShow").bootstrapTable({
url: "TradHandler.ashx?request=getTradList",
sortName: "rkey",//排序列
striped: true,//條紋行
sidePagination: "server",//服務(wù)器分頁
//showRefresh: true,//刷新功能
//search: true,//搜索功能
clickToSelect: true,//選擇行即選擇checkbox
singleSelect: true,//僅允許單選
//searchOnEnterKey: true,//ENTER鍵搜索
pagination: true,//啟用分頁
escape: true,//過濾危險字符
queryParams: getParams,//攜帶參數(shù)
pageCount: 10,//每頁行數(shù)
pageIndex: 0,//其實頁
method: "get",//請求格式
//toolbar: "#toolBar",
onPageChange: function (number, size) {
currPageIndex = number;
currLimit = size
},
onLoadSuccess: function ()
{
$("#searchBtn").button('reset');
}
});
//搜索
$("#searchBtn").click(function () {
$(this).button('loading');
var nullparamss = {};
$("#dataShow").bootstrapTable("refresh", nullparamss);
});
//enter鍵搜索
$("#searchKey").keydown(function (event) {
if (event.keyCode == 13)
{
$("#searchBtn").click();
}
});
//阻止enter鍵提交表單
$("#mainForm").submit(function () {
return false;
});
});
//默認(rèn)加載時攜帶參數(shù)
function getParams(params) {
var searchKey = $("#searchKey").val();
return { bysex: 1, limit: params.limit, offset: params.offset, search: searchKey };
}
TradHandler.ashx:
/// <summary>
/// 獲取批量數(shù)據(jù)示例
/// </summary>
/// <param name="context"></param>
private void getTradList(HttpContext context)
{
//用于序列化實體類的對象
JavaScriptSerializer jss = new JavaScriptSerializer();
#region 模擬數(shù)據(jù)獲取
List<SimpleModel> list = new List<SimpleModel>();
for (int i = 0; i < 1000; i++)
{
list.Add(new SimpleModel() { age = 18, name = "小李" + i, rkey = i + 1, sex = "男" });
}
//請求中攜帶的條件
string bysex = context.Request.Params["bysex"];
string searchKey = context.Request.Params["search"];
//請求中攜帶的頁數(shù)和下標(biāo)
int dataIndex = Convert.ToInt32(context.Request.Params["offset"]);
int pageCount = Convert.ToInt32(context.Request.Params["limit"]);
//查詢滿足條件的數(shù)據(jù)
List<SimpleModel> getList;
if (bysex != null && searchKey != null)
{
getList = (from p in list
where p.sex == (bysex == "0" ? "女" : "男") && p.name.Contains(searchKey.Trim())
select p).ToList();
}
else
{
getList = list;
}
#endregion
//將結(jié)果增加一列序號列
Dictionary<int, SimpleModel> testModel = new Dictionary<int, SimpleModel>();
for (int i=0;i< getList.Count;i++)
{
testModel.Add(i + 1, getList[i]);
}
//給分頁實體賦值
PageModels<SimpleModel> model = new PageModels<SimpleModel>();
model.total = getList.Count;
if (getList.Count % pageCount == 0)
model.page = getList.Count / pageCount;
else
model.page = (getList.Count / pageCount) + 1;
//獲取對應(yīng)頁的數(shù)據(jù)
model.rows = testModel.Where(t => t.Key > dataIndex && t.Key <= dataIndex + pageCount).Select(t => t.Value).ToList();
//將查詢結(jié)果返回
context.Response.Write(jss.Serialize(model));
}
有同學(xué)問pagemodel實體類,這里也分享一下,泛型實體類,因為該插件需要這些屬性才能正常自動綁定
[Serializable]
public class TablePageModel<T>
{
/// <summary>
/// 總行數(shù)
/// </summary>
public long total { get; set; }
/// <summary>
/// 總頁數(shù)
/// </summary>
public int page { get; set; }
private List<T> _rows;
/// <summary>
/// 數(shù)據(jù)源
/// </summary>
public List<T> rows
{
get
{
if (_rows == null)
_rows = new List<T>();
return _rows;
}
set
{
_rows = value;
}
}
}
展示數(shù)據(jù)結(jié)果如下:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript庫urlcat?之URL構(gòu)建器庫
這篇文章主要介紹了JavaScript庫urlcat之URL構(gòu)建器庫,urlcat?是一個小型的JavaScript庫,使構(gòu)建URL非常方便并防止常見錯誤。下文來看對其詳細(xì)介紹吧,需要的小伙伴可以參考一下2022-02-02
javascript中創(chuàng)建對象的幾種方法總結(jié)
以下幾種,是javascript中最常用的創(chuàng)建對象的方式。初學(xué)者看到后,可能會暈掉,甚至?xí)X得擔(dān)心。其實完全不用擔(dān)心,這些種方式,只需要掌握一兩種,對其他的幾種只需要理解就好了2013-11-11
關(guān)于js中removeEventListener取消事件監(jiān)聽的坑
許多入前端不久的人都會遇到removeEventListener無法清除監(jiān)聽的情況,下面這篇文章主要給大家介紹了關(guān)于js中removeEventListener取消事件監(jiān)聽的坑,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
淺析javascript中函數(shù)聲明和函數(shù)表達(dá)式的區(qū)別
這篇文章主要介紹了淺析javascript中函數(shù)聲明和函數(shù)表達(dá)式的區(qū)別,需要的朋友可以參考下2015-02-02
javascript實現(xiàn)修改微信分享的標(biāo)題內(nèi)容等
這篇文章主要介紹了javascript實現(xiàn)修改微信分享的標(biāo)題內(nèi)容等,需要的朋友可以參考下2014-12-12
Javascript 鍵盤事件的組合使用實現(xiàn)代碼
Javascript 鍵盤事件的組合使用實現(xiàn)代碼,需要的朋友可以參考下2012-05-05

