jquery.each()过滤器基于是否与xml中的类别类型匹配 [英] jquery .each() filter based on if matches category type in xml

查看:72
本文介绍了jquery.each()过滤器基于是否与xml中的类别类型匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下功能来显示在上一个功能中收集的每个程序.

I have the below function to display each program collected in the previous function.

它工作正常,我希望能够对其进行优化,以便在单击按钮时执行each().仅适用于具有特定类别(例如"Jeu")的那些程序

Its working fine, I want to be able ot refine it so that when a button is clicked it does the each(). for only those programs which have a certain category eg 'Jeu'

关于如何优化each()的任何想法?

Any ideas on how I can refine this each() ?

function displayChannels()
{
    $.each(channel_list, function(index, channel) {
        //Make the code for the channel
        var code_channel = "<li>"
        +"<img src='"+channel._icon+"' alt='"+channel._name+"' title='"+channel._name+"'/>"
        +"<p>"+channel._name+"</p>"
        +"</li>";
        //Display it
        $("#channel-list").append(code_channel);

        //Open a list for the channel's program list
        var code_prog_list = "<ul class='channel_row'>";
        //Add each program in it as a li each time

 // DO I NEED TO PUT A FILTER HERE FOR PROGRAMS WHICH MATCH THE CATEGORY 'JEU'
        $.each(channel._programs, function( index, programme ) {
            code_prog_list+="<div class='program'>"
            +"<div class='program_bg'><img src='"+programme._img+"' alt=' '/></div>"
            +"<div class='programInner'>"
            +"<div class='channelIcon'><img src='logos/logo"+programme._icon+".png'/></div>"
            +"<div class='textContainer'><h4>"+programme._title+"</h4>"
            +"<strong><p>"+programme._hour+":"+programme._minutes+"</strong> | "+programme._cat+" ("+programme._duree+"mn)"+"</p></div>" // end of text container
            +"</div>"// end of programInner
            +"<div class='desc'>"+programme._desc+"</div>"
            +"</div>"; // end of div program 
        });

        //Display it
        $("#main").append(code_prog_list);
    });
}

推荐答案

您可以检查函数中类别是否为'JEU'并返回true:

You can check if the category is 'JEU' inside the function and return true:

if(programme._cat !== 'JEU') {
    return true;
}

根据jQuery.each的文档

According to the documentation for jQuery.each,

通过使回调函数返回false,可以在特定的迭代中中断$ .each()循环.返回非false与for循环中的continue语句相同;它将立即跳至下一个迭代.

We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.

因此,基本上,通过在.each内的匿名函数中返回true,可以跳过不包含"JEU"作为其类别的程序.

So basically by returning true in the anonymous function inside .each you're skipping programmes that don't have 'JEU' as their category.

这篇关于jquery.each()过滤器基于是否与xml中的类别类型匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆