私はinitの後にメソッドとオプションを受け入れる最初のプラグインを作成しようとしています。 私はjQueryのウェブサイト上でオーサリングプラグインのチュートリアルを読んでいたし、私はこのJQueryオーサリングプラグイン。合格メソッドとオプション
(function($) {
/* Default Options */
var defaults = {
column_sort_map: []
};
/* Global Scope */
var sort_col = false;
var sortMethods = {
date: function(a, b) {
var date1 = new Date($(a).find(":nth-child(" + sort_col + ")").html());
var date2 = new Date($(b).find(":nth-child(" + sort_col + ")").html());
if (date1 == date2) {
return 0;
}
if (date1 < date2) {
return -1;
}
return 1;
},
string_case: function(a, b) {
var aa = $(a).find(":nth-child(" + sort_col + ")").html();
var bb = $(b).find(":nth-child(" + sort_col + ")").html();
if (aa == bb) {
return 0;
}
if (aa > bb) {
return 1;
}
return -1;
},
string_nocase: function(a, b) {
var aa = $(a).find(":nth-child(" + sort_col + ")").html().toLowerCase();
var bb = $(b).find(":nth-child(" + sort_col + ")").html().toLowerCase();
if (aa == bb) {
return 0;
}
if (aa > bb) {
return 1;
}
return -1;
},
numeric: function(a, b) {
var aa = $(a).find(":nth-child(" + sort_col + ")").html().replace(/\D/g, '');
var bb = $(b).find(":nth-child(" + sort_col + ")").html().replace(/\D/g, '');
if (isNaN(aa)) {
aa = 0;
}
if (isNaN(bb)) {
bb = 0;
}
return aa - bb;
}
};
var methods = {
init: function(options) {
// extend options
if (options) {
$.extend(defaults, options);
}
alert(options.column_sort_map);
},
test: function() {
alert("I am a Test");
}
};
$.fn.dataTable = function(method) {
return this.each(function() {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.dataTable');
}
});
};
})(jQuery);
が出ていると私はHTMLコードが非常に大きい
$("#tbl").dataTable({
column_sort_map: [
"numeric",
"string_nocase",
"string_nocase",
"date",
"string_nocase",
"string_nocase",
"numeric"
]
});
$("#tbl").dataTable("test");
でそれを呼び出し、私は新しいテーブルを書くような気がしません。しかし、私の問題では、それは必須ではありません。
私はもう一度ストレスを感じなければなりません。これはこのようなプラグインを作成した私の最初の時の1つです。私はチュートリアルを完全に間違って解釈し、主に間違っているかもしれません。
私の問題は、options.column_sort_mapにアクセスしようとすると「未定義」エラーが発生するということです。ただし、test
への関数呼び出しは期待どおりに機能します。
私はあなたが示唆した変更をまだ行っていません。 http://jsfiddle.net/rlemon/TXF9S/450/私は 'var options = $ .extend({}、defaults、options);' – rlemon
を更新しました。今すぐチェックしてください。あなたが匿名関数の内部にいたため、使用しようとしていた「引数」パラメータがリセットされました。私は "適用"の代わりに "呼び出し"を使用し、引数の代わりにメソッドのパラメータを使用しました。 –
フィドルで貼り付けるのを忘れましたhttp://jsfiddle.net/skylar/TXF9S/454/ –