简体   繁体   English

Datatables jquery插件 - 通过多个表循环过滤

[英]Datatables jquery plugin - cycling through multiple tables with filtering

I have a page that dynamically fills the datatables jquery plugin from datatables.net. 我有一个页面,动态填充datatables.net的datatables jquery插件。

On the page there are 4 tables generated and this is performed by .each jquery call. 在页面上有4个表生成,这是由.each jquery调用执行的。 I have setup an array to hold these datatables: 我已经设置了一个数组来保存这些数据表:

oTable[x].  

Once the data has filled the datatables, I have to instantiate the filtering by calling this function: 一旦数据填满了数据表,我就必须通过调用这个函数来实例化过滤:

oTable[x].fnFilter("^" + $(this).val() + "$", 8, true);

I have a counter running that counts each time a new table is created. 我有一个计数器运行,每次创建一个新表时都会计数。 I was trying to use a for loop to handle the filtering by doing so: 我试图使用for循环来处理过滤:

 for (var x = 1; x < counter; x++) {
    $("#selectMetricImperial" + x).live('change', function () { oTable[x].fnFilter("^" + $(this).val() + "$", 8, true); });
  } 

Unfortunately, It doesn't seem to work. 不幸的是,它似乎没有用。 When I execute the filtering via the dropdown it goes to the .live line but executes everyone as x=1. 当我通过下拉列表执行过滤时,它会转到.live行,但会执行每个人x = 1。 Something else is wrong I think. 我认为还有别的错。

This code does work but is not dynamic: 此代码确实有效,但不是动态的:

$("#selectMetricImperial" + 1).live('change', function () { oTable[1].fnFilter("^" + $(this).val() + "$", 8, true); });
$("#selectMetricImperial" + 2).live('change', function () { oTable[2].fnFilter("^" + $(this).val() + "$", 8, true); });
$("#selectMetricImperial" + 3).live('change', function () { oTable[3].fnFilter("^" + $(this).val() + "$", 8, true); });
$("#selectMetricImperial" + 4).live('change', function () { oTable[4].fnFilter("^" + $(this).val() + "$", 8, true); }); 

What I would like to have happen is generate the above code dyanmically based on the counter passed in at the end of the .each loop that creates the datatables. 我想要发生的是根据在创建数据表的.each循环末尾传入的计数器动态生成上面的代码。 For instance, if the counter=5 then 5 of the above statements would be generated so that the filtering works for each dropdown. 例如,如果counter = 5,那么将生成上述语句中的5个,以便过滤适用于每个下拉列表。

I hope I am explaining this properly. 我希望我能正确解释这一点。 If not, please ask questions and I will tune my question to better address my issue. 如果没有,请提出问题,我会调整我的问题以更好地解决我的问题。

Thanks for your help guys! 谢谢你的帮助!

I suggest you use a class selector. 我建议你使用类选择器。 For each select, add a class, say "selectchange". 对于每个选择,添加一个类,比如说“selectchange”。

Then use just one selector, instead of a loop: 然后只使用一个选择器,而不是循环:

$(".selectchange").on("change", 
    function() 
    { 
        var id = this.id.substring("selectMetricImperial".length);
        alert(id);
        alert($(this).val());
        oTable[id].fnFilter("^" + $(this).val() + "$", 8, true);
    }
);

See this jsfiddle: http://jsfiddle.net/KAqfk/ 看到这个jsfiddle: http//jsfiddle.net/KAqfk/

If you want to get it to work your way, you'll need to pass in "x" as an argument: 如果你想让它按照自己的方式工作,你需要传入“x”作为参数:

for (var x = 1; x < counter; x++) {
    $("#selectMetricImperial" + x).live('change', x, 
        function (e) { oTable[e.data].fnFilter("^" + $(this).val() + "$", 8, true); });
  }

In your original code, you use x in the inline function, but when the function is executed the value of x is unknown (it is probably the value when the for loop exited, unless you use x elsewhere). 在原始代码中,您在内联函数中使用x,但是在执行函数时,x的值是未知的(它可能是for循环退出时的值,除非您在其他地方使用x)。 I know this can be confusing but you have to be careful with inline functions. 我知道这可能会令人困惑,但你必须小心内联函数。 The code $("#selectMetricImperial" + x) knows what x is. 代码$("#selectMetricImperial" + x)知道x是什么。 But the inner function is not called until the change event is fired, when x could be anything. 但是在更改事件被触发之前,内部函数不会被调用,而x可以是任何东西。

bqb BQB

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

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