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

标签 javascript jquery

我有以下带有 JSFiddle 的代码,用于相同的 here :

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>

最佳答案

您可以使用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);

关于javascript - 仅使用一个警报打印所有记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39044894/

相关文章:

javascript - 中止 jquery 中的所有先前请求

javascript - 动态添加表单字段,jquery 不发布

javascript - 如果搜索字符串不匹配任何节点,jstree 将显示所有节点

javascript - Dreamweaver CC 中奇怪的 Javascript 验证

javascript - 使用 jQuery 修改 CSS 类的名称 - 无法加载

javascript - 在显示 div 时更改背景颜色

jquery - 从 Python 解析 jQuery 表单数据

javascript - 如何通过jQuery用replace方法更新textarea

javascript - HTML 和 CSS 更改整个网页上的日期格式

javascript - 向左浮动菜单,我哪里错了?