0

I am trying to use SQLAlchemy-DataTables to build a table with server-side processing that has a column with multi-select checkbox capability.

Here is my flask python code:

@app.route('/myiocs', methods=['GET'])
@login_required
def myiocs():
    title = "My IOCs"
    return render_template('myiocs.html', **locals())

@app.route('/myiocsdata', methods=['GET'])
@login_required
def myiocsdata():
    """Return server side data."""

    # defining columns
    columns = [
        ColumnDT(sparkDB.ioc),
        ColumnDT(sparkDB.ioc_type),
        ColumnDT(sparkDB.active_mon),
        ColumnDT(sparkDB.lifecycle_mon),
        ColumnDT(sparkDB.added_by),
        ColumnDT(sparkDB.tags),
    ]
    query = sparkDB.query.filter_by(organization=current_user.organization).order_by(sparkDB.added_on)
    params = request.args.to_dict()
    rowTable = DataTables(params, query, columns)

    return jsonify(convertToUTF8(rowTable.output_result()))

Here is my table structure in the html:

            <div class="row-fluid">
                <form id="myiocs-frm" action="/myiocs" method="POST">
                    <table id="sparkdb" class="table table-striped table-bordered checkboxes-select" cellspacing="0" width="100%">
                        <thead>
                            <tr>
                                <th></th>
                                <th>IOC</th>
                                <th>IOC Type</th>
                                <th>Active Mon</th>
                                <th>Lifecycle Mon</th>
                                <th>Added By</th>
                                <th>Tags</th>
                            </tr>
                        </thead>
                    </table>
                </form>
            </div>

Here is my javascript that should help render the datatable:

    $(document).ready(function() {
        var table = $('#sparkdb').DataTable({
            "processing": true,
            "serverSide": true, 
            "ajax": "{{ url_for('myiocsdata') }}",
            'columnDefs': [ 
                {               
                    'targets': 0,
                    'checkboxes': {
                        'selectRow': true
                    }           
                }               
            ],              
            'select': { 
                    'style': 'multi'
                },          
            'order': [[1, 'asc']]
        });                         
       $('#myiocs-frm').on('submit', function(e){
          var form = this;  

          var rows_selected = table.column(0).checkboxes.selected();

          // Iterate over all selected checkboxes
          $.each(rows_selected, function(index, rowId){
             // Create a hidden element 
             $(form).append(
                 $('<input>')
                    .attr('type', 'hidden')
                    .attr('name', 'id[]')
                    .val(rowId)
             );
            });
        });
    });

When I attempt to run the code I get back the following error as an alert from the datatables js library:

DataTables warning: table id=sparkdb - Requested unknown parameter '6' for row 0, column 6. For more information about this error, please see http://datatables.net/tn/4

After dismissing the error, the table displays, however there is no checkbox and the columns are misaligned. Any help pointing me in the right direction would be greatly appreciated!

1 Answer 1

1

You have 7 columns but are returning only 6. The error:

Requested unknown parameter '6' for row 0, column 6

Is basically saying that there is no data for column 7. You will need to return a blank for your checkbox column. Something like this:

columns = [
    "",
    ColumnDT(sparkDB.ioc),
    ColumnDT(sparkDB.ioc_type),
    ColumnDT(sparkDB.active_mon),
    ColumnDT(sparkDB.lifecycle_mon),
    ColumnDT(sparkDB.added_by),
    ColumnDT(sparkDB.tags),
]

The error is stopping the Datatables init process causing the checkboxes to not show. It looks like you are using Gyrocode Checkboxes plugin. I assume you are loading the include files for the plugin: https://www.gyrocode.com/projects/jquery-datatables-checkboxes/

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the response! I added in the blank in my flask app and I am receiving a different error now. DataTables warning: table id=sparkdb - 'str' object has no attribute 'sqla_expr' I think you're right about defining a value in the columns list variable, but I'm not sure it likes the empty string.
I'm not sure what your Flask function is doing or what is returned in rowTable = DataTables(params, query, columns) - arrays or objects?. Initially I thought ColumnDT was used to build the column definition for Datatables but maybe not. If you are returning an array of objects then use columns.data to define your columns and remove the "" I suggested above. Using objects will allow you to define the checkbox column without having to return data for it.
Also note the known limitations of the jQuery Datatables Checkboxes library: Column containing checkboxes must have unique data. Using columns.data option set to null for the column containing checkboxes will result in unexpected behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.