0

now i got some error when i try to get data and display on the datatables which using serverSide.

following to my code below i have get my data by using ajax POST method because i want to send some data that need to use to filter in my database, and this is my code.

Javascript

report_table = $('#report_table').DataTable({
            "scrollX": true,
            "serverSide":true,
            "deferRender": true,
            "ajax": {
                "url": "load_report_table",
                "type": "POST",
                "data":{
                    'region_id': function() { return $('#select_reg').val() },
                }
            },
});

Python

from datatables import ColumnDT, DataTables
from flask import request
@app.route('/load_report_table', methods=['POST', 'GET'])
def load_report_table(): 
    reg_id = request.form['region_id']
    columns = [
        ColumnDT(Battery_Log.time),
        ColumnDT(Battery.serial_number),
        ColumnDT(Battery_Log.usage),

    ]
    query = db.session.query().select_from(Battery, Battery_Log)\
            .filter(Battery.reg_id == reg_id, Battery_Log.battery_id == Battery.id)

    params = request.args.to_dict()
    rowTable = DataTables(params, query, columns)

    return rowTable.output_result()

but when i print rowTable.output_result() it got the error like this.

{'draw': '1', 'recordsTotal': '14733', 'recordsFiltered': '14733', 'error': "int() argument must be a string, a bytes-like object or a number, not 'NoneType'"}

For now i guess the error that cause by using POST method because when i have change my code to call only url and not post any data to the route function it work normally, the code is show below.

report_table = $('#report_table').DataTable({
            "scrollX": true,
            "serverSide":true,
            "deferRender": true,
            "ajax": "load_report_table"
});

So are there anyway to fix that i can POST my data to my route function and return the data to show on the datatables.

Kaow
  • 483
  • 2
  • 9
  • 22
  • 1
    Your approach (using a function for the `data` option) is correct - but the syntax is incorrect. You can see a couple of examples [here](https://stackoverflow.com/questions/66243388/how-to-reload-datatable-by-using-new-url-and-some-parameters-without-reinitializ/66244738#66244738). Everything needs to be placed inside the function (including `'region_id'`). Also, bear in mind the warning in the same link about not overwriting `serverSide` response data. – andrewJames May 08 '21 at 13:22
  • @andrewjames Thank for response, i have try with this `"data":function () { return {'region_id': function() { return $('#select_reg').val() }};` and this `"data":function (d) { return $.extend( {}, d,{'region_id': function() { return $('#select_reg').val() }});` but it still got the same error T-T – Kaow May 08 '21 at 16:18
  • Right - because those are not valid syntaxes, either. You have a function inside your JavaScript object `{ }`. You don't need that. You just need the value of `$('#select_reg').val()`. Why wrap that in a function? – andrewJames May 08 '21 at 16:35
  • @andrewjames Because i want it to be current value when i reload the datatable and now i have change it to `$('#select_reg').val()` or even i set it to static value like 1 but it still not working, for now i wonder may be `sqlalchemy-datatables`(Backend) that not working with POST method because i also try with not send any data to flask but it still not working anyway. – Kaow May 09 '21 at 08:16
  • Sorry if my point was not clear. You only need the one function `"data": function () { ... }`. Inside that function you can write whatever (valid!) JavaScript you need to build the `{ ... }` object you need. You do not need a _second_ function to do that. Because you are using a function for your `data` option, it will be re-evaluated every time you re-execute the DataTable's ajax call (for example by using [`ajax.reload()`](https://datatables.net/reference/api/ajax.reload()). – andrewJames May 09 '21 at 12:46
  • Please provide a [mre] - for example, as a JS Fiddle. – andrewJames May 09 '21 at 12:47

0 Answers0