简体   繁体   English

有条件地更改每个列表项的背景色JQuery Mobile

[英]Change each list item background color conditionally JQuery Mobile

I have developed an Android app, which consists in a listview of items populated from strings, which changes the color of each list row depending on a word coincidence. 我开发了一个Android应用程序,该应用程序包含一个由字符串填充的项的列表视图,该视图根据单词的重合度更改每个列表行的颜色。

Now i'm trying to develop same app, for the web. 现在,我正在尝试为网络开发相同的应用程序。 After being investigating, the best way I did found, was using JQuery Mobile. 经过调查,发现的最好方法是使用JQuery Mobile。

So, now I want to accomplish the same, a ListView that conditionally changes each list item background-color conditionally. 因此,现在我想完成同样的操作,一个ListView有条件地更改每个列表项的背景颜色。

After several days investigating and learning, I'm populating the list from a JSON, like you can see here in JSFiddle (This is what I've achieved until now, based on another JSFiddle I did found, because I had never used JQuery Mobile.) 经过几天的研究和学习,我从JSON填充了列表, 就像您在JSFiddle中看到的那样 (这是我到目前为止所实现的,基于我发现的另一个JSFiddle,因为我从未使用过JQuery Mobile 。)

//JSON goes above here

$(document).on("pageinit", "#info-page", function () { 
//set up string for adding <li/>
var li = "";
//container for $li to be added
$.each(info, function (i, name) {
    //add the <li> to "li" variable
    //note the use of += in the variable
    //meaning I'm adding to the existing data. not replacing it.
    //store index value in array as id of the <a> tag
    li += '<li><a href="#" id="' + i + '" class="info-go">' + name.Número + '<p>' + name.Origen + '</p></a></li>';'</a></li>';
});
//append list to ul
$("#prof-list").append(li).promise().done(function () {
    //wait for append to finish - thats why you use a promise()
    //done() will run after append is done
    //add the click event for the redirection to happen to #details-page
    $(this).on("click", ".info-go", function (e) {
        e.preventDefault();
        //store the information in the next page's data
        $("#details-page").data("info", info[this.id]);
        //change the page # to second page. 
        //Now the URL in the address bar will read index.html#details-page
        //where #details-page is the "id" of the second page
        //we're gonna redirect to that now using changePage() method
        $.mobile.changePage("#details-page");
    });

    //refresh list to enhance its styling.
    $(this).listview("refresh");
});
});

//use pagebeforeshow
//DONT USE PAGEINIT! 
//the reason is you want this to happen every single time
//pageinit will happen only once
$(document).on("pagebeforeshow", "#details-page", function () {
    //get from data - you put this here when the "a" wa clicked in the previous page
    var info = $(this).data("info");
    //string to put HTML in
    var info_view = "";
    //use for..in to iterate through object
    for (var key in info) {
        //Im using grid layout here.
        //use any kind of layout you want.
        //key is the key of the property in the object 
        //if obj = {name: 'k'}
        //key = name, value = k
        info_view += '<div class="ui-grid-a"><div class="ui-block-a"><div class="ui-bar field" style="font-weight : bold; text-align: left;">' + key + '</div></div><div class="ui-block-b"><div class="ui-bar value" style="width : 75%">' + info[key] + '</div></div></div>';
    }
    //add this to html
    $(this).find("[data-role=content]").html(info_view);
});

So, basically what I want is to change (if it is possible) the colour of each row, depending of the of the word under the row title (or any other variable I could include in the JSON, would only be three variables): This is what i have in Android, just to clarify what I want: 因此,基本上,我想要更改的是每行的颜色(取决于行标题下的单词的含义) (或我可以在JSON中包含的任何其他变量,只有三个变量):这就是我在Android中所拥有的,只是为了阐明我想要什么:

if (additiveslist.get(position).getOrigen().equals("Vegano")) {
        holder.relativeLayout.setBackgroundColor(0xB790D55D);
            }

    if (additiveslist.get(position).getOrigen().equals("Dudoso")) {
        holder.relativeLayout.setBackgroundColor(0x96F6B22D);
    }
    if (additiveslist.get(position).getOrigen().equals("No vegano")) {
        holder.relativeLayout.setBackgroundColor(0x84f51000);



    }

And this is how it looks like on Android App: 这是在Android App上的样子:

这就是Android App上的样子

Hope I explained well and someone can help me, because I am a complete beginner in JQuery Mobile (or maybe I did wrong choosing JQuery Mobile to do this kind of web app...) 希望我能讲得很好,并且有人可以帮助我,因为我是JQuery Mobile的完整初学者(或者也许我选择JQuery Mobile进行此类Web应用程序做错了...)

You can create CSS classes for each of the background colors eg: 您可以为每种背景色创建CSS类,例如:

.vegano {
    background-color: #ABDD87 !important;    
}
.dudoso {
    background-color: #F5CB98 !important;
}
.novegano {
    background-color: #F47D75 !important;
}

Then in the script when you are iterating the data, add the appropriate class to the anchor within the LI based on your criteria, eg: 然后,在脚本中进行数据迭代时,根据您的条件将适当的类添加到LI中的锚点,例如:

$.each(info, function (i, name) {
    //add the <li> to "li" variable
    //note the use of += in the variable
    //meaning I'm adding to the existing data. not replacing it.
    //store index value in array as id of the <a> tag        
    var bColor = "vegano";
    if (name.Origen == "Dudoso") {
        bColor = "dudoso";
    } else if (name.Origen == "No vegano") {
        bColor = "novegano";
    }
    li += '<li><a href="#" id="' + i + '" class="info-go ' + bColor + '">' + name.Número + '<p>' + name.Origen + '</p></a></li>';'</a></li>';
});

Here is your updated FIDDLE 这是您更新的FIDDLE

PS Once you start changing the backcolor, you might want to get rid of the default jQM text shadows with this CSS: PS一旦开始更改背景色,您可能希望使用以下CSS摆脱默认的jQM文本阴影:

#prof-list li a{
    text-shadow: none;
}

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

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