Home > Web Front-end > Bootstrap Tutorial > bootstrap table from scratch

bootstrap table from scratch

angryTom
Release: 2020-05-15 09:19:19
Original
4800 people have browsed it

bootstrap table from scratch

The editor of this article will take you from scratch and tell you step by step how to use the bootstrap Table plug-in to display a table on the front end

First, download the bootstrap Table plug-in Necessary js, address: https://github.com/wenzhixin/bootstrap-table

Official document address: http://bootstrap-table.wenzhixin.net .cn/zh-cn/documentation/

Recommended tutorial: bootstrap tutorial

The editor has circled the points that need attention in this article with a red pen

Show the renderings first

bootstrap table from scratch

Then start using the bootstrap Table plug-in to create Table

Put the plug-in js downloaded from the above address into the project, which are js, css, fonts

bootstrap table from scratch

There is a detail here: The name of the locale folder cannot be modified, and all language js inside must be pasted in. This article uses MVC as an example, of course WebForm is also possible

Usage steps:

1. Create a new controller and view, which references the _Layout master version

2. Reference the corresponding js in the view

bootstrap table from scratch

Write a table table container on the page. This sentence must contain

<table id="ArbetTable"></table>
Copy after login

3. Initialize bootstrap Table
##

$(function () {
     //1.初始化Table
     var oTable = new TableInit();
      oTable.Init();
   });
Copy after login

4. Use bootstrap Table

var TableInit = function () {
    var oTableInit = new Object();
    //初始化Table
    oTableInit.Init = function () {
        $(&#39;#ArbetTable&#39;).bootstrapTable({
            url: &#39;/Interface/GetData&#39;,         //请求后台的URL(*)
            method: &#39;get&#39;,                      //请求方式(*)
            toolbar: &#39;#toolbar&#39;,                //工具按钮用哪个容器
            striped: true,                      //是否显示行间隔色
            cache: false,                       //是否使用缓存,默认为true,所以一般情况下需要设置一下这个属性(*)
            pagination: true,                   //是否显示分页(*)
            sortable: false,                     //是否启用排序
            sortOrder: "asc",                   //排序方式
            queryParams: oTableInit.queryParams,//传递参数(*)
            sidePagination: "server",           //分页方式:client客户端分页,server服务端分页(*)
            pageNumber: 1,                       //初始化加载第一页,默认第一页
            pageSize: 10,                       //每页的记录行数(*)
            pageList: [10, 25, 50, 100],        //可供选择的每页的行数(*)
            search: true,                       //是否显示表格搜索,此搜索是客户端搜索,不会进服务端,所以,个人感觉意义不大
            contentType: "application/x-www-form-urlencoded",
            strictSearch: true,
            showColumns: true,                  //是否显示所有的列
            showRefresh: true,                  //是否显示刷新按钮
            minimumCountColumns: 2,             //最少允许的列数
            clickToSelect: true,                //是否启用点击选中行
            height: 700,                        //行高,如果没有设置height属性,表格自动根据记录条数觉得表格高度
            uniqueId: "no",                     //每一行的唯一标识,一般为主键列
            showToggle: true,                    //是否显示详细视图和列表视图的切换按钮
            cardView: false,                    //是否显示详细视图
            detailView: false,                   //是否显示父子表
            columns: [
            {
                field: &#39;ID&#39;,
                title: &#39;ID&#39;
            }, {
                field: &#39;Name&#39;,
                title: &#39;名字&#39;
            }, {
                field: &#39;Sex&#39;,
                title: &#39;性别&#39;
            },
            {
                field: &#39;operate&#39;,
                title: &#39;操作&#39;,
                formatter: operateFormatter //自定义方法,添加操作按钮
            },
            ],
            rowStyle: function (row, index) {
                var classesArr = [&#39;success&#39;, &#39;info&#39;];
                var strclass = "";
                if (index % 2 === 0) {//偶数行
                    strclass = classesArr[0];
                } else {//奇数行
                    strclass = classesArr[1];
                }
                return { classes: strclass };
            },//隔行变色
        });

    };


    //得到查询的参数
    oTableInit.queryParams = function (params) {
        var temp = {   //这里的键的名字和控制器的变量名必须一直,这边改动,控制器也需要改成一样的
            limit: params.limit,   //页面大小
            offset:params.offset
        };
        return temp;
    };
    return oTableInit;
};


function operateFormatter(value, row, index) {//赋予的参数
    return [
        &#39;<a class="btn active disabled" href="#">编辑</a>&#39;,
        &#39;<a class="btn active" href="#">档案</a>&#39;,
        &#39;<a class="btn btn-default" href="#">记录</a>&#39;,
        &#39;<a class="btn active" href="#">准入</a>&#39;
    ].join(&#39;&#39;);
}
Copy after login

4. Backend url returns data

public ActionResult GetData(int limit, int offset)
        {
            var data = new List<object>(){new { ID=1, Name="Arbet", Sex="男"},
                new { ID= 2, Name="Arbet1", Sex="女" },
                new {ID=3, Name="Arbet2",Sex="男" },
                new {ID=4, Name="Arbet3",Sex="女" },
                new {ID=5, Name="Arbet4",Sex="男" },
                new {ID=6, Name="Arbet5",Sex="男" },
                new {ID=7, Name="Arbet6",Sex="女" },
                new {ID=8, Name="Arbet7",Sex="男" },
                new { ID=9, Name="Arbet1", Sex="女" },
                new {ID=10, Name="Arbet2",Sex="男" },
                new {ID=11, Name="Arbet3",Sex="女" },
                new {ID=12, Name="Arbet4",Sex="男" },
                new {ID=13, Name="Arbet5",Sex="男" },
                new {ID=14, Name="Arbet6",Sex="女" },
                new {ID=15, Name="Arbet7",Sex="男" }
            };
            var total = data.Count;
            var rows = data.Skip(offset).Take(limit).ToList();
            return Json(new { total = total, rows = rows }, JsonRequestBehavior.AllowGet);
        }
Copy after login

In this article, the blogger sets the data with an anonymous collection, and you can obtain the data by querying the database

It should be noted here:

The returned parameters must be total and rows, total returns the total number of data sets, and rows returns the json format of the table

5. Display effect

bootstrap table from scratch

I found this bug, what’s going on

Open the source on the browser Code

bootstrap table from scratch

I found that there are some other js files. This is the js file introduced in the layout master page

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    @RenderBody()

    @*@Scripts.Render("~/bundles/jquery")*@
    @RenderSection("scripts", required: false)
</body>
</html>
Copy after login
Put it in the red box in the picture above Comment out the js file and run

and find it successful! This is because the references to JS library files are in order, bootstrap table from scratch must first reference the JQuery library file, and then reference the plug-in js

The above is the detailed content of bootstrap table from scratch. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template