search
HomeWeb Front-endBootstrap Tutorialbootstrap table from scratch

bootstrap table from scratch

Aug 20, 2019 pm 02:12 PM

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>

3. Initialize bootstrap Table
##

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

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;);
}

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);
        }

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>

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!

Statement
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
What is Bootstrap? An Introduction for BeginnersWhat is Bootstrap? An Introduction for BeginnersApr 22, 2025 am 12:07 AM

BootstrapisafreeCSSframeworkthatsimplifieswebdevelopmentbyprovidingpre-styledcomponentsandJavaScriptplugins.It'sidealforcreatingresponsive,mobile-firstwebsites,offeringaflexiblegridsystemforlayoutsandasupportivecommunityforlearningandcustomization.

Bootstrap Demystified: A Simple ExplanationBootstrap Demystified: A Simple ExplanationApr 21, 2025 am 12:13 AM

Bootstrapisafree,open-sourceCSSframeworkthathelpscreateresponsive,mobile-firstwebsites.1)Itoffersagridsystemforlayoutflexibility,2)includespre-styledcomponentsforquickdesign,and3)ishighlycustomizabletoavoidgenericlooks,butrequiresunderstandingCSStoop

Bootstrap vs. React: Choosing the Right ApproachBootstrap vs. React: Choosing the Right ApproachApr 20, 2025 am 12:09 AM

Bootstrap is suitable for quick construction and small projects, while React is suitable for complex and interactive applications. 1) Bootstrap provides predefined CSS and JavaScript components to simplify responsive interface development. 2) React improves performance and interactivity through component development and virtual DOM.

Bootstrap's Purpose: Building Consistent and Attractive WebsitesBootstrap's Purpose: Building Consistent and Attractive WebsitesApr 19, 2025 am 12:07 AM

The main purpose of Bootstrap is to help developers quickly build responsive, mobile-first websites. Its core functions include: 1. Responsive design, which realizes layout adjustments of different devices through a grid system; 2. Predefined components, such as navigation bars and modal boxes, ensure aesthetics and cross-browser compatibility; 3. Support customization and extensions, and use Sass variables and mixins to adjust styles.

Bootstrap vs. Other Frameworks: A Comparative OverviewBootstrap vs. Other Frameworks: A Comparative OverviewApr 18, 2025 am 12:06 AM

Bootstrap is better than TailwindCSS, Foundation, and Bulma because it is easy to use and quickly develop responsive websites. 1.Bootstrap provides a rich library of predefined styles and components. 2. Its CSS and JavaScript libraries support responsive design and interactive functions. 3. Suitable for rapid development, but custom styles may be more complicated.

Integrating Bootstrap Styles in React: Methods and TechniquesIntegrating Bootstrap Styles in React: Methods and TechniquesApr 17, 2025 am 12:04 AM

Integrating Bootstrap in React projects can be done in two ways: 1) introduced using CDN, suitable for small projects or rapid prototyping; 2) installation using npm package manager, suitable for scenarios that require deep customization. With these methods, you can quickly build beautiful and responsive user interfaces in React.

Bootstrap in React: Advantages and Best PracticesBootstrap in React: Advantages and Best PracticesApr 16, 2025 am 12:17 AM

Advantages of integrating Bootstrap into React projects include: 1) rapid development, 2) consistency and maintainability, and 3) responsive design. By directly introducing CSS files or using the React-Bootstrap library, you can use Bootstrap's components and styles efficiently in your React project.

Bootstrap: A Quick Guide to Web FrameworksBootstrap: A Quick Guide to Web FrameworksApr 15, 2025 am 12:10 AM

Bootstrap is a framework developed by Twitter to help quickly build responsive, mobile-first websites and applications. 1. Ease of use and rich component libraries make development faster. 2. The huge community provides support and solutions. 3. Introduce and use class names to control styles through CDN, such as creating responsive grids. 4. Customizable styles and extension components. 5. Advantages include rapid development and responsive design, while disadvantages are style consistency and learning curve.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools