Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

313
Views
Bootstrap multi row selection from table data and post to Flask backend

What I want to do is to let my users select multiple rows in a table and them hit a button to send this data back. My JS script file looks has the following:

<script>
$(document).ready(function () {
// Setup - add a text input to each footer cell
$('#example tfoot th').each(function () {
    var title = $(this).text();
    $(this).html('<input type="text" placeholder="Search ' + title + '" />');
});

$('#example tbody').on('click', 'tr', function () {
    $(this).toggleClass('selected');
});

$('#button').click(function () {
    alert(table.rows('.selected').data().length + ' row(s) selected');
});
// DataTable
var table = $('#example').DataTable({
    initComplete: function () {
        // Apply the search
        this.api()
            .columns()
            .every(function () {
                var that = this;

                $('input', this.footer()).on('keyup change clear', function () {
                    if (that.search() !== this.value) {
                        that.search(this.value).draw();
                    }
                });
            });
    },
});
});
</script>

My HTML code is as follows:

<form action="/get1" method="POST" name = 'input'>
<button id="button">Row count</button>
<table id="example" class="table table-striped">
    <thead>
        <tr>
            <th scope="col">Index</th>
            <th scope="col">Col2</th>
            <th scope="col">Col3</th>
            <th scope="col">Col4</th>
        </tr>
    </thead>
    <tbody>
     {% for item in rows %}
            <tr>
                <td>{{item[0]}}</td>
                <td>{{ item[1] }}</td>
                <td>{{ item[2] }}</td>
                <td>{{ item[3] }}</td>
            </tr>
        {% endfor %}
    </tbody>
<tfoot>
<tr>
                <th>Index</th>
                <th>Col2</th>
                <th>Col3</th>
                <th>Col4</th>
            </tr>
</tfoot>
  </table>
</form>

I need some help with JS and HTML to POST values (such as row index) to the backend that the backend can read.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Let's assume you have added the following DataTables Select resources to the head of your web page:

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/select/1.4.0/css/select.dataTables.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/select/1.4.0/js/dataTables.select.js"></script>

Then you can enable the extension by adding the following option to your DataTable:

select: true

Now you can experiment with selecting and de-selecting rows. You can use the CTRL and SHIFT keys to select multiple rows.

In your button click event you can now use the selected() function I mentioned in my comment - for example:

$('#button').click(function () {
  selected_ids = [];
  table.rows().every(function () {
    if ( this.selected() ) {
      selected_ids.push( this.data()[0] );
    }
  });
  console.log( selected_ids );
});

This will capture an array of IDs from the selected rows.

Note how it uses very similar logic to the columns().every() function you already have - except it uses rows().every() to iterate over the rows in the DataTable - not only the visible rows in the current page, but also all rows in other DataTables pages (if there multiple pages of table data).


The next step is a completely different question: How to submit the IDs in that array to your Flask back end.

That has been covered elsewhere in various questions - for example: jQuery Ajax POST example with PHP. Yes, this mentions PHP, but the jQuery piece is independent of the server technology.

That question has 25 answers, and there are various other questions along similar lines, which should help you. If you get stuck you can ask a new question with the specific problem you are facing.

But, basically, instead of my console.log( selected_ids );, you would use a jquery Ajax call: $.ajax( { ... } ); to send the data to Flask.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!