问题
I want to run a function on click that runs multiple other javascript functions. It works, but only for the first few functions called.
More Detail: I am trying to filter a grid. The "savedSearch" function is called by a button that will then search multiple fields according to your saved search criteria (i.e. filter column A for "Monday", which is in the Model). Each "sub-function" (the "appliedSearch" functions) applies to a different field. The first few usually work (I've tried formatting this many different ways). For example, it will filter columns A, B, and C, but nothing happens for columns D, E, and F.
Note: "applySearch" is the function I am trying to call, "ColumnA" is the field name I am sending to the function, @Html.Raw(Json.Encode(Model.ColumnA)) is the variable (i.e. "Monday" in my example above).
function savedSearch(e){
if (@Html.Raw(Json.Encode(Model.ColumnA)) != "0")
{
applySearch1("ColumnA", @Html.Raw(Json.Encode(Model.ColumnA)));
}
if (@Html.Raw(Json.Encode(Model.ColumnB)) != "0")
{
applySearch2("ColumnB", @Html.Raw(Json.Encode(Model.ColumnB)));
}
if (@Html.Raw(Json.Encode(Model.ColumnC)) != "0")
{
applySearch3("ColumnC", @Html.Raw(Json.Encode(Model.ColumnC)));
}
if (@Html.Raw(Json.Encode(Model.ColumnD)) != "0")
{
applySearch4("ColumnD", @Html.Raw(Json.Encode(Model.ColumnD)));
}
if (@Html.Raw(Json.Encode(Model.ColumnE)) != "0")
{
applySearch5("ColumnE", @Html.Raw(Json.Encode(Model.ColumnE)));
}
if (@Html.Raw(Json.Encode(Model.ColumnF)) != "0")
{
applySearch6("ColumnF", @Html.Raw(Json.Encode(Model.ColumnF)));
}
}
}
The "applySearch" functions being called are like below (this works):
function applySearch(filterField, filterValue) {
var gridData = $("#grid").data("kendoGrid");
var currFilterObj = gridData.dataSource.filter();
var currentFilters = currFilterObj ? currFilterObj.filters : [];
if (currentFilters && currentFilters.length > 0) {
for (var i = 0; i < currentFilters.length; i++) {
if (currentFilters[i].field == filterField) {
currentFilters.splice(i, 1);
break;
}
}
}
if (filterValue != "0" && filterValue != "") {
currentFilters.push({
field: filterField,
operator: "contains",
value: filterValue
});
}
gridData.dataSource.filter({
logic: "and",
filters: currentFilters
});
}
来源:https://stackoverflow.com/questions/30645765/when-running-multiple-javascript-functions-only-first-few-work