Bootstrap Table 使用示例及代码

http://issues.wenzhixin.net.cn/bootstrap-table/

<!DOCTYPE html>
<html>
<head>
    <title>Bootstrap Table Examples</title>
    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/bootstrap-table/src/bootstrap-table.css">
    <link rel="stylesheet" href="//rawgit.com/vitalets/x-editable/master/dist/bootstrap3-editable/css/bootstrap-editable.css">
    <link rel="stylesheet" href="assets/examples.css">
    <script src="assets/jquery.min.js"></script>
    <script src="assets/bootstrap/js/bootstrap.min.js"></script>
    <script src="ga.js"></script>
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn‘t work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/json2/20140204/json2.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="container">
    <h1>Bootstrap Table Examples <a href="https://github.com/wenzhixin/bootstrap-table-examples" class="btn btn-primary" role="button" target="_blank">Learn more &raquo;</a></h1>
    <div id="toolbar">
        <button id="remove" class="btn btn-danger" disabled>
            <i class="glyphicon glyphicon-remove"></i> Delete
        </button>
    </div>
    <table id="table"
           data-toolbar="#toolbar"
           data-search="true"
           data-show-refresh="true"
           data-show-toggle="true"
           data-show-columns="true"
           data-show-export="true"
           data-detail-view="true"
           data-detail-formatter="detailFormatter"
           data-minimum-count-columns="2"
           data-show-pagination-switch="true"
           data-pagination="true"
           data-id-field="id"
           data-page-list="[10, 25, 50, 100, ALL]"
           data-show-footer="false"
           data-side-pagination="server"
           data-url="/examples/bootstrap_table/data"
           data-response-handler="responseHandler">
    </table>
</div>

<script>
    var $table = $(‘#table‘),
        $remove = $(‘#remove‘),
        selections = [];

    function initTable() {
        $table.bootstrapTable({
            height: getHeight(),
            columns: [
                [
                    {
                        field: ‘state‘,
                        checkbox: true,
                        rowspan: 2,
                        align: ‘center‘,
                        valign: ‘middle‘
                    }, {
                        title: ‘Item ID‘,
                        field: ‘id‘,
                        rowspan: 2,
                        align: ‘center‘,
                        valign: ‘middle‘,
                        sortable: true,
                        footerFormatter: totalTextFormatter
                    }, {
                        title: ‘Item Detail‘,
                        colspan: 3,
                        align: ‘center‘
                    }
                ],
                [
                    {
                        field: ‘name‘,
                        title: ‘Item Name‘,
                        sortable: true,
                        editable: true,
                        footerFormatter: totalNameFormatter,
                        align: ‘center‘
                    }, {
                        field: ‘price‘,
                        title: ‘Item Price‘,
                        sortable: true,
                        align: ‘center‘,
                        editable: {
                            type: ‘text‘,
                            title: ‘Item Price‘,
                            validate: function (value) {
                                value = $.trim(value);
                                if (!value) {
                                    return ‘This field is required‘;
                                }
                                if (!/^\$/.test(value)) {
                                    return ‘This field needs to start width $.‘
                                }
                                var data = $table.bootstrapTable(‘getData‘),
                                    index = $(this).parents(‘tr‘).data(‘index‘);
                                console.log(data[index]);
                                return ‘‘;
                            }
                        },
                        footerFormatter: totalPriceFormatter
                    }, {
                        field: ‘operate‘,
                        title: ‘Item Operate‘,
                        align: ‘center‘,
                        events: operateEvents,
                        formatter: operateFormatter
                    }
                ]
            ]
        });
        // sometimes footer render error.
        setTimeout(function () {
            $table.bootstrapTable(‘resetView‘);
        }, 200);
        $table.on(‘check.bs.table uncheck.bs.table ‘ +
                ‘check-all.bs.table uncheck-all.bs.table‘, function () {
            $remove.prop(‘disabled‘, !$table.bootstrapTable(‘getSelections‘).length);

            // save your data, here just save the current page
            selections = getIdSelections();
            // push or splice the selections if you want to save all data selections
        });
        $table.on(‘expand-row.bs.table‘, function (e, index, row, $detail) {
            if (index % 2 == 1) {
                $detail.html(‘Loading from ajax request...‘);
                $.get(‘LICENSE‘, function (res) {
                    $detail.html(res.replace(/\n/g, ‘<br>‘));
                });
            }
        });
        $table.on(‘all.bs.table‘, function (e, name, args) {
            console.log(name, args);
        });
        $remove.click(function () {
            var ids = getIdSelections();
            $table.bootstrapTable(‘remove‘, {
                field: ‘id‘,
                values: ids
            });
            $remove.prop(‘disabled‘, true);
        });
        $(window).resize(function () {
            $table.bootstrapTable(‘resetView‘, {
                height: getHeight()
            });
        });
    }

    function getIdSelections() {
        return $.map($table.bootstrapTable(‘getSelections‘), function (row) {
            return row.id
        });
    }

    function responseHandler(res) {
        $.each(res.rows, function (i, row) {
            row.state = $.inArray(row.id, selections) !== -1;
        });
        return res;
    }

    function detailFormatter(index, row) {
        var html = [];
        $.each(row, function (key, value) {
            html.push(‘<p><b>‘ + key + ‘:</b> ‘ + value + ‘</p>‘);
        });
        return html.join(‘‘);
    }

    function operateFormatter(value, row, index) {
        return [
            ‘<a class="like" href="javascript:void(0)" title="Like">‘,
            ‘<i class="glyphicon glyphicon-heart"></i>‘,
            ‘</a>  ‘,
            ‘<a class="remove" href="javascript:void(0)" title="Remove">‘,
            ‘<i class="glyphicon glyphicon-remove"></i>‘,
            ‘</a>‘
        ].join(‘‘);
    }

    window.operateEvents = {
        ‘click .like‘: function (e, value, row, index) {
            alert(‘You click like action, row: ‘ + JSON.stringify(row));
        },
        ‘click .remove‘: function (e, value, row, index) {
            $table.bootstrapTable(‘remove‘, {
                field: ‘id‘,
                values: [row.id]
            });
        }
    };

    function totalTextFormatter(data) {
        return ‘Total‘;
    }

    function totalNameFormatter(data) {
        return data.length;
    }

    function totalPriceFormatter(data) {
        var total = 0;
        $.each(data, function (i, row) {
            total += +(row.price.substring(1));
        });
        return ‘$‘ + total;
    }

    function getHeight() {
        return $(window).height() - $(‘h1‘).outerHeight(true);
    }

    $(function () {
        var scripts = [
                location.search.substring(1) || ‘assets/bootstrap-table/src/bootstrap-table.js‘,
                ‘assets/bootstrap-table/src/extensions/export/bootstrap-table-export.js‘,
                ‘http://rawgit.com/hhurz/tableExport.jquery.plugin/master/tableExport.js‘,
                ‘assets/bootstrap-table/src/extensions/editable/bootstrap-table-editable.js‘,
                ‘http://rawgit.com/vitalets/x-editable/master/dist/bootstrap3-editable/js/bootstrap-editable.js‘
            ],
            eachSeries = function (arr, iterator, callback) {
                callback = callback || function () {};
                if (!arr.length) {
                    return callback();
                }
                var completed = 0;
                var iterate = function () {
                    iterator(arr[completed], function (err) {
                        if (err) {
                            callback(err);
                            callback = function () {};
                        }
                        else {
                            completed += 1;
                            if (completed >= arr.length) {
                                callback(null);
                            }
                            else {
                                iterate();
                            }
                        }
                    });
                };
                iterate();
            };

        eachSeries(scripts, getScript, initTable);
    });

    function getScript(url, callback) {
        var head = document.getElementsByTagName(‘head‘)[0];
        var script = document.createElement(‘script‘);
        script.src = url;

        var done = false;
        // Attach handlers for all browsers
        script.onload = script.onreadystatechange = function() {
            if (!done && (!this.readyState ||
                    this.readyState == ‘loaded‘ || this.readyState == ‘complete‘)) {
                done = true;
                if (callback)
                    callback();

                // Handle memory leak in IE
                script.onload = script.onreadystatechange = null;
            }
        };

        head.appendChild(script);

        // We handle everything using the script element injection
        return undefined;
    }
</script>
</body>
</html>
时间: 2024-12-12 17:27:26

Bootstrap Table 使用示例及代码的相关文章

JS组件系列——表格组件神器:bootstrap table(三:终结篇,最后的干货福利)

前言:前面介绍了两篇关于bootstrap table的基础用法,这章我们继续来看看它比较常用的一些功能,来个终结篇吧,毛爷爷告诉我们做事要有始有终~~bootstrap table这东西要想所有功能覆盖似乎不太现实,博主挑选了一些自认为比较常用的功能在此分享给各位园友.源码也在这篇统一给出.好了,不多说废话,开始我们的干货之旅吧. bootstrap table系列: JS组件系列——表格组件神器:bootstrap table JS组件系列——表格组件神器:bootstrap table(二

JS组件系列——Bootstrap Table 表格行拖拽

原文:JS组件系列--Bootstrap Table 表格行拖拽 前言:之前一直在研究DDD相关知识,好久没更新JS系列文章了.这两天做了一个简单的业务需求,觉得效果还可以,今天在这里分享给大家,欢迎拍砖~~ 一.业务需求及实现效果 项目涉及到订单模块,那天突然接到一个需求,说是两种不同状态的订单之间要实现插单的效果,页面上呈现方式是:左右两个Table,左边Table里面是状态为1的订单,右边Table里面是状态为2订单,左边Table里面的行数据拖动到右边Table里面指定行的位置,拖动完成

JS组件系列——表格组件神器:bootstrap table(二:父子表和行列调序)

原文:JS组件系列--表格组件神器:bootstrap table(二:父子表和行列调序) 前言:上篇 JS组件系列——表格组件神器:bootstrap table 简单介绍了下Bootstrap Table的基础用法,没想到讨论还挺热烈的.有园友在评论中提到了父子表的用法,今天就结合Bootstrap table的父子表和行列调序的用法再来介绍下它稍微高级点的用法. bootstrap table系列: JS组件系列——表格组件神器:bootstrap table JS组件系列——表格组件神器

bootstrap table 服务器端分页--ashx+ajax

1.准备静态页面 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title></title> 6 <meta charset="utf-8" /> 7 <link rel="

Bootstrap Table Fixed Columns

最近在使用BootStrap 做项目前端,自然也用到了  Bootstrap Table. 推荐大家多去这个http://bootstrap-table.wenzhixin.net.cn/zh-cn/  网站看看,上面有很详细的介绍以及其他扩展功能 下面写下 Fixed Columns(固定列)的使用方法.附件下载地址:http://pan.baidu.com/s/1kUEQTPt 1.引用css文件,js文件(注意引用顺序) <link rel="stylesheet" hre

新的表格展示利器 Bootstrap Table Ⅱ

    上一篇文章介绍了Bootstrap Table的基本知识点和应用,本文针对上一篇文章中未解决的文件导出问题进行分析,同时介绍BootStrap Table的扩展功能,当行表格数据修改. 1.Bootstrap Bable 全部数据导出分析 在表格导出数据中,发现设置了分页参数,导出的数据仅为表格加载的分页参数数据,于是,针对这样的情况,通过设置分页参数的值,使表格可以加载更多的数据,可达到导出所有数据的功能需求.然而,在实际的实验中,发现此方案存在以下问题: 表格一次加载一千条数据时,网

Bootstrap Table使用分享

版权声明:本文为博主原创文章,未经博主允许不得转载. 最近客户提出需求,想将原有的管理系统,做下优化,通过手机也能很好展现,想到2个方案: a方案:保留原有的页面,新设计一套适合手机的页面,当手机访问时,进入m.zhy.com(手机页面),pc设备访问时,进入www.zhy.com(pc页面) b方案:采用bootstrap框架,替换原有页面,自动适应手机.平板.PC 设备 采用a方案,需要设计一套界面,并且要得重新写适合页面的接口,考虑到时间及成本问题,故项目采用了b方案 一.效果展示 二.B

bootstrap table 实现固定悬浮table 表头并可以水平滚动

在开发项目中,需要将表格头部固定,而且表格大多数情况下是会水平滚动的.项目的css框架是bootstrap 3,故也可以叫做bootstrap table. 需要实现的是:表格头部固定,并且支持水平滚动 html code(source table): <table id="top_fix_table" border="0" cellpadding="4" cellspacing="0" class="tabl

JS表格组件神器bootstrap table详解(基础版)

这篇文章主要介绍了JS表格组件神器bootstrap table,bootstrap table界面采用扁平化的风格,用户体验比较好,更好兼容各种客户端,需要了解更多bootstrap table的朋友可以参考下 一.Bootstrap Table的引入 关于Bootstrap Table的引入,一般来说还是两种方法: 1.直接下载源码,添加到项目里面来.由于Bootstrap Table是Bootstrap的一个组件,所以它是依赖Bootstrap的,我们首先需要添加Bootstrap的引用.