简体   繁体   English

动态jQuery对话框从循环中传递JSON对象

[英]Dynamic jQuery Dialog passing JSON object from within loop

I'm wondering if there is a better (cleaner?) method than my current implementation. 我想知道是否有比当前实现更好的方法(更清洁?)。 I'm currently encoding a PHP SimpleXMLObject (USPS Tracking API) to JSON and looping through said JSON object via Javascript to operate the front-end. 我目前正在将PHP SimpleXMLObject (USPS跟踪API)编码为JSON,并通过Javascript遍历所述JSON对象以操作前端。

Examples from my current implementation below: 下面是我当前的实现示例:

Function to display dialog implemented anonymously outside of .ready() : 显示在.ready()外部匿名实现的对话框的函数:

var moreInfo_popup = function(i) {
$('#moreinfo'+i).dialog({
        modal:false,
        autoOpen:false,
        height:555,
        title: 'Detailed View',
        width:500,
        draggable:false,
        buttons: {
            Ok: function(){
                $(this).dialog("close");
        }
    }
    });

        $('#moreinfo'+i).dialog('open');

        }

Main loop for displaying Tracking ID, Most Recent Event, and Mail Class for each API response- I'm currently generating a content div appended to #modal_container , then calling moreInfo_popup() inline via <input onClick=""> : 显示每个API响应的跟踪ID,最新事件和邮件类的主循环-我当前正在生成附加到#modal_container的内容div,然后通过<input onClick="">内联调用moreInfo_popup()

for(var key in obj) {
                    if(obj.hasOwnProperty(key)) {
                    if(key % 2 === 0) {
                    $('#page-nav').append("<div id=\"results_table\"><table class=\"data_table\"id=\"data_table_id\"border=\"0\"width=\"60%\"align=\"center\"><tr><td align=center width=20%>"+obj[key].TrackInfo.Attributes.ID+"</td><td align=\"center\"width=\"35%\">"+obj[key].TrackInfo.StatusSummary+"</td><td align=\"center\"width=\"20%\">"+obj[key].TrackInfo.Class+"</td><td align=\"center\"><input type=\"button\"class=\"moreInfo\"value=\"Detail\"id=\"_buttonMoreInfo\"onClick=\"moreInfo_popup("+key+")\"></td></tr></table></div>");

                    $('#modal_container').append("<div id=\"moreinfo" + key + "\"><table><tr><td>" + obj[key].TrackInfo.Attributes.ID +"</td></tr></table>").hide();
                  }
                else {
                  $('#page-nav').append("<div id=\"results_table\"><table class=\"data_table_even\" id=\"data_table_id\" border=0 width=60% align=center><tr><td align=center width=20%>" 
                                      + obj[key].TrackInfo.Attributes.ID + "</td><td align=center width=35%>" + obj[key].TrackInfo.StatusSummary + "</td><td align=center width=20%>" 
                                      + obj[key].TrackInfo.Class + "</td><td align=\"center\"><input type=\"button\" value=\"Detail\" class=\"moreInfo\" id=\"_buttonMoreInfo\"onClick=\"moreInfo_popup("+key+")\"></td></tr></table></div>");

                  $('#modal_container').append("<div id=\"moreinfo" + key + "\"><table><tr><td>" + obj[key].TrackInfo.Attributes.ID +"</td></tr><tr><td> <button>OK</button></td></tr></table>");


                }

            }

      $("#page-nav td:contains('undefined')").html("Invalid");    
  }

As I'm sure you can see, this feels like an incredibly tedious way of accomplishing the desired outcome, to which there is surely a better alternative. 正如您确定的那样,这确实是一种令人难以置信的单调乏味的方法,可以实现预期的结果,当然还有更好的选择。 As a newcomer to JavaScript/jQuery, I've done plenty of searching on this subject, but haven't really understood much of what I found- If indeed I was asking the right questions in the first place. 作为JavaScript / jQuery的新手,我已经在这个主题上进行了大量的搜索,但是并没有真正理解我所发现的东西-如果确实我是在问正确的问题。

I think you can use angular and data bind: 我认为您可以使用角度和数据绑定:

So you can just use a directive and automatically bind your JSON object from the server side easily to the html elements. 因此,您只需使用一条指令,即可将服务器端的JSON对象自动轻松地绑定到html元素。

However you should start studying angular. 但是,您应该开始研究角度。

you can start looking for your elegant way of doing stuffs here: https://docs.angularjs.org/tutorial/step_04 您可以在这里开始寻找做事的优雅方式: https : //docs.angularjs.org/tutorial/step_04

I hope it was useful. 我希望它是有用的。

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

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