


Bootstrap table table component artifact of JS component series [Final Chapter]_javascript skills
May 16, 2016 pm 03:01 PMbootstrap table series:
Detailed explanation of JS table component artifact bootstrap table (basic version)
Bootstrap table table component artifact of JS component series [Final Chapter]
Bootstrap Table is a lightweight and feature-rich data display in the form of a table. It supports radio selection, checkbox, sorting, paging, show/hide columns, fixed title scrolling table, responsive design, and Ajax loading. JSON data, click-sorted columns, card views, and more. So this article will introduce to you the Bootstrap table component artifact of the JS component series [Final Chapter], let’s learn together!
1. Effect display
1. Table row style
For example, we have a requirement to display an order page. Orders with different statuses display different colors, as shown in the figure:
2. Table inline editing
During the first article, some garden friends asked the blogger whether the effect of in-line editing can be supported, and the answer is yes. Let’s take a look at the effect first:
Before editing
Click on a cell data
After editing
3. Merge table rows and columns
Bloggers think that the need to merge rows and columns is very common, especially when making page reports. Let’s take a look at the effect first:
The current page is not fully displayed, click to enter and take a look. How about it? The effect is not bad.
4. Export table data
Regarding table data export, bootstrap table supports three modes of export: basic, all, and selected. That is, the current page data export, all data export, and selected data export. It also supports exporting multiple types of files, such as common excel, xml, json and other formats.
Export current page to excel
Export all data in the table
Export selected row data
As for the export of other types of files, it is basically the same as excel, so the effects will not be displayed.
2. Table row style code example
Regarding the style setting of table rows, the others are one of its most basic functions. Why put it in the third article? It’s because the blogger thinks this feature may be useful everywhere. Of course, the effect is not difficult. You can also use jQuery to set the background color of tr. However, the blogger feels that since bootstrap table provides a mechanism to set the background color of rows, why don't we use its built-in API. Let's see how to do it.
When initializing the form
//初始化Table $('#tb_order').bootstrapTable({ url: '/TableStyle/GetOrder', //請(qǐng)求后臺(tái)的URL(*) method: 'get', //請(qǐng)求方式(*) //toolbar: '#toolbar', //工具按鈕用哪個(gè)容器 striped: true, //是否顯示行間隔色 cache: false, //是否使用緩存,默認(rèn)為true,所以一般情況下需要設(shè)置一下這個(gè)屬性(*) pagination: true, //是否顯示分頁(*) sortable: false, //是否啟用排序 sortOrder: "asc", //排序方式 queryParams: oTableInit.queryParams,//傳遞參數(shù)(*) sidePagination: "server", //分頁方式:client客戶端分頁,server服務(wù)端分頁(*) pageNumber: 1, //初始化加載第一頁,默認(rèn)第一頁 pageSize: 10, //每頁的記錄行數(shù)(*) pageList: [10, 25, 50, 100], //可供選擇的每頁的行數(shù)(*) search: true, //是否顯示表格搜索,此搜索是客戶端搜索,不會(huì)進(jìn)服務(wù)端,所以,個(gè)人感覺意義不大 strictSearch: true, showColumns: true, //是否顯示所有的列 showRefresh: true, //是否顯示刷新按鈕 minimumCountColumns: 2, //最少允許的列數(shù) clickToSelect: true, //是否啟用點(diǎn)擊選中行 height: 500, //行高,如果沒有設(shè)置height屬性,表格自動(dòng)根據(jù)記錄條數(shù)覺得表格高度 uniqueId: "ID", //每一行的唯一標(biāo)識(shí),一般為主鍵列 showToggle: true, //是否顯示詳細(xì)視圖和列表視圖的切換按鈕 cardView: false, //是否顯示詳細(xì)視圖 detailView: false, //是否顯示父子表 rowStyle: function (row, index) { //這里有5個(gè)取值代表5中顏色['active', 'success', 'info', 'warning', 'danger']; var strclass = ""; if (row.ORDER_STATUS == "待排產(chǎn)") { strclass = 'success';//還有一個(gè)active } else if (row.ORDER_STATUS == "已刪除") { strclass = 'danger'; } else { return {}; } return { classes: strclass } }, columns: [{ checkbox: true }, { field: 'ORDER_NO', title: '訂單編號(hào)' }, { field: 'ORDER_TYPE', title: '訂單類型' }, { field: 'ORDER_STATUS', title: '訂單狀態(tài)' }, { field: 'REMARK', title: '備注' }, ] });
In fact, the key point lies in this parameter:
rowStyle: function (row, index) { //這里有5個(gè)取值代表5中顏色['active', 'success', 'info', 'warning', 'danger']; var strclass = ""; if (row.ORDER_STATUS == "待排產(chǎn)") { strclass = 'success';//還有一個(gè)active } else if (row.ORDER_STATUS == "已刪除") { strclass = 'danger'; } else { return {}; } return { classes: strclass } },
bootstrap table支持5中表格的行背景色,分別是'active', 'success', 'info', 'warning', 'danger'這五種,至于每種對(duì)應(yīng)的背景顏色,將代碼運(yùn)行起來就可看到。關(guān)于這個(gè)方法的返回值,博主第一次用的時(shí)候也研究了好久,按照bootstrap table的規(guī)則,必須返回一個(gè)json格式的對(duì)象型如: { classes: strclass } 。
三、表格行內(nèi)編輯代碼示例
關(guān)于表格行內(nèi)編輯,需要使用bootstrap table擴(kuò)展的幾個(gè)js文件。
1、引入額外的js文件
<link rel="stylesheet" href="//rawgit.com/vitalets/x-editable/master/dist/bootstrap3-editable/css/bootstrap-editable.css"> <script src="//rawgit.com/vitalets/x-editable/master/dist/bootstrap3-editable/js/bootstrap-editable.js"></script> <script src="~/Content/bootstrap-table/extensions/editable/bootstrap-table-editable.js"></script>
2、在cshtml頁面定義表格時(shí),添加兩個(gè)屬性
<table id="tb_departments"> <thead> <tr> <th data-field="Name" data-editable="true">部門名稱</th> <th data-field="ParentName">上級(jí)部門</th> <th data-field="Level" data-editable="true">部門級(jí)別</th> <th data-field="Desc" data-editable="true">描述</th> </tr> </thead> </table>
如果是在js里面初始化,寫法如下:
{ field: "name", title: "名稱", editable:true }
3、在js里面初始化表格的時(shí)候注冊(cè)編輯保存的事件
$('#tb_departments').bootstrapTable({ url: '/Editable/GetDepartment', //請(qǐng)求后臺(tái)的URL(*) method: 'get', //請(qǐng)求方式(*) toolbar: '#toolbar', //工具按鈕用哪個(gè)容器 striped: true, //是否顯示行間隔色 cache: false, //是否使用緩存,默認(rèn)為true,所以一般情況下需要設(shè)置一下這個(gè)屬性(*) pagination: true, //是否顯示分頁(*) sortable: false, //是否啟用排序 sortOrder: "asc", //排序方式 queryParams: oTableInit.queryParams,//傳遞參數(shù)(*) sidePagination: "server", //分頁方式:client客戶端分頁,server服務(wù)端分頁(*) pageNumber: 1, //初始化加載第一頁,默認(rèn)第一頁 pageSize: 10, //每頁的記錄行數(shù)(*) onEditableSave: function (field, row, oldValue, $el) { $.ajax({ type: "post", url: "/Editable/Edit", data: { strJson: JSON.stringify(row) }, success: function (data, status) { if (status == "success") { alert("編輯成功"); } }, error: function () { alert("Error"); }, complete: function () { } }); } });
重點(diǎn)還是看看這個(gè)事件的處理方法
onEditableSave: function (field, row, oldValue, $el) { $.ajax({ type: "post", url: "/Editable/Edit", data: { strJson: JSON.stringify(row) }, success: function (data, status) { if (status == "success") { alert("編輯成功"); } }, error: function () { alert("Error"); }, complete: function () { } }); }
對(duì)應(yīng)的方法里面需要自己處理保存的邏輯。四個(gè)參數(shù)field, row, oldValue, $el分別對(duì)應(yīng)著當(dāng)前列的名稱、當(dāng)前行數(shù)據(jù)對(duì)象、更新前的值、編輯的當(dāng)前單元格的jQuery對(duì)象。
四、表格行列合并代碼示例
表格的行列合并功能不用引用其他的js文件,只需要在cshtml頁面使用table的colspan和rowspan即可實(shí)現(xiàn)。
1、cshtml頁面
<table id="tb_report"> <thead> <tr> <th colspan="4" data-valign="middle" data-align="center">第一季度</th> <th colspan="4" data-valign="middle" data-align="center">第二季度</th> <th colspan="4" data-valign="middle" data-align="center">第三季度</th> <th colspan="4" data-valign="middle" data-align="center">第四季度</th> <th data-field="TotalCount" rowspan="2" data-valign="middle" data-align="center">年度匯總</th> </tr> <tr> <th data-field="JanCount" data-align="center">一月</th> <th data-field="FebCount" data-align="center">二月</th> <th data-field="MarCount" data-align="center">三月</th> <th data-field="FirstQuarter" data-align="center">第一季度</th> <th data-field="AprCount" data-align="center">四月</th> <th data-field="MayCount" data-align="center">五月</th> <th data-field="JunCount" data-align="center">六月</th> <th data-field="SecondQuarter" data-align="center">第二季度</th> <th data-field="JulCount" data-align="center">七月</th> <th data-field="AguCount" data-align="center">八月</th> <th data-field="SepCount" data-align="center">九月</th> <th data-field="ThirdQuarter" data-align="center">第三季度</th> <th data-field="OctCount" data-align="center">十月</th> <th data-field="NovCount" data-align="center">十一月</th> <th data-field="DecCount" data-align="center">十二月</th> <th data-field="ForthQuarter" data-align="center">第四季度</th> </tr> </thead> </table>
2、js初始化并無特殊
$('#tb_report').bootstrapTable({ url: '/GroupColumns/GetReport', //請(qǐng)求后臺(tái)的URL(*) method: 'get', //請(qǐng)求方式(*) toolbar: '#toolbar', //工具按鈕用哪個(gè)容器 striped: true, //是否顯示行間隔色 cache: false, //是否使用緩存,默認(rèn)為true,所以一般情況下需要設(shè)置一下這個(gè)屬性(*) pagination: true, //是否顯示分頁(*) sortOrder: "asc", //排序方式 queryParams: oTableInit.queryParams,//傳遞參數(shù)(*) sidePagination: "server", //分頁方式:client客戶端分頁,server服務(wù)端分頁(*) pageNumber: 1, //初始化加載第一頁,默認(rèn)第一頁 pageSize: 10, //每頁的記錄行數(shù)(*) pageList: [10, 25, 50, 100], //可供選擇的每頁的行數(shù)(*) });
怎么樣,有沒有很簡(jiǎn)單。當(dāng)然,有人說了,你都可以不用js初始化,直接在cshtml里面用table的屬性去設(shè)置url、分頁等信息。確實(shí),如果我們看過它的api,會(huì)發(fā)現(xiàn)它初始化的每一個(gè)屬性都對(duì)應(yīng)一個(gè)table的屬性。型如
如果你的表格沒有一些特殊的事件需要處理,這樣是完全沒有問題的。
五、表格數(shù)據(jù)導(dǎo)出代碼示例
表格數(shù)據(jù)的導(dǎo)出功能也需要一些擴(kuò)展的js支持。
1、引入額外的js文件
<script src="~/Content/bootstrap-table/extensions/export/bootstrap-table-export.js"></script> <script src="//rawgit.com/hhurz/tableExport.jquery.plugin/master/tableExport.js"></script>
2、js初始化的時(shí)候
$('#tb_departments').bootstrapTable({ url: '/Export/GetDepartment', //請(qǐng)求后臺(tái)的URL(*) method: 'get', //請(qǐng)求方式(*) toolbar: '#toolbar', //工具按鈕用哪個(gè)容器 striped: true, //是否顯示行間隔色 cache: false, //是否使用緩存,默認(rèn)為true,所以一般情況下需要設(shè)置一下這個(gè)屬性(*) pagination: true, //是否顯示分頁(*) sortable: false, //是否啟用排序 sortOrder: "asc", //排序方式 queryParams: oTableInit.queryParams,//傳遞參數(shù)(*) sidePagination: "client", //分頁方式:client客戶端分頁,server服務(wù)端分頁(*) pageNumber: 1, //初始化加載第一頁,默認(rèn)第一頁 pageSize: 10, //每頁的記錄行數(shù)(*) pageList: [10, 25, 50, 100], //可供選擇的每頁的行數(shù)(*) clickToSelect:true, showExport: true, //是否顯示導(dǎo)出 exportDataType: "basic", //basic', 'all', 'selected'. columns: [{ checkbox: true }, { field: 'Name', title: '部門名稱' }, { field: 'ParentName', title: '上級(jí)部門' }, { field: 'Level', title: '部門級(jí)別' }, { field: 'Desc', title: '描述' }, ] });
還是來看重點(diǎn):這兩個(gè)屬性
showExport: true, //是否顯示導(dǎo)出 exportDataType: "basic", //basic', 'all', 'selected'. showExport表示是否顯示導(dǎo)出的按鈕,exportDataType表示導(dǎo)出的模式是當(dāng)前頁、所有數(shù)據(jù)還是選中數(shù)據(jù)。
六、總結(jié)
以上就是功能的效果以及實(shí)現(xiàn)的簡(jiǎn)單代碼。博主發(fā)現(xiàn)有幾個(gè)問題有待解決。
1、行內(nèi)編輯的功能是每一個(gè)單元格提交到后臺(tái),這樣會(huì)造成數(shù)據(jù)庫的頻繁操作,感覺不太合適。不知道有沒有更好的方式,每行提交到后臺(tái)。
2、導(dǎo)出的功能雖然很好用,但是遺憾的是不支持IE瀏覽器,博主試過官網(wǎng)上面的example,好像IE也導(dǎo)出不了。待驗(yàn)證。
以上所述是小編給大家介紹的JS組件系列之Bootstrap table表格組件神器【終結(jié)篇】的相關(guān)內(nèi)容,希望對(duì)大家有所幫助!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

To verify dates in Bootstrap, follow these steps: Introduce the required scripts and styles; initialize the date selector component; set the data-bv-date attribute to enable verification; configure verification rules (such as date formats, error messages, etc.); integrate the Bootstrap verification framework and automatically verify date input when form is submitted.

How to use Bootstrap to get the value of the search bar: Determines the ID or name of the search bar. Use JavaScript to get DOM elements. Gets the value of the element. Perform the required actions.

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

Web development design is a promising career field. However, this industry also faces many challenges. As more businesses and brands turn to the online marketplace, web developers have the opportunity to demonstrate their skills and succeed in their careers. However, as demand for web development continues to grow, the number of developers is also increasing, resulting in increasingly fierce competition. But it’s exciting that if you have the talent and will, you can always find new ways to create unique designs and ideas. As a web developer, you may need to keep looking for new tools and resources. These new tools and resources not only make your job more convenient, but also improve the quality of your work, thus helping you win more business and customers. The trends of web development are constantly changing.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text
