仅使用一个警报打印所有记录

时间:2016-08-19 17:40:21

标签: javascript jquery

对于相同的here我有以下代码和JSFiddle:

var result = [ 
    {"Id": 10004, "PageName": "club"}, 
    {"Id": 10040, "PageName": "qaz"}, 
    {"Id": 10059, "PageName": "jjjjjjj"}
];

$.each(result, function(i, item) {
    alert(result[i].PageName);
});

为了查看所有结果,我必须在警报窗口中单击“确定”两次。如何仅使用一个警报显示内容?

    //f1.js ---

    var PatientReviewPage = (function () {
        var cls = function () { }

        var self = cls.prototype;

        self.urlKey = "showmrn";

        self.getData = function (mrn_) {

            /*
             if (isEmpty(mrn_)) { alert("Invalid mrn in getData()"); return false; }
             
             // Lookup the AJAX web service URL
             var url = regman.getWebServiceURL(self.urlKey);
             if (isEmpty(url)) { alert("Invalid URL in getData()"); return false; }
             
             
             var ajaxRequest = jQuery.ajax({
             //beforeSend: TODO: show spinner!
             data: {
             registry_id: regman.registry.id,
             mrn: mrn_
             },
             dataType: "json",
             method: "GET",
             url: url
             })*/

            //Some code related to ajax reques
            this.done(function (data_, textStatus_, jqXHR_) {

                // Validate the web service and retrieve the status.
                if (typeof (data_) === "undefined" || data_ === null) {
                    alert("Invalid data returned from web service");
                    return false;
                }
                if (isEmpty(data_.webservice_status) || isEmpty(data_.webservice_status.status)) {
                    alert("Invalid web service status");
                    return false;
                }
                if (data_.webservice_status.status != "SUCCESS") {
                    alert(data_.webservice_status.message);
                    return false;
                }

                self.processdataDocuments(data_.data_document_list);
            });
            this.fail(function (jqXHR_, textStatus_, errorThrown_) {
                alert("Error in getData(): " + errorThrown_);
                return false;
            });
        };

        // Initialize the page
        self.initialize = function () {
            var mrn = regman.selectedData["mrn"];

            if (isEmpty(mrn)) {
                alert("Invalid MRN provided by calling page");
                return false;
            }

            self.getData(mrn);
        };

        self.processdataDocuments = function (collection_) {

            if (isEmpty(collection_) || collection_.length < 1) {

                // Populate the count and exit
                jQuery("#nlpDocumentCount").html("(0)");
                return true;
            }
            var source =
                    {
                        localdata: collection_,
                        datatype: "array"
                    };
            var dataAdapter = new $.jqx.dataAdapter(source, {
                loadComplete: function (data) { },
                loadError: function (xhr, status, error) { }
            });
            $("#nlpDocumentPanel").jqxGrid(
                    {
                        source: dataAdapter,
                        width: '1000',
                        height: 150,
                        columns: [
                            {
                                text: 'Type', datafield: 'nc_type'
                            },
                            {
                                text: 'SubType', datafield: 'nc_subtype'
                            },
                            {
                                text: 'Concept', datafield: 'concept_text'
                            },
                            {
                                text: 'Date', datafield: 'nc_dos'
                            }
                        ]
                    });
            // For getting the contents of a row, I am using jqxgrid approach as mentioned in their doc here :
            //    http://www.jqwidgets.com/getting-the-clicked-grid-row/           

            $("#nlpDocumentPanel").on('rowclick', function (event) {

                var row = event.args.rowindex;

                var datarow = $("#nlpDocumentPanel").jqxGrid('getrowdata', row);
                response = JSON.stringify(datarow, null, 10)
                //alert(jsonStringify); // This alert displays the JSON data in a formatted manner 
                var obj = jQuery.parseJSON(response);
                //alert("display Subtype "+obj.nc_subtype)  // Works fine

                self.mySubtype = obj.nc_subtype;
            });

        };

        //I added this line for the demo to show 'f2' accessing this property from 'f1'. You should remove it if copying this code into your application
        self.mySubtype = "Foo";

        return cls;
    })();

    var f1 = new PatientReviewPage();

//f2.js ---

    var DocumentDetailsDialog = (function () {
        var cls = function () { }

        var self = cls.prototype;

        alert(f1.mySubtype);

        /*this.getData = function (IDNumber_) {
         
         // some code will be here      
         
         var ajaxRequest = jQuery.ajax({
         // some code will be here
         })
         .done(function (data_, textStatus_, jqXHR_) {
         // some code will be here
         })
         .fail(function (jqXHR_, textStatus_, errorThrown_) {
         // some code will be here
         });
         };*/

        return cls;
    })();

    var f2 = new DocumentDetailsDialog();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

2 个答案:

答案 0 :(得分:3)

您可以使用map()join()并返回带有网页名称的字符串。

var result = [ 
    {"Id": 10004, "PageName": "club"}, 
    {"Id": 10040, "PageName": "qaz"}, 
    {"Id": 10059, "PageName": "jjjjjjj"}
];

var r = result.map(function(e) {
    return e.PageName;
}).join(' ');

alert(r);

答案 1 :(得分:0)

使用结果构建一个字符串并提醒字符串:

alert(result.map( _ => _.PageName).join(', '))