简体   繁体   English

将参数从一个函数传递到另一个而不调用它

[英]Pass Arguments from One Function to Another Without Calling It

I'm trying to get either options or, ideally, dynamicTable passed from initializeTable to the applyTableFilters function and I'm having problems getting the expected values. 我试图让任何options或理想, dynamicTable从传递initializeTableapplyTableFilters功能,我有得到预期值的问题。 I'm using List.js to make a table dynamic and I need to pass or recreate the dynamicTable object so I can go ahead and use it to filter the table. 我正在使用List.js使表动态化,我需要传递或重新创建dynamicTable对象,以便可以继续使用它来过滤表。

Here is the function that creates the List.js object from the HTML table: 这是从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();
}

I've tried different methods to pass the variables to the function below. 我尝试了不同的方法将变量传递给下面的函数。 I tried .call , applyTableFilters(args) , and .apply , but the problem is that I do not want the function to execute from inside here, only when the click event from the button goes off (not shown in these functions). 我试过.callapplyTableFilters(args) ,和.apply ,但问题是,我不希望函数从内本地执行的,只有当从按钮的Click事件熄灭(在这些功能中未示出)。

This is the function I want to pass the object to and proceed to make the filter functions using it: 这是我要传递给对象并继续使用它进行过滤功能的函数:

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;
    });
}

I've used: 我用过:

applyTableFilters.bind(this, dynamicTable/options); 
applyTableFilters.bind(null, dynamicTable/options);
applyTableFilters.bind(dynamicTable/options);

Switching between the two since I don't need both passed if one ends up working, etc. I always get a mouse event passed in and that's not even the right type of object I'm looking for. 切换两者,因为如果一个都工作了,则不需要同时传递两者,等等。我总是传递一个鼠标事件,这甚至不是我要寻找的对象的正确类型。 How can I get the right object passed? 如何获得正确的对象? Also all the values in the first function are not empty and are populated as expected so it's not the original variables being undefined or null. 同样,第一个函数中的所有值都不为空,并且按预期填充,因此不是原始变量未定义或为null。 Thanks in advance. 提前致谢。

From your initializeTable function return a function that wraps the applyTableFilters function with the arguments you want. 从您的initializeTable函数返回一个函数,该函数将applyTableFilters函数与所需的参数包装在一起。

Then assign the returned function to a var to be executed later. 然后将返回的函数分配给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()

JSFiddle example JSFiddle示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM