![](/img/trans.png)
[英]How to pass arguments from a javascript function to another function without calling it?
[英]Pass Arguments from One Function to Another Without Calling It
我試圖讓任何options
或理想, dynamicTable
從傳遞initializeTable
到applyTableFilters
功能,我有得到預期值的問題。 我正在使用List.js使表動態化,我需要傳遞或重新創建dynamicTable
對象,以便可以繼續使用它來過濾表。
這是從HTML表創建List.js對象的函數:
function initializeTable(options) { // initializes table to be dynamic using List.js functions
var dynamicTable = new List("table-content", options);
dynamicTable.on("updated", function (list) { // writes a message to the user if no results are found
if (list.matchingItems.length == 0) {
document.getElementById("no-results").style.display = "block";
}
else {
document.getElementById("no-results").style.display = "none";
}
});
console.log(dynamicTable);
console.log(options);
console.log(arguments.length);
applyTableFilters.bind();
}
我嘗試了不同的方法將變量傳遞給下面的函數。 我試過.call
, applyTableFilters(args)
,和.apply
,但問題是,我不希望函數從內本地執行的,只有當從按鈕的Click事件熄滅(在這些功能中未示出)。
這是我要傳遞給對象並繼續使用它進行過濾功能的函數:
function applyTableFilters(dynamicTable) {
var form = document.getElementById("filter-form");
//console.log(options);
//var dynamicTable = new List("table-content", options);
console.log(dynamicTable);
var filters = form.querySelectorAll('input[type="checkbox"]:checked');
dynamicTable.filter(function (item) {
console.log(item);
console.log(item._values);
if (item.values().id == 2) {
return true;
}
else {
return false;
}
//var filterStrings = [];
//console.log(filters);
//for (var i = 0; i < filters.length; i++) {
// var filterVal = filters[i].value;
// var filterString = "(" + item.values().column == filterVal + ")"; // filterVal.contains(item.values().column) ||
// filterStrings.push(filterString);
// console.log(filterVal);
// console.log(filterString);
//}
//console.log(filterStrings);
//var filterString = filterStrings.join(" && ");
//console.log(filterString);
//return filterString;
});
}
我用過:
applyTableFilters.bind(this, dynamicTable/options);
applyTableFilters.bind(null, dynamicTable/options);
applyTableFilters.bind(dynamicTable/options);
切換兩者,因為如果一個都工作了,則不需要同時傳遞兩者,等等。我總是傳遞一個鼠標事件,這甚至不是我要尋找的對象的正確類型。 如何獲得正確的對象? 同樣,第一個函數中的所有值都不為空,並且按預期填充,因此不是原始變量未定義或為null。 提前致謝。
從您的initializeTable
函數返回一個函數,該函數將applyTableFilters
函數與所需的參數包裝在一起。
然后將返回的函數分配給var,以便稍后執行。
function initializeTable(options) {
var dynamicTable = new List("table-content", options);
// other stuff
return function () {
applyTableFilters(dynamicTable)
}
}
// other stuff
var applyTableFiltersPrep = initializeTable(options)
// later, when you want to execute...
applyTableFiltersPrep()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.