if / else Javascript语句与DataTables

时间:2016-09-19 21:59:08

标签: javascript jquery ajax datatables

我有以下Javascript代码初始化我网站上的DataTables图表。

基本上,我对此脚本的意图是基于用户当前所在的URL在不同数据中的AJAX。我编写了一个if / else语句,该语句适用于我页面上的其他组件,但不适用于我的DataTables图表。

为什么这不起作用?有没有更好的方法来实现这一目标?

var URL = window.location.href;

if (URL.indexOf("london") !== -1) {
  // initialize datatables chart
  $(document).ready( function () {
    var table = $('#aging_projects').DataTable({
    "processing": true,
    "serverSide": false,
    "ajax": "../financial-reports/aging-projects-london.php",
    "destroy": true,
    "resonsive": true,
    initComplete: function () {
        this.api().columns().every( function () {
            var column = this;
            var select = $('<select class="form-control"><option value=""></option></select>')
                .appendTo( $(column.footer()).empty() )
                .on( 'change', function () {
                    var val = $.fn.dataTable.util.escapeRegex(
                        $(this).val()
                    );

                    column
                        .search( val ? '^'+val+'$' : '', true, false )
                        .draw();
                } );

            column.data().unique().sort().each( function ( d, j ) {
                select.append( '<option value="'+d+'">'+d+'</option>' )
            } );
        } );
    }
    });
  });
}
if (URL.indexOf("nw") !== -1) {
  // initialize datatables chart
  $(document).ready( function () {
    var table = $('#aging_projects').DataTable({
    "processing": true,
    "serverSide": false,
    "ajax": "../financial-reports/aging-projects-nw.php",
    "destroy": true,
    "resonsive": true,
    initComplete: function () {
        this.api().columns().every( function () {
            var column = this;
            var select = $('<select class="form-control"><option value=""></option></select>')
                .appendTo( $(column.footer()).empty() )
                .on( 'change', function () {
                    var val = $.fn.dataTable.util.escapeRegex(
                        $(this).val()
                    );

                    column
                        .search( val ? '^'+val+'$' : '', true, false )
                        .draw();
                } );

            column.data().unique().sort().each( function ( d, j ) {
                select.append( '<option value="'+d+'">'+d+'</option>' )
            } );
        } );
    }
    });
  });
}
if (URL.indexOf("loupe") !== -1) {
  // initialize datatables chart
  $(document).ready( function () {
    var table = $('#aging_projects').DataTable({
    "processing": true,
    "serverSide": false,
    "ajax": "../financial-reports/aging-projects-loupe.php",
    "destroy": true,
    "resonsive": true,
    initComplete: function () {
        this.api().columns().every( function () {
            var column = this;
            var select = $('<select class="form-control"><option value=""></option></select>')
                .appendTo( $(column.footer()).empty() )
                .on( 'change', function () {
                    var val = $.fn.dataTable.util.escapeRegex(
                        $(this).val()
                    );

                    column
                        .search( val ? '^'+val+'$' : '', true, false )
                        .draw();
                } );

            column.data().unique().sort().each( function ( d, j ) {
                select.append( '<option value="'+d+'">'+d+'</option>' )
            } );
        } );
    }
    });
  });
}
if (URL.indexOf("new-york") !== -1) {
  // initialize datatables chart
  $(document).ready( function () {
    var table = $('#aging_projects').DataTable({
    "processing": true,
    "serverSide": false,
    "ajax": "../financial-reports/aging-projects-new-york.php",
    "destroy": true,
    "resonsive": true,
    initComplete: function () {
        this.api().columns().every( function () {
            var column = this;
            var select = $('<select class="form-control"><option value=""></option></select>')
                .appendTo( $(column.footer()).empty() )
                .on( 'change', function () {
                    var val = $.fn.dataTable.util.escapeRegex(
                        $(this).val()
                    );

                    column
                        .search( val ? '^'+val+'$' : '', true, false )
                        .draw();
                } );

            column.data().unique().sort().each( function ( d, j ) {
                select.append( '<option value="'+d+'">'+d+'</option>' )
            } );
            } );
        }
    });
  });
}
if (URL.indexOf("content") !== -1) {
  // initialize datatables chart
  $(document).ready( function () {
    var table = $('#aging_projects').DataTable({
    "processing": true,
    "serverSide": false,
    "ajax": "../financial-reports/aging-projects-content.php",
    "destroy": true,
    "resonsive": true,
    initComplete: function () {
        this.api().columns().every( function () {
            var column = this;
            var select = $('<select class="form-control"><option value=""></option></select>')
                .appendTo( $(column.footer()).empty() )
                .on( 'change', function () {
                    var val = $.fn.dataTable.util.escapeRegex(
                        $(this).val()
                    );

                    column
                        .search( val ? '^'+val+'$' : '', true, false )
                        .draw();
                } );

            column.data().unique().sort().each( function ( d, j ) {
                select.append( '<option value="'+d+'">'+d+'</option>' )
            } );
        } );
    }
    });
  });
}
else {
  // initialize datatables chart
  $(document).ready( function () {
    var table = $('#aging_projects').DataTable({
    "processing": true,
    "serverSide": false,
    "ajax": "../financial-reports/aging-projects.php",
    "destroy": true,
    "resonsive": true,
    initComplete: function () {
        this.api().columns().every( function () {
            var column = this;
            var select = $('<select class="form-control"><option value=""></option></select>')
                .appendTo( $(column.footer()).empty() )
                .on( 'change', function () {
                    var val = $.fn.dataTable.util.escapeRegex(
                        $(this).val()
                    );

                    column
                        .search( val ? '^'+val+'$' : '', true, false )
                        .draw();
                } );

            column.data().unique().sort().each( function ( d, j ) {
                select.append( '<option value="'+d+'">'+d+'</option>' )
            } );
        } );
        }
    });
  });
}

1 个答案:

答案 0 :(得分:1)

我看不到错误,但是如果以这种方式更改代码,应该更容易调试并找到错误。

var URL = window.location.href;
var ajaxURL = '';

if (URL.indexOf("london") !== -1) {
  ajaxURL = 'aging-projects-london.php';
}else if (URL.indexOf("nw") !== -1) {
  ajaxURL = 'aging-projects-nw.php';
}else if (URL.indexOf("loupe") !== -1) {
  ajaxURL = 'aging-projects-loupe.php';
}else if (URL.indexOf("new-york") !== -1) {
    ajaxURL = 'aging-projects-new-york.php'; 
}else if (URL.indexOf("content") !== -1) {
    ajaxURL = 'aging-projects-content.php'; 
}else {
  ajaxURL = 'aging-projects.php'; 
}
$(document).ready( function () {
    var table = $('#aging_projects').DataTable({
        "processing": true,
        "serverSide": false,
        "ajax": "../financial-reports/" + ajaxURL,
        "destroy": true,
        "resonsive": true,
        initComplete: function () {
            this.api().columns().every( function () {
                var column = this;
                var select = $('<select class="form-control"><option value=""></option></select>')
                .appendTo( $(column.footer()).empty() )
                .on( 'change', function () {
                    var val = $.fn.dataTable.util.escapeRegex(
                        $(this).val()
                        );

                    column
                    .search( val ? '^'+val+'$' : '', true, false )
                    .draw();
                } );

                column.data().unique().sort().each( function ( d, j ) {
                    select.append( '<option value="'+d+'">'+d+'</option>' )
                } );
            } );
        }
    });
});