Search code examples
javascripttwitter-bootstraphtml-table

Why my event on export bootstrap-tables is not working?


I'm trying to make a function when the export start event happens, but I'm not able to catch this event. I'm trying alert() when on export-started.bs.table. Just doesn't work, so what is the event when I try export my table?

(I couldn't make an example of export in this code too, can someone help me put the button that downloads the file in excel?)

let arrData = [
      {
        'id': 0,
        'name': 'Item 0',
        'price': '$0'
      },
      {
        'id': 1,
        'name': 'Item 1',
        'price': '$1'
      },
      {
        'id': 2,
        'name': 'Item 2',
        'price': '$2'
      },
      {
        'id': 3,
        'name': 'Item 3',
        'price': '$3'
      },
      {
        'id': 4,
        'name': 'Item 4',
        'price': '$4'
      },
      {
        'id': 5,
        'name': 'Item 5',
        'price': '$5'
      }
    ]

$('#merged').bootstrapTable({
    data: arrData,
    exportTypes: ['csv','json','excel','doc','txt','sql','xml'],
    exportDataType: 'all',
    columns: [{
                title: "id",
                align: 'center',
                valign: 'middle',
                field: "id"
            },{
                title: "name",
                align: 'center',
                valign: 'middle',
                field: "name"
            },{
                title: "price",
                align: 'center',
                valign: 'middle',
                field: "price"
            }]
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/bootstrap-table.min.css">
<script src="https://unpkg.com/[email protected]/dist/bootstrap-table.min.js"></script>
<script src="https://unpkg.com/tableexport.jquery.plugin/tableExport.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/extensions/export/bootstrap-table-export.min.js"></script>
<div>
  <table class="table" id="merged">
  </table>
</div>


Solution

  • That should work. You must enter the event in the table configuration.

    onExportStarted: function (arg1, arg2) { alert('hi'); },
    

    let arrData = [
          {
            'id': 0,
            'name': 'Item 0',
            'price': '$0'
          },
          {
            'id': 1,
            'name': 'Item 1',
            'price': '$1'
          },
          {
            'id': 2,
            'name': 'Item 2',
            'price': '$2'
          },
          {
            'id': 3,
            'name': 'Item 3',
            'price': '$3'
          },
          {
            'id': 4,
            'name': 'Item 4',
            'price': '$4'
          },
          {
            'id': 5,
            'name': 'Item 5',
            'price': '$5'
          }
        ]
    
    $('#merged').bootstrapTable({
        data: arrData,
        exportTypes: ['csv','json','excel','doc','txt','sql','xml'],
        exportDataType: 'all',
        showExport: true,
        onExportStarted: function (arg1, arg2) { alert('hi'); },
        columns: [{
                    title: "id",
                    align: 'center',
                    valign: 'middle',
                    field: "id"
                },{
                    title: "name",
                    align: 'center',
                    valign: 'middle',
                    field: "name"
                },{
                    title: "price",
                    align: 'center',
                    valign: 'middle',
                    field: "price"
                }]
    });
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/bootstrap-table.min.css">
    <script src="https://unpkg.com/[email protected]/dist/bootstrap-table.min.js"></script>
    <script src="https://unpkg.com/tableexport.jquery.plugin/tableExport.min.js"></script>
    <script src="https://unpkg.com/[email protected]/dist/extensions/export/bootstrap-table-export.min.js"></script>
    <div>
      <table class="table" id="merged">
      </table>
    </div>