在jQuery过滤器函数中使用动态创建的变量

时间:2019-02-18 13:50:23

标签: javascript jquery filter filtering

同时知道如何创建基于计数的变量列表(请参见Create dynamic variable names based on count result)。

但是现在我还需要知道如何在到目前为止看起来像这样的过滤器函数内使用此动态变量列表:

$("div.item")
  .filter(function( index ) {
  return $(this).data("sample1") == samplevariable1 &&
         $(this).data("muster1") == mustervariable1;
}).css( "border", "3px double red" );

可以有1到x个动态创建的变量,而不是SampleVariable1,SampleVariable2。

如何重写现有的过滤器函数,以便可以根据需要将其用于尽可能多的变量?

谢谢您的帮助!

JSFiddle: https://jsfiddle.net/SchweizerSchoggi/30od7vf8/58/

1 个答案:

答案 0 :(得分:1)

假设您要基于匹配某些生成的值来突出显示DIV列表

在以下基于your provided fiddle的解决方案中,您将找到:

  • 生成器功能,该函数用连续的数字填充两个数组(最多生成N个值)。请参见对相应问题Create dynamic variable names based on count result
  • 的回答数组方法
  • 修改功能,用于模拟先前生成的值的某些更改(因为您所说的脚本中可能会发生这种情况)
  • 一个过滤并突出显示功能,该功能使用 jQuery (用于具有“ item”类的DIV元素)选择HTML元素,并根据匹配的DATA对其进行过滤-带有相应的数组元素。过滤后的HTML元素将使用指定的CSS边框突出显示。

注意:不同的索引编制

  • JS中的数组通常以0(第一个元素)开始到N(其中数组有N + 1个元素)结束索引。
  • HTML的DIV元素(即data-sample1data-muster1)中的
  • 您的数据属性从1开始索引。因此,该解决方案定义了一个特殊的index-variable :let divIndex = i+1;

/* N: used to generate N initial values as arrays */
const SEGMENT_COUNT = 2;
/* contains two dynamically generated arrays */
var generatedData = {elementsCount: 0, sample: [], muster: []};


/* Generate dynamic variables:
Creates two arrays named 'sample' and 
muster' inside sampleMusterArrays. These arrays are each filled with elementsCount elements (so that elements can be index from 0 to elementsCount-1 ). */
function generateValues(sampleMusterArrays, elementsCount) {
  for (let i = 0; i < elementsCount; i++) {
    sampleMusterArrays.sample[i] = i+1;
    sampleMusterArrays.muster[i] = i+1;
  }
  sampleMusterArrays.elementsCount = elementsCount;
}

function modifyGeneratedValues() {
  // modify FIRST value in sample-array
  generatedData.sample[0] = '2';
  // modify SECOND value in sample-array
  generatedData.sample[1] = 'x';
  // modify FIRST value in muster-array
  generatedData.muster[0] = '3';
  // modify SECOND value in muster-array
  generatedData.muster[1] = 'x';
}

/* filter DIVs with class "item" based on matching sampleMusterArrays to corresponding data-values */
function highlightFilteredDivItems(sampleMusterArrays) {

  $( "div.item" )
    .filter(function( index ) {
      let foundMatch = false;
      for (var i=0; i < sampleMusterArrays.elementsCount; i++) {
        let divIndex = i+1;
        foundMatch = foundMatch ||
             $(this).data("sample"+divIndex) == sampleMusterArrays.sample[i] &&
             $(this).data("muster"+divIndex) == sampleMusterArrays.muster[i];
     } // end for-loop
     return foundMatch;
    }).css( "border", "3px double red" );
    
}

/* MAIN */

// generate data
generateValues(generatedData, SEGMENT_COUNT);
console.log("--- generated values: ", generatedData);

/* do other stuff in between              */
/* e.g. arbitratily modify generated data */
modifyGeneratedValues();
console.log("--- modified generated values: ", generatedData);

// only the first Div should be marked
highlightFilteredDivItems(generatedData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="item" data-sample1="2" data-muster1="3">Div 1</div>
<div class="item" data-sample1="3" data-muster1="3">Div 2</div>
<div class="item" data-sample1="4" data-muster1="3">Div 3</div>