服务器端分页的bootstrap-table无法正常工作

时间:2015-03-19 19:19:45

标签: twitter-bootstrap flask pagination server-side bootstrap-table

我需要通过Flask网络应用程序将大型数据集提供到网络上的表格中。我正在尝试使用bootstrap-table extension实现服务器端分页,但我无法使其正常工作。关于它的一些事情,我不理解。当表呈现时,它正确地知道我的表中的行数,并构建适当的页面列表。但是,表中的所有行都会在每个页面上呈现。

此外,此表的排序和搜索功能仅返回完整表。 bootstrap-table.js代码似乎具有用于对服务器端数据进行排序和搜索的功能,但我不确定。

我的引导程序表代码如下所示

<table class='sastable' id='servertable' data-toggle="table" data-classes='table table-condensed table-bordered'
   data-url="/manga/gettable"
   data-show-columns='true' data-toolbar='#toolbar' data-id-field='id'
   data-pagination="true" data-side-pagination="server"
   data-page-list="[10, 20, 50, 100]" data-search="true">

<thead>
    <tr id='head'>
        <th data-field="state" data-checkbox="true">ID</th>
        <th data-field="id" id='id' data-visible='false' data-switchable='false'>ID</th>
        {% for column in keys %}
        <th id='{{column}}' data-field='{{column}}' data-sortable='true' data-sorter="sort"
            data-cell-style="{{'cellStyle' if (column=='plate' or 'status' in column or 'comp' in column) else ''}}"
            data-visible="{{'false' if column not in cols else 'true'}}">{{column|upper}}</th>
        {% endfor %}        
    </tr>           
</thead>    

其中我的data-url属性'/ manga / gettable'是以这种格式返回JSON数据的链接,jsfiddle

在/ manga / gettable中传递数据的代码是

@tables_page.route('/manga/gettable', methods=['GET','POST'])
@tables_page.route('/gettable', methods=['GET','POST'])
def getTable():
''' Retrieve tables for server-side delivery '''


pl = plateList()
pl.load()
table = pl.plate2d    
size = len(table)

data = OrderedDict()
data['total'] = size
data['rows'] = []
cols = table.columns.keys()

for row in table:
    data['rows'].append({col:row.data[i] for i,col in enumerate(cols)})

return jsonify(data)

基本上只是从文件中加载一个表,并将其转换为JSON字典类型格式。这个表有大约50行,并正确地制作5页,假设每页默认10条记录,但实际上每页显示50行。

一个可信的工作示例,正确的分页,排序和搜索是here,但我不明白它是如何工作的。

我觉得我在这里缺少一些基本的东西。我在这里错过了什么?谢谢。

2 个答案:

答案 0 :(得分:1)

我在django上解决了这个问题:

我的js:

$(document).ready(function(){
    ...
    ajaxGet();
});

function ajaxGet(){
    var table_params = $('#docs_table').bootstrapTable({
        url: 'ajax/test_1/data?',
        queryParams: function (p) {
            return {
                limit: p.limit,
                offset: p.offset,
                sort: p.sort,
                order: p.order,
                'data[]': [selected_data],//for multi-select filter

            };
        }
    });
    $('#docs_table').bootstrapTable('refresh');
}

吡啶:

def test_1(request):

        q = m.Doc.objects.all()

        data = request.GET.get('data[]')


        if data:
            data = data.split(',')
            q = q.filter(id__in=pis)

        paginator = Paginator(q, 10)

        sort = request.GET.get('sort', 'id')
        order = request.GET.get('order', 'asc')
        limit = int(request.GET.get('limit'))
        offset = int(request.GET.get('offset'))

        if order == 'asc':
            q = q.order_by(sort)
        else:
            q = q.order_by('-' + sort)

        paginator = Paginator(q, limit)
        page = int(offset / limit) + 1

        try:
            docs = paginator.page(page)
        except PageNotAnInteger:
            docs = paginator.page(1)
        except EmptyPage:
            docs = paginator.page(paginator.num_pages)

        docs_dict = {
            'total': paginator.count,
            'rows': [{'id': doc.id,
                      'name': doc.name,
                      } for doc in docs]
        }

        return JsonResponse(docs_dict)

答案 1 :(得分:0)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> <nav id="#custom-bootstrap-menu" class="navbar navbar-default navbar-fixed-top" role="navigation"> <div class="container"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-menubuilder"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#"> Krishh Kidss Zone</a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse navbar-menubuilder"> <ul class="nav navbar-nav navbar-right"> <li><a href="about.html">About Us </a> </li> <li><a href="whykrishkids.html">Why Krishh Kidss</a> </li> <li><a href="admission.html">Admissions</a> </li> <li><a href="events.html">Events</a> </li> <li><a href="gallery.html">Gallery</a> </li> <li><a href="achivements.html">Achivements</a> </li> <li><a href="contact.html">Contact Us</a> </li> </ul> </div> <!-- /.navbar-collapse --> </div> <!-- /.container --> </nav> <!-- Full Page Image Background Carousel Header --> <header id="myCarousel" class="carousel fade"> <!-- Indicators --> <ol class="carousel-indicators"> <li data-target="#myCarousel" data-slide-to="0" class="active"></li> <li data-target="#myCarousel" data-slide-to="1"></li> <li data-target="#myCarousel" data-slide-to="2"></li> </ol> <!-- Wrapper for Slides --> <div class="carousel-inner"> <div class="item active"> <!-- Set the first background image using inline CSS below. --> <div class="fill" style="background-image:url('http://desktop-pictorials.com/SingleScreen/SinglePage01/Island002-1920x1080.jpg');"></div> </div> <div class="item"> <!-- Set the second background image using inline CSS below. --> <div class="fill" style="background-image:url('http://img.phombo.com/img1/photocombo/4448/The_Best_HD_HQ_Hi-Res_Wallpapers_Collection_-_Cityscape_by_tonyx__145_pictures-61.jpg_HDTV_monaco_1920x1080.jpg');"></div> </div> <div class="item"> <!-- Set the third background image using inline CSS below. --> <div class="fill" style="background-image:url('https://interfacelift.com/wallpaper/7yz4ma1/01407_harboursunset_1920x1080.jpg');"></div> </div> </div> <!-- Controls --> <a class="left carousel-control" href="#myCarousel" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left"></span> </a> <a class="right carousel-control" href="#myCarousel" data-slide="next"> <span class="glyphicon glyphicon-chevron-right"></span> </a> </header> <div class="container"> <!-- Marketing Icons Section --> <div class="row"> <div class="col-lg-12"> <h1 class="page-header"> Krishh Kidss Zone </h1> </div> <div class="col-md-4"> <div class="panel panel-default"> <div class="panel-heading"> <h4>Vision</h4> </div> <div class="panel-body"> <p>"To make your child grow into complete personality and develop the strength to meet the challenges ahed." <br/> <br/> <br/> <br/> </p> <div class="text-center"> <a href="vision.html" class="btn btn-default">Read More</a> </div> </div> </div> </div> <div class="col-md-4"> <div class="panel panel-default"> <div class="panel-heading"> <h4>About Us</h4> </div> <div class="panel-body"> <p>We have started Krishh Kidss Zone pre school in 11th February 2010 with motto to give best primary education to children with out any burden. We adopt the simple and effective slogan “Where learning is Fun” , where we always focus on the children’s learning with the help of fun.</p> <div class="text-center"> <a href="about.html" class="btn btn-default">Read More</a> </div> </div> </div> </div> <div class="col-md-4"> <div class="panel panel-default"> <div class="panel-heading"> <h4>Events</h4> </div> <div class="panel-body"> <p>We celebrate almost each and every festivals and days as a part of our curriculam. That will help the children to know and understand the impotance of the festivals and days. We try to fill the colours in education by celebrating such events. <br/> <br/> </p> <div class="text-center"> <a href="events.html" class="btn btn-default">See More</a> </div> </div> </div> </div> </div> <!-- Features Section --> <!-- /.row --> <hr> <!-- Call to Action Section --> <div class="well"> <div class="row"> <div class="col-md-8"> <h3> Contact Us:</h3> <p>Adress: <br/>18 Sarita Vihar, <br/>Opp. Kartavya Bunglows, <br/>Anand Nadiad Road, <br/>Lambhvel, 387-310 <br/>Anand, Gujarat</p> <br/> </div> <div class="col-md-4"> <p>Contact Details <br/>Ph.No. +91 99799 64200 <br/>Like Us On Facebook <ul class="list-unstyled list-inline list-social-icons"> <li> <a href="https://www.facebook.com/Krishkidszone?fref=ts"><i class="fa fa-facebook-square fa-2x"></i></a> </li> </ul> </p> <p>Copyright &copy; Krishh Kidss Zone 2015</p> <p>Developed By: Shree InfoTech Ltd.</p> </div> </div> </div> <hr> <!-- Footer --> </div> <!-- /.container -->表示您需要在服务器端实现分页。

您的服务器代码缺少处理页码,每页行数,订单等的内容。服务器应该只返回该页面的行。

查看官方示例,并在更改页面时观察ajax返回值。 http://wenzhixin.net.cn/p/bootstrap-table/docs/examples.html#server-side-pagination-table

这是请求&amp;返回第4页,10行。

请求网址:
server-side pagination

返回值:

http://wenzhixin.net.cn/examples/bootstrap_table/data?limit=10&offset=30&order=asc