从列选项中获取调用表

问题描述 投票:0回答:1

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

this$(this)都返回一个Object,它不起作用。 $(this).closest('table').data()也不起作用,而我期望一个是最真实的。

这是我使用的代码:

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

this返回带有行属性的JSON对象,$(this).closest('table').data(XXX)返回undefined。我希望它返回一个包含所有数据属性的数组。

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

示例代码:

<!-- 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
}
javascript jquery bootstrap-table
1个回答
1
投票

似乎在调用动作格式化程序时,执行上下文this是一个对象,其中包含所有引导表行关联数据以及该行的所有data-*属性。

考虑到这一点,您可以为每个表添加一个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检索表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();
}
© www.soinside.com 2019 - 2024. All rights reserved.