当标头为{{变量}}时,如何使用Jquery从表中获取(标头)文本

时间:2019-02-10 11:23:20

标签: jquery html flask datatables

我看过很多关于从标头获取值/文本的文章,但是没有一种解决方案适用于我的特定用例,而且我怀疑这可能是由于标头获得了其价值来自我的基础python代码(我正在使用flask)。

<table id="UCNTable" class="table table-striped table-bordered table-sm" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>
                    {{ header[0] }}
                    </th>
                    <th>
                    {{ header[2] }}
                    </th>
                    <th>
                    {{ header[3] }}
                    </th>
                    <th>
                    {{ header[4] }}
                    </th>
                    <th>
                    {{ header[5] }}
                    </th>
                </tr>
            </thead>
            <tbody>
                {% for row in data %}
                <tr>
                    <td>
                    {{row.name}}
                    </td>
                    <td>
                    {{row.Amt}}
                    </td>
                    <td>
                    {{row.OK}}
                    </td>
                    <td>
                    {{row.Err}}
                    </td>
                    <td>
                    {{row.prT}}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
            <tfoot>
                <tr>
                    <th>
                    {{ header[0] }}
                    </th>
                    <th>
                    {{ header[2] }}
                    </th>
                    <th>
                    {{ header[3] }}
                    </th>
                    <th>
                    {{ header[4] }}
                    </th>
                    <th>
                    {{ header[5] }}
                    </th>
                </tr>
            </tfoot>
        </table>

   <script>
        $('#UCNTable tfoot th').each( function () {
            var title = $(this).text();
            $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
        } );
</script>

因此,基本上,这应该说“搜索变量”,但它只是说搜索。 任何关于我做错事的想法吗? 我怀疑JQuery正在寻找一些文本,但是由于它是一个变量,所以它有问题-可以提供任何帮助! 谢谢。

1 个答案:

答案 0 :(得分:1)

我找到了解决方法! 事实证明,在我的函数中,当将 variable 添加到占位符时,不知何故/ n换行符出现在其中,因此我对变量进行了修剪以使其能够正确显示结果

$('#UCNTable tfoot th').each( function () {
        var title = $(this).text();         
        var trimTitle = $.trim(title.replace(/[\t\n]+/g,' '));

        $(this).html( '<input type="text" placeholder="Search '+trimTitle+'"/>' );
    } );