从列选项中调用 table

Get calling table from column option

我一页有几个Bootstraptable。每个 table 都有一些数据属性(如 data-link="test-page" 等)。除此之外,每个 Bootstrap table 的一列使用 column formatter,使用 data-formatter="actionFormatter"。但是,我想在调用 actionFormatter 时获取当前 table 数据属性,因此基于数据属性我可以 return 一个字符串。

this$(this) return 都是对象,这不起作用。 $(this).closest('table').data() 也行不通,而我希望那个是最真实的。

这是我使用的代码:

<th data-field="actions" data-formatter="actionFormatter" data-events="actionEvents">Actions</th>

this return 一个具有行属性的 JSON 对象,并且 $(this).closest('table').data(XXX) return 未定义。我希望它 return 一个包含所有数据属性的数组。

有没有办法从格式化程序中获取当前处理 table?

示例代码:

<!-- table 1 -->
<table
    data-actions="edit,remove"
    data-url="some/url"
>
    <thead>
        <tr>
            <th data-formatter="actionFormatter">Actions</th>
        </tr>
    </thead>
</table>

<!-- table 2 -->
<table
    data-actions="edit,remove"
    data-url="some/url"
>
    <thead>
        <tr>
            <th data-formatter="actionFormatter">Actions</th>
        </tr>
    </thead>
</table>


// actionFormatter:
function actionFormatter(value, row, index, field) {
    // get data-actions from the right table somehow, 
    // and return a string based on data-url/other
    // data attributes
}

似乎在调用操作格式化程序时,执行上下文 this 是一个包含所有 bootstrap table 行关联数据以及所有 data-* 行的属性。

考虑到这一点,您可以为每个 table 添加一个 ID,并为您的行添加一个 data-table-id 属性,例如:

<table
    id="table-1"
    data-actions="edit,remove"
    data-url="some/url"
>
<thead>
    <tr>
        <th data-formatter="actionFormatter" data-table-id="table-1">Actions</th>
    </tr>
</thead>

以便在您的格式化程序中您可以使用该 id 检索 table DOM 元素:

function actionFormatter(value, row, index, field) {
    // get data-actions from the right table somehow, 
    // and return a string based on data-url/other
    // data attributes
    var data = $('#' + this.tableId).data();
}