How to use jquery datatable server-side in python flask

Asked 2 years ago, Updated 2 years ago, 90 views

It is difficult to load the data table from Python flask to jquery because there are more than 30,000 data Find and apply server-side processing

Currently, in my code, the paging and search functions do not work properly after spraying only 10 data as fixed. Where should I fix it?

        <table id="data_table" class="data_table table table-hover">
                <thead>
                    <tr>
                        <th>Tag</th><th>EN</th><th>CN</th><th>KO</th><th>Edit</th>
                    </tr>
                </thead>
                <tbody id="data_table_tbody">
                </tbody>
        </table>
    $(function(){
        $('#data_table').DataTable( {
            "bprocessing" : true,
            "bServerSide" : true,
            "sAjaxSource" :"/admin_en/datatable",
            "sServerMethod": "POST",
            "columns" : [
                { { "data": "Tag" },
                { { "data": "EN" },
                { { "data": "CN" },
                { { "data": "KO" },
                { { "data": "Edit" },
            ],
            "dom" : 'lfBrtip',
            "buttons":[
                'csv'
            ]
        } );
    });//end function
@app.route("/admin_en/datatable", methods=['POST'])
def admin_en_datatable():
    en_filter_table = db.session.query(En_filter)
    datatable_data_dict = dict()
    datatable_data_list = list()
    for data in en_filter_table:
        filter_dict = dict()
        id = data.id
        filter_dict['id'] = id
        filter_dict['Tag'] = data.tag
        filter_dict['EN'] = data.en_filter
        filter_dict['CN'] = data.cn_filter
        filter_dict['KO'] = data.ko_filter
        filter_dict['Edit'] = "<button>btn</button>"
        datatable_data_list.append(filter_dict)
    datatable_data_dict['draw'] = 1
    datatable_data_dict['recordsTotal'] = int(en_filter_table.count())
    datatable_data_dict['recordsFiltered'] = int(en_filter_table.count())

    datatable_data_dict['data'] = datatable_data_list[:10]

    return jsonify(datatable_data_dict)

python flask jquery datatables

2022-09-21 11:10

1 Answers

Dataables server-side rendering means that the specified /admin_en/dataable route returns JSON in the following format:

{
    "draw": "1",
    "recordsTotal": 1861,
    "recordsFiltered": 32,
    "data": [
        {
            "column1": "data1",
            ...,
        }
    ]
}

draw can be returned to the requested value, and the problem is recordTotal and recordsFiltered. So, in my case, I'm dealing with it like this.

It seems that the source you gave us is to query En_filter once and get all the results, and then cut it with restrictions. Then, of course, it's slow and it won't work the way you want it to. Take a good look at the official document, and actually study the configuration of the parameters that dataable.js sends requests to that route.


2022-09-21 11:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.