balderman
balderman

Reputation: 23815

Adding event handlers to input fields inside bootstrap table

I am using bootstrap table.

My table contains cells that contains input fields.

I try to add event handlers (using jquery) to those input fields but it does not seem to work.

If I add the event handlers to a non bootstrap table it works.

The code below demonstrates the issue. When the user change input fields in the upper table (a bootstrap table), nothing is written to the console.When user change input fields in the lower table (non bootstrap table), a message is written to the console.

How do I add event handlers to the input fields in a bootstrap-table?

$(document).ready(function() {
  console.log('ready');
  $('#my_table_1').find('input[type="date"]').change(function() {
    console.log('Table 1.Date was changed. Need to check if table is sorted by column C.If so - call the table sort.');
  });
  $('#my_table_1').find('select').change(function() {
    console.log('Table 1.Selection was changed. Need to check if table is sorted by column B.If so - call the table sort.');
  });
  $('#my_table_2').find('input[type="date"]').change(function() {
    console.log('Table 2.Date was changed. Need to check if table is sorted by column C.If so - call the table sort.');
  });
  $('#my_table_2').find('select').change(function() {
    console.log('Table 2.Selection was changed. Need to check if table is sorted by column B.If so - call the table sort.');
  });

});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/bootstrap-table.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<span>A Bootstrap table</span>
<table id="my_table_1" data-toggle="table" data-sort-stable="true">
  <thead>
    <tr>
      <th data-sortable="true">A</th>
      <th data-sortable="true">B</th>
      <th data-sortable="true">C</th>
      <th data-sortable="false">D</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>
        <select>
          <option val="112">A</option>
          <option val="2">B</option>
          <option val="356" selected>C</option>
        </select>
      </td>
      <td><input type="date" value="2018-07-22"></td>
      <td><input type="checkbox"></td>
    </tr>
    <tr>
      <td>123</td>
      <td>
        <select>
          <option val="1" selected>A</option>
          <option val="2">B</option>
          <option val="3">C</option>
        </select>
      </td>
      <td><input type="date" value="2014-07-22"></td>
      <td><input type="checkbox"></td>
    </tr>

    <tr>
      <td>56</td>
      <td>
        <select>
          <option val="1">A</option>
          <option val="2" selected>B</option>
          <option val="3">C</option>

        </select>
      </td>
      <td><input type="date" value="2014-08-23"></td>
      <td><input type="checkbox"></td>
    </tr>

    <tr>
      <td>14</td>
      <td>
        <select>
          <option val="1">A</option>
          <option val="2" selected>B</option>
          <option val="3">C</option>

        </select>
      </td>
      <td><input type="date" value="2014-07-23"></td>
      <td><input type="checkbox"></td>
    </tr>
  </tbody>
</table>
<!------------------------------------------------------------------->
<hr>
<span>A Non Bootstrap table</span>

<table id="my_table_2">
  <thead>
    <tr>
      <th data-sortable="true">A</th>
      <th data-sortable="true">B</th>
      <th data-sortable="true">C</th>
      <th data-sortable="false">D</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>
        <select>
          <option val="112">A</option>
          <option val="2">B</option>
          <option val="356" selected>C</option>
        </select>
      </td>
      <td><input type="date" value="2018-07-22"></td>
      <td><input type="checkbox"></td>
    </tr>
    <tr>
      <td>123</td>
      <td>
        <select>
          <option val="1" selected>A</option>
          <option val="2">B</option>
          <option val="3">C</option>
        </select>
      </td>
      <td><input type="date" value="2014-07-22"></td>
      <td><input type="checkbox"></td>
    </tr>

    <tr>
      <td>56</td>
      <td>
        <select>
          <option val="1">A</option>
          <option val="2" selected>B</option>
          <option val="3">C</option>

        </select>
      </td>
      <td><input type="date" value="2014-08-23"></td>
      <td><input type="checkbox"></td>
    </tr>

    <tr>
      <td>14</td>
      <td>
        <select>
          <option val="1">A</option>
          <option val="2" selected>B</option>
          <option val="3">C</option>

        </select>
      </td>
      <td><input type="date" value="2014-07-23"></td>
      <td><input type="checkbox"></td>
    </tr>
  </tbody>
</table>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/bootstrap-table.min.js"></script>

Upvotes: 0

Views: 1387

Answers (1)

RamKumar
RamKumar

Reputation: 126

u need to add your script at the bottom and not in top of the table html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
    <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/bootstrap-table.min.css">
    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

    <title>sorted table</title>
</head>
<body>

<span>A Bootstrap table</span>

<table id="my_table_1" data-toggle="table" data-sort-stable="true">
    <thead>
    <tr>
        <th data-sortable="true">A</th>
        <th data-sortable="true">B</th>
        <th data-sortable="true">C</th>
        <th data-sortable="false">D</th>
    </tr>
    </thead>
   <tbody>
        <tr>
            <td>1</td>
            <td><select>
                <option val="112">A</option>
                <option val="2">B</option>
                <option val="356" selected>C</option>
            </select>
            </td>
            <td><input type="date" value="2018-07-22"></td>
            <td><input type="checkbox"></td>
        </tr>
        <tr>
            <td>123</td>
            <td><select>
                <option val="1" selected>A</option>
                <option val="2">B</option>
                <option val="3">C</option>
            </select>
            </td>
            <td><input type="date" value="2014-07-22"></td>
            <td><input type="checkbox"></td>
        </tr>

        <tr>
            <td>56</td>
            <td><select>
                <option val="1">A</option>
                <option val="2" selected>B</option>
                <option val="3">C</option>

            </select>
            </td>
            <td><input type="date" value="2014-08-23"></td>
            <td><input type="checkbox"></td>
        </tr>

        <tr>
            <td>14</td>
            <td><select>
                <option val="1">A</option>
                <option val="2" selected>B</option>
                <option val="3">C</option>

            </select>
            </td>
            <td><input type="date" value="2014-07-23"></td>
            <td><input type="checkbox"></td>
        </tr>
        </tbody>
</table>
<!------------------------------------------------------------------->
<hr>
<span>A Non Bootstrap table</span>

<table id="my_table_2">
    <thead>
    <tr>
        <th data-sortable="true">A</th>
        <th data-sortable="true">B</th>
        <th data-sortable="true">C</th>
        <th data-sortable="false">D</th>
    </tr>
    </thead>
   <tbody>
        <tr>
            <td>1</td>
            <td><select>
                <option val="112">A</option>
                <option val="2">B</option>
                <option val="356" selected>C</option>
            </select>
            </td>
            <td><input type="date" value="2018-07-22"></td>
            <td><input type="checkbox"></td>
        </tr>
        <tr>
            <td>123</td>
            <td><select>
                <option val="1" selected>A</option>
                <option val="2">B</option>
                <option val="3">C</option>
            </select>
            </td>
            <td><input type="date" value="2014-07-22"></td>
            <td><input type="checkbox"></td>
        </tr>

        <tr>
            <td>56</td>
            <td><select>
                <option val="1">A</option>
                <option val="2" selected>B</option>
                <option val="3">C</option>

            </select>
            </td>
            <td><input type="date" value="2014-08-23"></td>
            <td><input type="checkbox"></td>
        </tr>

        <tr>
            <td>14</td>
            <td><select>
                <option val="1">A</option>
                <option val="2" selected>B</option>
                <option val="3">C</option>

            </select>
            </td>
            <td><input type="date" value="2014-07-23"></td>
            <td><input type="checkbox"></td>
        </tr>
        </tbody>
</table>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
    <script src="https://unpkg.com/[email protected]/dist/bootstrap-table.min.js"></script>


    <script>
       $(document).ready(function() {
  console.log('ready');
  $('#my_table_1').find('input[type="date"]').change(function() {
    console.log('Table 1.Date was changed. Need to check if table is sorted by column C.If so - call the table sort.');
  });
  $('#my_table_1').find('select').change(function() {
    console.log('Table 1.Selection was changed. Need to check if table is sorted by column B.If so - call the table sort.');
  });
  $('#my_table_2').find('input[type="date"]').change(function() {
    console.log('Table 2.Date was changed. Need to check if table is sorted by column C.If so - call the table sort.');
  });
  $('#my_table_2').find('select').change(function() {
    console.log('Table 2.Selection was changed. Need to check if table is sorted by column B.If so - call the table sort.');
  });

});
    </script>
</body>
</html>

Upvotes: 1

Related Questions