Bootstrap table使用方法記錄
本文實(shí)例為大家分享了Bootstrap table的使用方法,供大家參考,具體內(nèi)容如下
HTML代碼:
/*index.cshtml*/
@section styles{
<style>
.main {
margin-top:20px;
}
.modal-body .form-group .form-control {
display:inline-block;
}
.modal-body .form-group .tips {
color:red;
}
</style>
}
<div class="main">
<div id="toolbar" class="btn-group">
<button id="addProductBtn" type="button" class="btn btn-default" onclick="showAddModal()">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增產(chǎn)品
</button>
</div>
<table id="table"></table>
</div>
<div class="modal fade" id="productModal" tabindex="-1" role="dialog" aria-labelledby="productModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="productModalLabel"></h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="prodId" class="col-md-2">編號(hào):</label>
<input type="text" class="form-control" id="prodId" disabled>
</div>
<div class="form-group">
<label for="prodName" class="col-md-2">名稱(chēng):</label>
<input type="text" class="form-control" id="prodName">
<span class="tips" >(最多20個(gè)字)</span>
</div>
<div class="form-group">
<label for="prodTecParam" class="col-md-2">技術(shù)參數(shù):</label>
<textarea rows="3" class="form-control" id="prodTecParam"></textarea>
<span class="tips" >(最多150個(gè)字)</span>
</div>
<div class="form-group">
<label for="prodType" class="col-md-2">類(lèi)型:</label>
<select class="form-control" id="prodType">
<option value="1001">普通產(chǎn)品</option>
<option value="1002">明星產(chǎn)品</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">關(guān)閉</button>
<button id="modalSubmitBtn" type="button" class="btn btn-primary">{{modalBtn}}</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal -->
</div>
@section scripts{
<script type="text/javascript" src="~/Scripts/bootstrap-table.js"></script>
<script type="text/javascript" src="~/Scripts/bootstrap-table-zh-CN.js"></script>
<script type="text/javascript" src="~/Scripts/common.js"></script>
<script type="text/javascript" src="~/Views/Home/index.js"></script>
}
JS代碼:
/*index.js*/
$(document).ready(function () {
var $table = $('#table');
var textLength = 30; //技術(shù)參數(shù)默認(rèn)折疊顯示長(zhǎng)度
$table.bootstrapTable({
locale: 'zh-CN',
url: '/product/getList',
method: 'post',
contentType: 'application/json',
dataType: "json",
toolbar: '#toolbar', //工具按鈕用哪個(gè)容器
pagination: true,
search: true,
showRefresh: true,
sidePagination: "server", //分頁(yè)方式:client客戶端分頁(yè),server服務(wù)端分頁(yè)
singleSelect: true, //單行選擇
pageNumber: 1, //初始化加載第一頁(yè),默認(rèn)第一頁(yè)
pageSize: 10, //每頁(yè)的記錄行數(shù)
pageList: [5, 10, 20],
queryParams: function (params) { //請(qǐng)求參數(shù)
var temp = {
pageSize: params.limit, //頁(yè)面大小
pageNo: params.offset / params.limit + 1, //頁(yè)碼
search: $('.search input').val()
};
return temp;
},
responseHandler: function (res) {
return {
pageSize: res.pageSize,
pageNumber: res.pageNo,
total: res.total,
rows: res.rows
};
},
columns: [
{
title: '產(chǎn)品編號(hào)',
field: 'id'
},
{
title: '產(chǎn)品名稱(chēng)',
width: 200,
field: 'name'
},
{
title: '技術(shù)參數(shù)',
field: 'tecParam',
width: 300,
formatter: tecParamFormatter,
events: {
"click .toggle": toggleText
}
},
{
title: '類(lèi)型',
field: 'type',
formatter: typeFormatter
},
{
title: '操作',
formatter: operateFormatter,
events: {
"click .mod": showUpdateModal,
"click .delete": deleteProduct
}
}
]
});
function tecParamFormatter(value, row, index) {
if (value != null && value.length > 30) {
return '<span class="tec-param">' + value.substr(0, textLength) + '...</span> <a class="toggle" href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" >展開(kāi)</a>';
}
return value;
}
function toggleText(e, value, row, index) {
if (value == null) {
return false;
}
var $tecParam = $(this).prev(".tec-param"),
$toggle = $(this);
if ($tecParam.text().length > textLength + 5) { //折疊
$tecParam.text(value.substr(0, textLength) + "...");
$toggle.text("展開(kāi)");
}
else if (value.length > textLength + 5 && $tecParam.text().length <= textLength + 5) { //展開(kāi)
$tecParam.text(value);
$toggle.text("折疊");
}
}
function typeFormatter(value, row, index) {
var type = "";
if (value == "1001")
type = "普通產(chǎn)品";
else if (value == "1002")
type = "明星產(chǎn)品";
return type;
};
function operateFormatter (value, row, index) {
return '<a class="mod btn btn-info" href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" >修改</a> '
+ '<a class="delete btn btn-danger" href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" >刪除</a>';
};
function showUpdateModal (e, value, row, index) {
$("#productModalLabel").text("更新產(chǎn)品信息");
$("#modalSubmitBtn").text("更新");
$("#prodId").val(row.id);
$("#prodId").attr("disabled", true); //禁止修改id
$("#prodName").val(row.name);
$("#prodTecParam").val(row.tecParam);
if (row.type == 1001)
$("#prodType").find('option[value="1001"]').attr("selected", true);
else if (row.type == 1002)
$("#prodType").find('option[value="1002"]').attr("selected", true);
$("#modalSubmitBtn").unbind();
$("#modalSubmitBtn").on("click", updateProduct);
$("#productModal").modal("show");
};
function deleteProduct (e, value, row, index) {
var product = {
id: row.id
};
if (product.id === null || product.id === "") {
return false;
}
Common.confirm({
message: "確認(rèn)刪除該產(chǎn)品?",
operate: function (result) {
if (result) {
$.ajax({
type: "post",
url: "/product/delete",
contentType: "application/json",
data: JSON.stringify(product),
success: function (data) {
if (data !== null) {
if (data.result) {
$("#table").bootstrapTable("refresh", { silent: true });
tipsAlert('alert-success', '提示', '刪除成功!');
}
else {
tipsAlert('alert-warning', '提示', '刪除失?。?);
}
}
},
error: function (err) {
tipsAlert('alert-danger', '警告', '服務(wù)器異常,請(qǐng)稍后再試!');
console.log("error:", err.statusText);
}
});
return true;
}
else {
return false;
}
}
});
};
var $search = $table.data('bootstrap.table').$toolbar.find('.search input');
$search.attr('placeholder', '請(qǐng)輸入編號(hào)、產(chǎn)品名稱(chēng)、技術(shù)參數(shù)搜索');
$search.css('width', '400');
$(".model .form-group input").on("click", function(){
$(this).next(".tips").text("");
});
});
var showAddModal = function () {
$("#productModalLabel").text("新增產(chǎn)品");
$("#modalSubmitBtn").text("新增");
$("#prodId").val('');
$("#prodName").val('');
$("#prodTecParam").val('');
$("#modalSubmitBtn").unbind();
$("#modalSubmitBtn").on("click", addProduct);
$("#productModal").modal("show");
};
var addProduct = function () {
var product = {
name: $("#prodName").val(),
tecParam: $("#prodTecParam").val(),
type: $("#prodType").val()
};
if (product.name == null || product.name == "") {
$("#prodName").next(".tips").text("產(chǎn)品名稱(chēng)不能為空!");
return false;
}
if (product.name.length > 20) {
$("#prodName").next(".tips").text("最多20個(gè)字!");
return false;
}
if (product.tecParam.length > 150) {
$("#prodTecParam").next(".tips").text("最多150個(gè)字!");
return false;
}
$.ajax({
type: "post",
url: "/product/add",
contentType: "application/json",
data: JSON.stringify(product),
success: function (data) {
if (data !== null) {
if (data.result) {
$("#table").bootstrapTable("refresh", { silent: true });
$("#productModal").modal('hide');
$("#prodId").val('');
$("#prodName").val('');
$("#prodTecParam").val('');
tipsAlert('alert-success', '提示', '新增成功!');
}
else {
tipsAlert('alert-warning', '提示', '新增失?。?);
}
}
},
error: function (err) {
tipsAlert('alert-danger', '警告', '服務(wù)器異常,請(qǐng)稍后再試!');
console.log("error:", err.statusText);
}
});
};
var updateProduct = function () {
var product = {
id: $("#prodId").val(),
name: $("#prodName").val(),
tecParam: $("#prodTecParam").val(),
type: $("#prodType").val()
};
if (product.name == null || product.name == "") {
$("#prodName").next(".tips").text("產(chǎn)品名稱(chēng)不能為空!");
return false;
}
if (product.name.length > 20) {
$("#prodName").next(".tips").text("最多20個(gè)字!");
return false;
}
if (product.tecParam.length > 150) {
$("#prodTecParam").next(".tips").text("最多150個(gè)字!");
return false;
}
$.ajax({
type: "post",
url: "/product/update",
contentType: "application/json",
data: JSON.stringify(product),
success: function (data) {
if (data !== null) {
if (data.result) {
$("#table").bootstrapTable("refresh", { silent: true });
$("#productModal").modal('hide');
$("#prodId").val('');
$("#prodName").val('');
$("#prodTecParam").val('');
tipsAlert('alert-success', '提示', '修改成功!');
}
else {
tipsAlert('alert-warning', '提示', '修改失?。?);
}
}
},
error: function (err) {
tipsAlert('alert-danger', '警告', '服務(wù)器異常,請(qǐng)稍后再試!');
console.log("error:", err.statusText);
}
});
};
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS表格組件神器bootstrap table詳解(基礎(chǔ)版)
- Bootstrap Table使用方法詳解
- JS組件Bootstrap Table使用方法詳解
- bootstrap table 服務(wù)器端分頁(yè)例子分享
- Bootstrap Table的使用總結(jié)
- BootStrap table表格插件自適應(yīng)固定表頭(超好用)
- 第一次動(dòng)手實(shí)現(xiàn)bootstrap table分頁(yè)效果
- BootStrap 可編輯表Table格
- Bootstrap嵌入jqGrid,使你的table牛逼起來(lái)
- Bootstrap table分頁(yè)問(wèn)題匯總
相關(guān)文章
threejs使用JSON格式保存和加載整個(gè)場(chǎng)景分析
本文介紹了如何使用Three.js將三維場(chǎng)景保存為JSON格式,并加載整個(gè)場(chǎng)景,通過(guò)調(diào)用各個(gè)對(duì)象的.toJSON()方法,可以保存和加載立方體、球體、obj、glb等三維模型的頂點(diǎn)和材質(zhì)數(shù)據(jù),文章詳細(xì)講解了實(shí)現(xiàn)思路和代碼樣例,一起看看吧2024-11-11
基于javascript實(shí)現(xiàn)的購(gòu)物商城商品倒計(jì)時(shí)實(shí)例
本文主要介紹了基于javascript實(shí)現(xiàn)的購(gòu)物商城商品倒計(jì)時(shí)實(shí)例,代碼詳細(xì),可直接復(fù)制試試看效果。需要的朋友可以參考借鑒2016-12-12
如何使用TS對(duì)axios的進(jìn)行簡(jiǎn)單封裝
在vue項(xiàng)目中和后臺(tái)交互獲取數(shù)據(jù)這塊,我們通常使用的是axios庫(kù),這篇文章主要給大家介紹了關(guān)于如何使用TS對(duì)axios的進(jìn)行簡(jiǎn)單封裝的相關(guān)資料,需要的朋友可以參考下2022-10-10
小程序短信驗(yàn)證碼頁(yè)面實(shí)現(xiàn)demo
這篇文章主要為大家介紹了小程序短信驗(yàn)證碼頁(yè)實(shí)現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
js點(diǎn)擊任意區(qū)域彈出層消失實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了js點(diǎn)擊任意區(qū)域彈出層消失實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
微信小程序Error:Fail?to?open?IDE問(wèn)題的解決方法
今天學(xué)習(xí)小程序時(shí)無(wú)法通過(guò)HBuilderX運(yùn)行微信小程序,查了相關(guān)資料后解決了,下面這篇文章主要給大家介紹了關(guān)于微信小程序Error:Fail?to?open?IDE問(wèn)題的解決方法,需要的朋友可以參考下2023-04-04
微信小程序?qū)崿F(xiàn)slideUp、slideDown滑動(dòng)效果及點(diǎn)擊空白隱藏功能示例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)slideUp、slideDown滑動(dòng)效果及點(diǎn)擊空白隱藏功能,涉及微信小程序事件響應(yīng)、頁(yè)面元素屬性動(dòng)態(tài)操作實(shí)現(xiàn)滑動(dòng)與蒙層功能相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下2018-12-12

