【问题标题】:How to combine two JavaScript code blocks如何组合两个 JavaScript 代码块
【发布时间】:2017-06-16 11:33:55
【问题描述】:

我是 javascript 新手,我正在尝试组合 2 个 javascript 块,但我崩溃了?

第一块

<script>
$(document).ready(function() {
    // Setup - add a text input to each footer cell
    $('table.table tfoot th').each( function () {
        var title = $(this).text();
        $(this).html( '<input type="text" placeholder="'+title+' ARA" />' );
    } );

    // DataTable
    var table = $('table.table').DataTable();

    // Apply the search
    table.columns().every( function () {
        var that = this;

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

第二块

$('table.table').DataTable({
"order": [
  [0, "desc"]
],
paging: true,
"oLanguage": {
  "sUrl": "js/dil/LANGUAGE.json",
}
} );

我想在第一个 javascript 中添加“oLanguage”和“order”行。我该怎么办?

【问题讨论】:

    标签: javascript datatables


    【解决方案1】:

    只需将数组从DataTables 复制粘贴到第一部分:

    $(document).ready(function() {
        // Setup - add a text input to each footer cell
        $('table.table tfoot th').each(function () {
            $(this).html('<input type="text" placeholder="'+$(this).text()+' ARA" />');
        });
    
        // DataTable
        var table = $('table.table').DataTable({
            order: [
              [0, "desc"]
            ],
            paging: true,
            oLanguage: {
              sUrl: "js/dil/LANGUAGE.json",
            }
        });
    
        // Apply the search
        table.columns().every(function () {
            var that = this;
    
            $('input', this.footer()).on('keyup change', function () {
                if (that.search() !== this.value ) {
                    that
                        .search( this.value )
                        .draw();
                }
            });
        });
    });
    

    注意 DataTables 过去在 hUngarianNotation 中有它的变量,后来移到 camlCase,所以很可能你的 oLanguage 没有被新 API 识别

    【讨论】:

    • +$(this).text()+ 不显示 seacrh 结果?其他人在工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多