简体   繁体   English

Javascript / jQuery根据值更改表的单元格

[英]Javascript / jQuery changing cell of a table based on the value

Hi i have a javascript function that take the contents of a JSON file and the puts it in to a table, here is the code. 嗨,我有一个javascript函数,它接收JSON文件的内容并将其放入表中,这是代码。

$.getJSON("People.json",
    function(data) {
        $.each(data.People, function(i, PersonObj) {
            var Person = PersonObj[Object.keys(PersonObj)[0]];
            content = '<tr>';
            content += '<tbody>';
            content += '<td>' + Person.Op + ' </td>';
            content += '<td>' + Person.Name + ' </td>';
            content += '<td>' + Person.WorkHours + ' </td>';
            content += '<td>' + Person.Start + ' </td>';
            content += '<td>' + Person.End + ' </td>';
            content += '<td>' + Person.Clock + ' </td>';
            content += '<td>' + Person.OFF + ' </td>';
            content += '<td>' + Person.ON + ' </td>';
            content += '<td>' + Person.OUT + ' </td>';
            content += '</tr>';
            content += '</tbody>';
            content += '<p>';
            $(content).appendTo("tbody");
            console.log(Person);
        });
    });

However what i need is to some formatting via if statements so for example 但是我需要通过if语句进行某种格式设置,例如

normally i would use something like the badly put together bunch of If statements. 通常情况下,我会使用一些糟糕的If语句。

if (Person.clock !== false) {
document.getElementById('the cell where Person.Op is placed').style.backgroundColor = 'lime';
}
if (Person.Off !== false) {
document.getElementById('the cell where Person.Op is placed').style.backgroundColor = 'red';
}
if (Person.on !== false) {
document.getElementById('the cell where Person.Op is placed').style.backgroundColor = 'lime';
}
if (Person.out !== false) {
document.getElementById('the cell where Person.Op is placed').style.backgroundColor = 'red';
}

However i am struggling to find the best way of putting what i already have with the function for creating the table and putting the formatting from the If statements together what would be the best way to do this? 但是,我正在努力寻找将现有功能与创建表的功能以及If语句的格式放在一起的最佳方法,这是最好的方法?

Why not add a class to each such case. 为什么不为每个这种情况添加一个类。 something like this: 像这样的东西:

       $.getJSON("People.json",
            function(data) {
                $.each(data.People, function(i, PersonObj) {
                    var Person = PersonObj[Object.keys(PersonObj)[0]];
                    content = '<tr>';
                    content += '<td>' + Person.Op + ' </td>';
                    content += '<td>' + Person.Name + ' </td>';
                    content += '<td>' + Person.WorkHours + ' </td>';
                    content += '<td>' + Person.Start + ' </td>';
                    content += '<td>' + Person.End + ' </td>';
                    content += '<td class="'+(Person.clock? 'hasClock' : '') +'">' + Person.Clock + ' </td>';
                    content += '<td>' + Person.OFF + ' </td>';
                    content += '<td>' + Person.ON + ' </td>';
                    content += '<td>' + Person.OUT + ' </td>';
                    content += '</tr>';
                    $(content).appendTo("tbody");
                    console.log(Person);
                });
            });

and then add a class to your css: 然后向您的CSS添加一个类:

.hasClock{ 
    background-color :lime;
}
$.getJSON("People.json",
                function(data) {
                    $.each(data.People, function(i, PersonObj) {
                        var Person = PersonObj[Object.keys(PersonObj)[0]];
                        content = '<tr>';
                        content += '<tbody>';
                        content += '<td class="op ' + Person.Op + '">' + Person.Op + ' </td>';
                        content += '<td>' + Person.Name + ' </td>';
                        content += '<td>' + Person.WorkHours + ' </td>';
                        content += '<td>' + Person.Start + ' </td>';
                        content += '<td>' + Person.End + ' </td>';
                        content += '<td>' + Person.Clock + ' </td>';
                        content += '<td>' + Person.OFF + ' </td>';
                        content += '<td>' + Person.ON + ' </td>';
                        content += '<td>' + Person.OUT + ' </td>';
                        content += '</tr>';
                        content += '</tbody>';
                        content += '<p>';
                        $(content).appendTo("tbody");
                        console.log(Person);
                    });
                });

Add class to your td's with the value. 使用该值将类添加到您的td中。 If you have definite values coming up, then you can create css selectors for that with the CSS properties you want. 如果您要确定的值,则可以使用所需的CSS属性为其创建css选择器。

Suppose you had value of op as someOpValue You could write your css like foloowing. 假设您具有op的值作为someOpValue,则可以像下面那样写css。

td.op.someOpValue{
    background : red;
}

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

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