如何基于 JSON 更改显示值/颜色 td

发布于 2025-01-09 09:13:21 字数 1255 浏览 0 评论 0原文

我正在开发一个应用程序,通过 ajax 调用获取 json。此 json 包含对象,您可以在其中获取每个分机的特定状态代码(1 = 在线,2,正在响铃,3 = 忙碌)

我如何确保返回的值转换为文本(最好使用不同颜色的文本) )

所以当我得到 1 回来时,我希望它显示在线,并带有 2 环等

    $.ajax({
    type:'GET',
    url: url,
    dataType: 'json',
    error: function(jqXHR, exception) {ajax_error_handler(jqXHR, exception);},
    success: function(data){
    
    // console.log(JSON.parse(data.responseText));
    // console.log(JSON.parse(data.responseJSON));
        

        console.log(data['entry']);
            var event_data = '';
            $.each(data.entry, function(index, value){
             /*  console.log(data['entry']);*/
                            
                event_data += '<tr>';
                event_data += '<td>'+value.extension+'</td>';
                event_data += '<td>'+value.status+'</td>';
                <!--event_data += '<td>'+value.registration+'</td>';-->
                event_data += '</tr>';
                    
            });
            $("#list_table_json").append(event_data);
        },
    error: function(d){
        /*console.log("error");*/
        alert("404. Please wait until the File is Loaded.");
    }

});

提前致谢!

I'm working on an app where I get a json via an ajax call. This json contains objects where you get a certain status code per extension (1 = online, 2, is ringing, 3 = busy)

How can I ensure that the value that I get back is converted to the text (preferably with a different color of the )

So when I get a 1 back I want it to show Online, and with a 2 Ring etc

    $.ajax({
    type:'GET',
    url: url,
    dataType: 'json',
    error: function(jqXHR, exception) {ajax_error_handler(jqXHR, exception);},
    success: function(data){
    
    // console.log(JSON.parse(data.responseText));
    // console.log(JSON.parse(data.responseJSON));
        

        console.log(data['entry']);
            var event_data = '';
            $.each(data.entry, function(index, value){
             /*  console.log(data['entry']);*/
                            
                event_data += '<tr>';
                event_data += '<td>'+value.extension+'</td>';
                event_data += '<td>'+value.status+'</td>';
                <!--event_data += '<td>'+value.registration+'</td>';-->
                event_data += '</tr>';
                    
            });
            $("#list_table_json").append(event_data);
        },
    error: function(d){
        /*console.log("error");*/
        alert("404. Please wait until the File is Loaded.");
    }

});

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笛声青案梦长安 2025-01-16 09:13:21

我已经更改了代码

function get_blf() {
$.ajax({
类型:'获取',
网址: 网址,
数据类型:'json',
错误:函数(jqXHR,异常){ajax_error_handler(jqXHR,异常);},
成功: function(data){

    $.each(data.entry, (index, value) => {
        const tableRow = document.createElement('tr');

        const tdExtension = document.createElement('td');
        extension.textContent = value.status;

        const tdStatus = document.createElement('td');
        if (value.status == 3) status.textContent = 'Busy';
        if (value.status == 2) status.textContent = 'Ringing';
        if (value.status == 1) status.textContent = 'Online';
        tdStatus.classList.add(`status-${value.status}`);

        tableRow.appendChild(tdExtension);
        tableRow.appendChild(tdStatus);
        $('#list_table_json').append(tableRow);
    }
});

}

}

并添加 css,但现在我无法获取任何值。但现在我无法取回任何值。 (抱歉,我对 javascript 还很陌生)

I have change the code

function get_blf() {
$.ajax({
type:'GET',
url: url,
dataType: 'json',
error: function(jqXHR, exception) {ajax_error_handler(jqXHR, exception);},
success: function(data){

    $.each(data.entry, (index, value) => {
        const tableRow = document.createElement('tr');

        const tdExtension = document.createElement('td');
        extension.textContent = value.status;

        const tdStatus = document.createElement('td');
        if (value.status == 3) status.textContent = 'Busy';
        if (value.status == 2) status.textContent = 'Ringing';
        if (value.status == 1) status.textContent = 'Online';
        tdStatus.classList.add(`status-${value.status}`);

        tableRow.appendChild(tdExtension);
        tableRow.appendChild(tdStatus);
        $('#list_table_json').append(tableRow);
    }
});

}

}

and add the css, but now i can't get any values back. but now i can't get any values back. (sorry I'm fairly new to javascript)

自演自醉 2025-01-16 09:13:21

请使用 DOM API

获取颜色的一种方法是使用 CSS 类来表示状态:

// js
...
$.each(data.entry, (index, value) => {
    const tableRow = document.createElement('tr');

    const tdExtension = document.createElement('td');
    extension.textContent = value.extension;

    const tdStatus = document.createElement('td');
    if (value.status == 3) status.textContent = 'Busy';
    if (value.status == 2) status.textContent = 'Ringing';
    if (value.status == 1) status.textContent = 'Online';
    tdStatus.classList.add(`status-${value.status}`);

    tableRow.appendChild(tdExtension);
    tableRow.appendChild(tdStatus);
    $('#list_table_json').append(tableRow);
});

...
// css
.status-1 {
    color: green;
}
.status-2 {
    color: red;
}
.status-3 {
    color: orange;
}

Please use the DOM API

One way of getting colors would be to use CSS classes for the status:

// js
...
$.each(data.entry, (index, value) => {
    const tableRow = document.createElement('tr');

    const tdExtension = document.createElement('td');
    extension.textContent = value.extension;

    const tdStatus = document.createElement('td');
    if (value.status == 3) status.textContent = 'Busy';
    if (value.status == 2) status.textContent = 'Ringing';
    if (value.status == 1) status.textContent = 'Online';
    tdStatus.classList.add(`status-${value.status}`);

    tableRow.appendChild(tdExtension);
    tableRow.appendChild(tdStatus);
    $('#list_table_json').append(tableRow);
});

...
// css
.status-1 {
    color: green;
}
.status-2 {
    color: red;
}
.status-3 {
    color: orange;
}
淡笑忘祈一世凡恋 2025-01-16 09:13:21

我终于让脚本工作了。我现在正在尝试构建轮询,但是我看到 ajax 调用再次执行并获取了数组。但是,该表没有刷新,而是添加了一个新表,有谁知道这个问题的解决方案吗?

我现在用于重新轮询的代码是

function repoll(poll_request, poll_interval, param=null) {

if (poll_interval != 0) {
                
    if (window.timeoutPool) {
        window.timeoutPool.push(setTimeout(function() { poll_request(param); }, poll_interval));
    }
    else {
        setTimeout(function() { poll_request(param); }, poll_interval);
    }
}
else {
    log_msg('Poll cancelled.');
}

}


tableRow.appendChild(tdExtension);
        tableRow.appendChild(tdNr);
        tableRow.appendChild(tdStatus);
        
        $('#list_table_json').append(tableRow);
        
            });     
repoll(get_blf, poll_interval_blf);

I finally got the script working. I am now trying to build in a polling, however I see that the ajax call is executed again and the array is fetched. However, the table is not refreshed but a new table is added, does anyone know a solution for this?

code I'm using now for the repoll is

function repoll(poll_request, poll_interval, param=null) {

if (poll_interval != 0) {
                
    if (window.timeoutPool) {
        window.timeoutPool.push(setTimeout(function() { poll_request(param); }, poll_interval));
    }
    else {
        setTimeout(function() { poll_request(param); }, poll_interval);
    }
}
else {
    log_msg('Poll cancelled.');
}

}


tableRow.appendChild(tdExtension);
        tableRow.appendChild(tdNr);
        tableRow.appendChild(tdStatus);
        
        $('#list_table_json').append(tableRow);
        
            });     
repoll(get_blf, poll_interval_blf);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文