更新每个JSON值的HTML值

时间:2017-12-14 13:02:28

标签: jquery json ajax asp.net-mvc

我试图通过从控制器操作中获取JSON来更新视图中div的每个值。 我目前能够定位每个div,但我不知道如何读取每个JSON值。

这是我的div:

 <div class="progress" style="background: white" value="@item.CompletionRate"
     data-acr="@item.Acronym"></div>

这是我的Ajax获得JSON:

$.ajax({
    url: "/Home/ModelsUpdate",
    success: function(result) {
        console.log(result);
        $('.progress').each(function () {
            // JSON condition should be here
        });
        drawCircles();
    }
});

条件应该是这样的:

var acr= $(this).attr('acr'); //referring to .progress data-acr field
each(result){
    if(result.Acronym == acr){
        $(this).data('value', result.CompletitionRate)
    }

JSON看起来像这样:

[{"Name":"Raw To Common","Acronym":"RTC","Status":"Running","CompletionRate":11,"Started":"\/Date(1513220400000)\/","Ended":null},{"Name":"ePack","Acronym":"EPK","Status":"Not Started","CompletionRate":0,"Started":null,"Ended":null},{"Name":"Finished Goods Long Constrained","Acronym":"FLC","Status":"Not Started","CompletionRate":0,"Started":null,"Ended":null},{"Name":"Raw Material Long Term Constrained","Acronym":"RLC","Status":"Not Started","CompletionRate":0,"Started":null,"Ended":null},{"Name":"Finished Goods Long Term Unconstrained","Acronym":"FLU","Status":"Not Started","CompletionRate":0,"Started":null,"Ended":null},{"Name":"Finished Goods Short Term Constrained","Acronym":"FSC","Status":"Not Started","CompletionRate":0,"Started":null,"Ended":null},{"Name":"Finished Goods Short Term Unconstrained","Acronym":"FSU","Status":"Not Started","CompletionRate":0,"Started":null,"Ended":null},{"Name":"Raw Materials Long Term Unconstrained","Acronym":"RLU","Status":"Not Started","CompletionRate":0,"Started":null,"Ended":null},{"Name":"Finished Goods Long Term Fixed Supply","Acronym":"FLF","Status":"Not Started","CompletionRate":0,"Started":null,"Ended":null}]

我的控制器看起来像这样:

public JsonResult ModelsUpdate()
    {
        DateTime minDate = DateTime.Today;
        DayOfWeek todayDay = DateTime.Today.DayOfWeek;
        DateTime resultminDate = new DateTime();
        resultminDate = CalculateminDate(minDate,todayDay);


        var viewModel = CacheHelper.Current.Get(resultminDate);

        return Json(viewModel, JsonRequestBehavior.AllowGet);
    }

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
var result = [{
    "Name": "Raw To Common",
    "Acronym": "RTC",
    "Status": "Running",
    "CompletionRate": 11,
    "Started": "\/Date(1513220400000)\/",
    "Ended": null
  },
  {
    "Name": "ePack",
    "Acronym": "EPK",
    "Status": "Not Started",
    "CompletionRate": 0,
    "Started": null,
    "Ended": null
  },
  {
    "Name": "Finished Goods Long Constrained",
    "Acronym": "FLC",
    "Status": "Not Started",
    "CompletionRate": 0,
    "Started": null,
    "Ended": null
  }
]

$(".progress").each(function(element) {
  var acr = $(this).data('acr');
  var item = result.find(item => item.Acronym == acr);
  if (item) {
    $(this).attr("value", item.CompletionRate);
    $(this).text(item.Acronym + ' - ' + item.Status);
  } else {
    console.log(acr, "Not found.");
  }
});
&#13;
.progress {
  display: inline-block;
  width: 50px;
  height: 50px;
  border: 1px solid black;
  padding: 10px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="progress" style="background: white" value="0" data-acr="RTC"></div>
<div class="progress" style="background: white" value="0" data-acr="EPK"></div>
<div class="progress" style="background: white" value="0" data-acr="FLC"></div>
&#13;
&#13;
&#13;