简体   繁体   English

jQuery DataTable列搜索因翻译而中断

[英]Jquery DataTable column search breaking with translations

We're using Jquery DataTable as our grid library. 我们正在使用Jquery DataTable作为我们的网格库。 Whenever we initialize the DataTable everything works fine. 每当我们初始化DataTable时,一切都很好。 Our application will support multiple locales, so obviously we want the grid to translate itself. 我们的应用程序将支持多种语言环境,因此显然我们希望网格能够进行自身转换。

We're using the standard approach which we found in the documentation. 我们正在使用在文档中找到的标准方法。 The translation works as expected but the column search always return no results. 翻译工作正常,但列搜索始终不返回任何结果。 When we comment out the language property the column search works. 当我们注释掉language属性时,列搜索有效。

The json files are copied directly from the CDN provided by the library. json文件直接从库提供的CDN复制。

var locale = $('#locale').text();

var advance = $('#advanced-table').DataTable( {
    dom: 'Bfrtip',
    // language: {
    //     'url': '/assets/js/translations/datatable/' + locale + '.json'
    // }, <- this is causing the column search to break
    responsive: true,
    autoWidth: false,
    buttons: [
        {
            extend: 'excel',
            exportOptions: {
                columns: 'thead th:not(.no-sort)'
            }
        },
        {
            extend: 'pdf',
            exportOptions: {
                columns: 'thead th:not(.no-sort)'
            }
        },
        {
            extend: 'print',
            exportOptions: {
                columns: 'thead th:not(.no-sort)'
            }
        }
    ]
});

$('#advanced-table tfoot th').each(function() {
    var title = $(this).text();
    $(this).html('<div class="md-input-wrapper"><input type="text" class="md-form-control" placeholder="' + translator.get('datatable.search') + " " + title + '" /><span class="md-line"></span></div>');
});

advance.columns().every(function() {
    var that = this;

    $('input', this.footer() ).on('keyup change', function () {
        if (that.search() !== this.value) {
            that
                .search(this.value)
                .draw();
        }
    });
});

Ok.. seems like it was just a "simple" case of a race-condition. 好吧..看来这只是一个竞争条件的“简单”案例。 The language was loaded too slow and meant that the search function could not find the correct controls to filter. 该语言的加载速度太慢,意味着搜索功能无法找到要过滤的正确控件。 Solved it by putting the search function in the initComplete property. 通过将搜索功能放在initComplete属性中解决了该问题。

var locale = $('#locale').text();

var advance = $('#advanced-table').DataTable( {
    dom: 'Bfrtip',
    language: {
        'url': '/assets/js/translations/datatable/' + locale + '.json'
    },
    responsive: true,
    autoWidth: false,
    buttons: [
        {
            extend: 'excel',
            exportOptions: {
                columns: 'thead th:not(.no-sort)'
            }
        },
        {
            extend: 'pdf',
            exportOptions: {
                columns: 'thead th:not(.no-sort)'
            }
        },
        {
            extend: 'print',
            exportOptions: {
                columns: 'thead th:not(.no-sort)'
            }
        }
    ],
    initComplete: function () {
        advance.columns().every( function () {
            var that = this;
            $( 'input', this.footer() ).on( 'keyup change', function() {
                if ( that.search() !== this.value ) {
                    that
                        .search( this.value )
                        .draw();
                }
            });
        });
    }
});

$('#advanced-table tfoot th').each(function() {
    var title = $(this).text();
    $(this).html('<div class="md-input-wrapper"><input type="text" class="md-form-control" placeholder="' + translator.get('datatable.search') + " " + title + '" /><span class="md-line"></span></div>');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM