jqueryを使用してデータテーブルを作成しています。そのために、私はdocument.ready関数内のjavascriptにjsonオブジェクトを書きました。以下は私のコードです:jatableオブジェクトをデータテーブルとして宣言する方法
$(document).ready(function() {
var multistatement = [
{"accountNumber":"131-202553701","name":"Account 1","alias":"Atlanta 1","dueDate":"10/19/2016","statementBalance":"34.60","statement":"1"},
{"accountNumber":"131-202553702","name":"Account 2","alias":"Atlanta 2","dueDate":"10/19/2015","statementBalance":"14.50","statement":"3"},
{"accountNumber":"131-202553703","name":"Account 3","alias":"Atlanta 3","dueDate":"10/19/2015","statementBalance":"15.50","statement":"1"},
{"accountNumber":"131-202553704","name":"Account 4","alias":"Atlanta 4","dueDate":"10/19/2015","statementBalance":"15.50","statement":"1"},
{"accountNumber":"131-202553705","name":"Account 5","alias":"Atlanta 5","dueDate":"09/19/2016","statementBalance":"100.50","statement":"1"},
{"accountNumber":"131-202553706","name":"Account 6","alias":"Atlanta 6","dueDate":"12/19/2017","statementBalance":"18.50","statement":"1"},
{"accountNumber":"131-202553707","name":"Account 7","alias":"Atlanta 7","dueDate":"01/01/2015","statementBalance":"105.50","statement":"3"},
{"accountNumber":"131-202553708","name":"Account 8","alias":"Atlanta 8","dueDate":"10/19/2015","statementBalance":"15.50","statement":"1"},
{"accountNumber":"131-202553709","name":"Account 9","alias":"Atlanta 9","dueDate":"10/07/2015","statementBalance":"15.50","statement":"10"},
{"accountNumber":"131-202553710","name":"Account 10","alias":"Atlanta 10","dueDate":"10/19/2016","statementBalance":"15.50","statement":"1"},
{"accountNumber":"131-202553711","name":"Account 11","alias":"Atlanta 11","dueDate":"10/19/2015","statementBalance":"15.50","statement":"1"},
{"accountNumber":"131-202553712","name":"Account 12","alias":"Atlanta 12","dueDate":"04/04/2016","statementBalance":"115.50","statement":"1"},
{"accountNumber":"131-202553713","name":"Account 13","alias":"Atlanta 13","dueDate":"05/19/2015","statementBalance":"25.50","statement":"1"},
{"accountNumber":"131-202553714","name":"Account 14","alias":"Atlanta 14","dueDate":"03/19/2015","statementBalance":"135.50","statement":"1"},
{"accountNumber":"131-202553715","name":"Account 15","alias":"Atlanta 15","dueDate":"10/19/2017","statementBalance":"15.50","statement":"1"},
{"accountNumber":"131-202553716","name":"Account 16","alias":"Atlanta 16","dueDate":"10/19/2015","statementBalance":"08.50","statement":"1"},
{"accountNumber":"131-202553717","name":"Account 17","alias":"Atlanta 17","dueDate":"10/08/2015","statementBalance":"10.50","statement":"1"},
{"accountNumber":"131-202553718","name":"Account 18","alias":"Atlanta 18","dueDate":"09/19/2015","statementBalance":"15.50","statement":"1"},
{"accountNumber":"131-202553719","name":"Account 19","alias":"Atlanta 19","dueDate":"10/05/2015","statementBalance":"15.80","statement":"1"},
{"accountNumber":"131-202553720","name":"Account 20","alias":"Atlanta 20","dueDate":"10/19/2015","statementBalance":"39.50","statement":"1"},
{"accountNumber":"131-202553721","name":"Account 21","alias":"Atlanta 21","dueDate":"10/21/2015","statementBalance":"15.50","statement":"1"},
{"accountNumber":"131-202553722","name":"Account 22","alias":"Atlanta 22","dueDate":"10/19/2016","statementBalance":"15.50","statement":"1"}
];
generateDatatable();
});
function generateDatatable(){
$('#multiple-account-table').dataTable({
"data": multistatement,
"dom": 'it',
"pageLength": 8,
"language": {
"info": "Viewing accounts 1 - _END_ of _TOTAL_ ",
"emptyTable": "No records are available",
},
"columns": [
{"data": null},
{"data": "accountNumber"},
{"data": "name"},
{"data": "alias"},
{"data": "dueDate"},
{"data": "statementBalance"},
{"data": null}
],
"columnDefs": [
{className: "pad-md-left-p-10 pad-top-bottom-p-10 white-active-bg mouse-link", "targets": [0,1,2,3,4,5,6]},
{
'targets': 0,
'orderable': false,
'render': function(data, type, full, meta) {
return '<input type="checkbox" name="payment-checkbox" class="multi-checkbox"/><label for="select-checkbox"></label>';
}
}
}); //End of datatable function
}
しかし、このコードは、「複数ステートメントのが定義されていない」というエラーを与えています。 jsonオブジェクトを定義するのは間違った方法ですか?
私は、同じjavascriptファイル内のどの関数からもアクセスできます。なぜ私はこれをdocument.ready関数の中に入れているのですか?私はこれをgenerateDatatable()に入れれば、別の関数でアクセスすることはできません。 – Teddu
その主な理由は、私は口座番号の値を "ステートメント"の値に基づいて表示したい。文が1つだけの場合は、それだけが表示されます。そのために別の関数を書く必要がありました – Teddu
document.ready()の外で宣言すると、グローバルに利用可能になります –