AJAX

Dialog

사라링 2012. 6. 19. 14:22

호출부

$("#popupBtn").on("click", function(){
            Page.popup({
                url : "/common/popup/zipPop",
                width : "500",
                height : "400",
                title :"우편번호 조회",
                close : function(result){   
                    if(result.addr1 !=""&&result.addr1 !=null){
                        $("#addr1_std").val(result.addr1);
                        $("#zip_std").val(result.zipcode);
                    }
                }
            });

        });       



구현부

Page.popup = function(opt){
    var dlgid = "dlg"+Math.floor(Math.random()*1000000);
    opt.data = opt.data ? opt.data : {};
    opt.data.dlgid = dlgid;
    var modal = false;
    if(opt.modal){
        modal = opt.modal;
    }
    $.ajax({
        url : opt.url,
        data : opt.data,
        async : false,
        type : "post",
        dataType : "html",
        success : function(result){
            var popup = $("<div/>").attr("id", dlgid);
            $("body").append(popup);
           
            popup.html(result);
            popup.dialog({
                title: opt.title,
                resizable: true,
                modal: modal,
                width: opt.width ? opt.width : 300,
                height: opt.height ? opt.height : 300
            }).on("dialogclose", function(e, data){
                $(this).remove();
                if( opt.close ) opt.close(data);
                popup.dialog("destroy");
            });
        },
        error : function(res){
            Page.parseError(res.responseText);
        }
    });
};


    $("#"+dlgid).trigger("dialogclose", {
                zipcode: $(this).data("zipcode"),
                addr1 : $(this).data("addr")
            });


아작스 호출

$.ajax({
                type : "POST",
                url : "/common/popup/zipListPop",
                dataType : "json",
                data : { sch : $("#iSearch").val()},
                success : function(data){
                    $("#ziptbl tbody").html("");
                    if(data.length > 0){
                        $.each(data, function(idx, row){
                            var tr = $("<tr><td/><td class='txt_l'/></tr>");
                            var data = {
                                zipcode : row.zipcode,
                                addr : row.addr
                            };
                            tr.data(data).addClass("zipcodeList");
                            tr.find("td:eq(0)").text(row.zipcode);
                            tr.find("td:eq(1)").text(row.addr);
   
                            $("#ziptbl tbody").append(tr);
                        });
                    }else{
                        $("#ziptbl tbody").html("<tr><td colspan='2' height='40'>검색된 자료가 없습니다.</td></tr>");
                    }
                }