javascript - AJAX:使用数组值的背景颜色更改功能不起作用?

标签 javascript php jquery arrays ajax

我会尝试尽可能简单地描述我的问题。我有一个 AJAX 调用来获取 Json_encoded 数组。随着“aht_value”被检索,我得到了一个彩色背景 从绿色绘制到红色(我正在制作热图)。我的值正在正确输出。

问题: 我的“colorMe”功能。我不确定我做错了什么,或者我是否将所有函数放在错误的位置,因为我正在“成功”内和 FOR 循环之后执行所有操作,如下所示。但是,如果我将“min”替换为 100,将“max”替换为 1800,则会显示颜色。

提前致谢。

来自 show.php 的数组:

[{
   "username":"OXGOR",
   "aht_value":"241",
   "station":"B20"
  },
  {
   "username":"AISAI2",
    "aht_value":"199",
   "station":"B21"
  },
  {
   "username":"CAPAP3",
   "aht_value":"NA",
   "station":"B10"
  }]

map.php - JS 部分

<script type="text/javascript">
        $(document).ready(function() {
                $('#aht').click(function(){
                    $.ajax({
                    type:"GET",
                    url : "show_aht.php",
                    data:{ } , // do I need to pass data if im GET ting?
                    dataType: 'json',
                    success : function(data){

                        var min = data.reduce(function(prev, curr) {
                                return isNaN(curr['aht_value']) || prev < curr['aht_value'] ? prev : curr['aht_value'];
                                }, 1000000);

                                alert(min); //not returning the correct number

                                var max = data.reduce(function(prev, curr) {
                                  return isNaN(curr['aht_value']) || prev > curr['aht_value'] ? prev : curr['aht_value'];
                                }, -1000000);    

                                alert(max); //nor returning the correct number

                                //function for calculation of background color depending on aht_value               
                                function conv(x){
                                    return Math.floor((x - min) / (max - min) * 255);
                                }

                                //function for background color
                                function colorMe(v){
                                    return "rgb(" + conv(v) + "," + (255-conv(v)) + ",0)";
                                }

                        //going through all DIVs only once with this loop
                            for(var i = 0; i < data.length; i++) { // loop over results
                            var divForResult = $('#desk_' + data[i]['station']); // look for div for this object
                            if(divForResult.length) { // if a div was found
                                divForResult.html(data[i]['aht_value']).css("background-color", colorMe(data[i]['aht_value']));
                            }//end if
                            }//end for  
                    }//end success
                });//end ajax   
              });//end click
            });//end rdy
</script>

show.php:我使用json_encode

$result = array();
    foreach ($memo as $username => $memodata) {
    if (in_array($username, array_keys($user))) {
    // Match username against the keys of $user (the usernames) 
    $userdata = $user[$username];
    //if AHT is null give N/A as value
    if (is_null($memodata['aht_value'])) {
        $result[] = array( 'username'  => $userdata['username'],
                                             'aht_value' => 'NA',
                                             'station'  => $userdata['station']
                                            );
    }//end inner if 
    //else give the actual value of AHT without the decimals
    else {
        $result[] = array( 'username'  => $userdata['username'],
                           'aht_value' => substr($memodata['aht_value'],0,-3),
                           'station'   => $userdata['station']
                                            );
echo json_encode($result);

如果我在 colorMe 函数中手动将最小值设置为 100,将最大值设置为 1800,则此方法有效: what is suposd to look like

每当我使用最小和最大计算时,这都会返回错误的颜色,如下所示 not giving right colors

最佳答案

您的数组在第一个对象后面缺少逗号。

关于javascript - AJAX:使用数组值的背景颜色更改功能不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27107563/

相关文章:

javascript - 在每个 Ajax.BeginForm 上传递默认的 AjaxOption

php - 更新时Mysql语法错误

javascript - 如何使用 angular 2 或 javascript 从客户端机器获取日期和时间格式?

jquery - nvd3 ghaph 中的点不清晰可见

javascript - 使用逻辑与的箭头函数隐式返回

javascript - 从按钮中间打开下拉菜单

javascript - 正则表达式匹配分隔符后的任何内容

php - 按其子数组的大小对多维数组进行排序

php - PHP 中是否有像 ASP.Net 中那样的 httphandlers

Javascript 单击按钮以名称开头