Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After destroying bootstrap-table custom view events not remove #6578

Closed
wil-wu opened this issue Dec 23, 2022 · 1 comment
Closed

After destroying bootstrap-table custom view events not remove #6578

wil-wu opened this issue Dec 23, 2022 · 1 comment
Labels
help-wanted Issues we need or would love help from the community to resolve.

Comments

@wil-wu
Copy link

wil-wu commented Dec 23, 2022

Bootstraptable version(s) affected

1.21.2

Description

When custom view body rendered, I add some listeners on custom view element. but when I destroy bootstrap-table, event 'custom-view-post-body.bs.table' not remove. the event bound many times.

Example(s)

<button id="build" class="btn btn-primary">build</button>
<button id="destroy" class="btn btn-danger">destroy</button>
<table id="table"></table>
<template id="viewTemplate">
    <div class="col">
        <button class="action btn btn-info">%name%</button>
    </div>
</template>

<script>
    let $table = $('#table')

    function initTable() {
        $table.bootstrapTable('destroy').bootstrapTable({
            showCustomView: true,
            customView: customView,
            columns: [{
                field: 'id',
                title: 'Item ID'
            }, {
                field: 'name',
                title: 'Item Name'
            }, {
                field: 'price',
                title: 'Item Price'
            }],
            data: [{
                id: 1,
                name: 'Item 1',
                price: '$1'
            }, {
                id: 2,
                name: 'Item 2',
                price: '$2'
            }, {
                id: 2,
                name: 'Item 2',
                price: '$2'
            }]
        })

        $table.on('custom-view-post-body.bs.table', () => {
            actions()
        })

        function customView(data) {
            let template = document.getElementById('viewTemplate').innerHTML
            let view = ''
            for (const row of data) {
                view += template.replace('%name%', row.name)
            }
            return `<div class="row">${view}</div>`
        }

        function actions() {
            let customViewEl = document.querySelector('.fixed-table-custom-view')

            customViewEl.querySelectorAll('.action').forEach((el) => {
                el.addEventListener('click', () => {
                    console.log('action')
                })
            })
        }
    }

    document.getElementById('build').addEventListener('click', function () {
        initTable()
    })

    document.getElementById('destroy').addEventListener('click', function () {
        $table.bootstrapTable('destroy')
    })

</script>

Possible Solutions

initTable function

$table
  .off('custom-view-post-body.bs.table')
  .on('custom-view-post-body.bs.table', () => {
      actions()
  })

Additional Context

No response

@wil-wu wil-wu added the Bug Issues which are marked as Bug label Dec 23, 2022
@UtechtDustin UtechtDustin added help-wanted Issues we need or would love help from the community to resolve. and removed Bug Issues which are marked as Bug labels Feb 13, 2023
@UtechtDustin
Copy link
Collaborator

UtechtDustin commented Feb 13, 2023

This is not a bug, the event listener is created by you.
So you have to remove it, if you destroy the table.
The bootstrap-table will only remove event listeners which are created by the bootstrap-table.

The easiest fix for you would be to move adding the listener out of the initTable method.
Would also increase the performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help-wanted Issues we need or would love help from the community to resolve.
Projects
None yet
Development

No branches or pull requests

2 participants