添加第二个查找现有的ajax调用

时间:2014-03-01 17:09:09

标签: jquery ajax

我有以下功能,它在数据库中查找文档并循环结果以形成链接列表。我需要添加另一个嵌套查找来查找DATE_INPUT字段 - 它位于另一个表中 - 基于CNT_DOCUMENT_ID字段,将其添加到结果中,然后按降序对它们进行排序以查找添加的最后三个文档。这将需要与Payload和Params进行另一个API调用,但我不确定如何进行第二次调用并将结果附加到第一次查找。这是代码:

function getNewsDocuments(strCategoryID) {
    getDomainRootCat(); //looks up locreg
    var CategoryId = "";
    CategoryId = strCategoryID;

    var strPayload = "<Parameters><RequestContext><RequestUserId>ML.BASE.USR.Administrator</RequestUserId><RequestLocaleId>" + locreg + "</RequestLocaleId><RequestRegionId>" + locreg + "</RequestRegionId></RequestContext>" +
        "<Payload1>" +
        "<USR_LMS_USER_ID>ML.BASE.USR.Administrator</USR_LMS_USER_ID><CNTCTGY_CATEGORY_ID>" + CategoryId + "</CNTCTGY_CATEGORY_ID><strIncludeSubCategories>T</strIncludeSubCategories>" +
        "<SearchType>ML.BASE.DV.SearchAllWords</SearchType><TABLE_NAME>CONTENT_SEARCH</TABLE_NAME><CNT_CONTENT_TYPE_ID>ML.BASE.DOCUMENT</CNT_CONTENT_TYPE_ID>" +
        "</Payload1></Parameters>";  

    var strParams = "strCall=AdHocCall&strClassName=" + escape('BusinessLogic.ContentManagement') + "&strMethodName=GenericSearch&strPayload=" + escape(strPayload);
    var list = "<ul style=\"list-style: none; text-indent: -1.2em;\" class=\"newsupdates-list\" id=\"newsupdates-" + strCategoryID + "\">";

    $.ajax({
      type: "POST",
      async: false,
      data: strParams,
      url: "/AJAXUtilities.aspx",      
      success: function(msg){
        msg = $.createXMLDocument(msg);      

        $(msg).find('CONTENT_SEARCH').each(function(){

            if ($(this).find("CNT_AVAILABLE").text() == "T") {
            var objid = $(this).find("CNT_CONTENT_ID").text();
            var title = $(this).find("CNT_TITLE").text();

            if (title.length >= 40){
            var trimtitle = title.substring(0,39);
            list = list.concat("<li><a href=\"/MyOptions.aspx?id=" + objid + "\">" + trimtitle +"...</a></li><hr>");    
            }else{
            list = list.concat("<li><a href=\"/MyOptions.aspx?id=" + objid + "\">" + title +"</a></li><hr>");
            }

            }
        });

      }
    });

    list = list.concat("</ul>");
    list = list.concat("<br /><a href=\"\">View previous updates>></a>");   
    $("#newscatlist").append(list);
    return list;

}

更新:我能够看一下从API调用创建的XML,并且我可以使用顺序ID号,而不是进行第二次调用来获取日期。我一直试图让降序排序工作,但似乎无法做到正确。这是更新的ajax调用:

$.ajax({
  type: "POST",
  async: false,
  data: strParams,
  url: "/Kview/CustomCodeBehind/Base/Utilities/AJAXUtilities.aspx",      
  success: function(msg){
    msg = $.createXMLDocument(msg);      
    var li_count = 0;
    $(msg).find('CONTENT_SEARCH').each(function(){
        if ($(this).find("CNT_AVAILABLE").text() == "T") {
            var seq = $(this).find("CNT_SEQ_ID").text();
            seq.sort(function(a, b){
                return (parseInt($(a).node("CNT_SEQ_ID")) - parseInt($(b).node("CNT_SEQ_ID")));
                seq.each(function(){
                    var objid = $(this).find("CNT_CONTENT_ID").text();
                    var title = $(this).find("CNT_TITLE").text();
                    if (title.length >= 40){

                    var trimtitle = title.substring(0,39);
                    list = list.concat("<li><a href=\"/Kview/CustomCodeBehind/Base/Content/MyOptions.aspx?id=" + objid + "\">" + trimtitle +"...</a></li><hr>");    
                    }else{
                    list = list.concat("<li><a href=\"/Kview/CustomCodeBehind/Base/Content/MyOptions.aspx?id=" + objid + "\">" + title +"</a></li><hr>");
                    }
                    li_count = li_count+1;
                    if (li_count == 3) {
                    return false;
                    }
                });
            });
        }
    });
  }
});
list = list.concat("</ul>");
list = list.concat("<br /><a href=\"\">View previous clinical updates>></a>");  
$("#newscatlist").append(list);
return list;

以下是API调用生成的XML结构:

  <CONTENT_SEARCH>
    <CNT_SEQ_ID>29056</CNT_SEQ_ID>
    <CNT_CONTENT_ID>394414E8962345FAA04C01EF09E1E97E</CNT_CONTENT_ID>
    <CNT_CONTENT_TYPE_ID>ML.BASE.DOCUMENT</CNT_CONTENT_TYPE_ID>
    <CNT_TITLE>News Document5</CNT_TITLE>
    <CNT_DESCRIPTION>News Document4</CNT_DESCRIPTION>
    <CTYP_CONTENT_LAUNCH_TYPE>Document</CTYP_CONTENT_LAUNCH_TYPE>
    <CNT_AVAILABLE>T</CNT_AVAILABLE>
    <CTYPLCL_DISPLAY_NAME>Document</CTYPLCL_DISPLAY_NAME>
    <OBJ_STATUS>T</OBJ_STATUS>
    <CTYP_HAS_SCORE_FLAG>F</CTYP_HAS_SCORE_FLAG>
    <CNT_USER_COST>0</CNT_USER_COST>
    <PERMISSION>1</PERMISSION>
  </CONTENT_SEARCH>
  <CONTENT_SEARCH>
    <CNT_SEQ_ID>29134</CNT_SEQ_ID>
    <CNT_CONTENT_ID>F59863E43FDC4250A662BE6CC35239BD</CNT_CONTENT_ID>
    <CNT_CONTENT_TYPE_ID>ML.BASE.DOCUMENT</CNT_CONTENT_TYPE_ID>
    <CNT_TITLE>News Document6</CNT_TITLE>
    <CNT_DESCRIPTION>news document 6</CNT_DESCRIPTION>
    <CTYP_CONTENT_LAUNCH_TYPE>Document</CTYP_CONTENT_LAUNCH_TYPE>
    <CNT_AVAILABLE>T</CNT_AVAILABLE>
    <CTYPLCL_DISPLAY_NAME>Document</CTYPLCL_DISPLAY_NAME>
    <OBJ_STATUS>T</OBJ_STATUS>
    <CTYP_HAS_SCORE_FLAG>F</CTYP_HAS_SCORE_FLAG>
    <CNT_USER_COST>0</CNT_USER_COST>
    <PERMISSION>1</PERMISSION>
  </CONTENT_SEARCH>
  <CONTENT_SEARCH>
    <CNT_SEQ_ID>29135</CNT_SEQ_ID>
    <CNT_CONTENT_ID>7CB8D290D9C3433797FCB213CCAD7B22</CNT_CONTENT_ID>
    <CNT_CONTENT_TYPE_ID>ML.BASE.DOCUMENT</CNT_CONTENT_TYPE_ID>
    <CNT_TITLE>News Document7</CNT_TITLE>
    <CNT_DESCRIPTION>News Document7</CNT_DESCRIPTION>
    <CTYP_CONTENT_LAUNCH_TYPE>Document</CTYP_CONTENT_LAUNCH_TYPE>
    <CNT_AVAILABLE>T</CNT_AVAILABLE>
    <CTYPLCL_DISPLAY_NAME>Document</CTYPLCL_DISPLAY_NAME>
    <OBJ_STATUS>T</OBJ_STATUS>
    <CTYP_HAS_SCORE_FLAG>F</CTYP_HAS_SCORE_FLAG>
    <CNT_USER_COST>0</CNT_USER_COST>
    <PERMISSION>1</PERMISSION>
  </CONTENT_SEARCH>

我需要对CNT_SEQ_ID元素进行排序,然后遍历结果以创建列表项。我在排序中缺少什么?

2 个答案:

答案 0 :(得分:0)

您可以将Ajax调用拉出到函数中以供重用。

function getNewsDocuments(strCategoryID) {
    getDomainRootCat(); //looks up locreg
    var CategoryId = "";
    CategoryId = strCategoryID;

    var strPayload = "<Parameters><RequestContext><RequestUserId>ML.BASE.USR.Administrator</RequestUserId><RequestLocaleId>" + locreg + "</RequestLocaleId><RequestRegionId>" + locreg + "</RequestRegionId></RequestContext>" +
        "<Payload1>" +
        "<USR_LMS_USER_ID>ML.BASE.USR.Administrator</USR_LMS_USER_ID><CNTCTGY_CATEGORY_ID>" + CategoryId + "</CNTCTGY_CATEGORY_ID><strIncludeSubCategories>T</strIncludeSubCategories>" +
        "<SearchType>ML.BASE.DV.SearchAllWords</SearchType><TABLE_NAME>CONTENT_SEARCH</TABLE_NAME><CNT_CONTENT_TYPE_ID>ML.BASE.DOCUMENT</CNT_CONTENT_TYPE_ID>" +
        "</Payload1></Parameters>";  

    var strParams = "strCall=AdHocCall&strClassName=" + escape('BusinessLogic.ContentManagement') + "&strMethodName=GenericSearch&strPayload=" + escape(strPayload);
    var list = "<ul style=\"list-style: none; text-indent: -1.2em;\" class=\"newsupdates-list\" id=\"newsupdates-" + strCategoryID + "\">";

    var result = postAJAXUtilities(strPayload, strParams)

}

function getSomethingElse() {
    var strPayload = "?";
    var strParams = "?";
    var result = postAJAXUtilities(strPayload, strParams)
}

function postAJAXUtilities(strPayload, strParams) {
    $.ajax({
      type: "POST",
      async: false,
      data: strParams,
      url: "/AJAXUtilities.aspx",      
      success: function(msg){
        msg = $.createXMLDocument(msg);      

        $(msg).find('CONTENT_SEARCH').each(function(){

            if ($(this).find("CNT_AVAILABLE").text() == "T") {
            var objid = $(this).find("CNT_CONTENT_ID").text();
            var title = $(this).find("CNT_TITLE").text();

            if (title.length >= 40){
            var trimtitle = title.substring(0,39);
            list = list.concat("<li><a href=\"/MyOptions.aspx?id=" + objid + "\">" + trimtitle +"...</a></li><hr>");    
            }else{
            list = list.concat("<li><a href=\"/MyOptions.aspx?id=" + objid + "\">" + title +"</a></li><hr>");
            }

            }
        });

      }
    });

    list = list.concat("</ul>");
    list = list.concat("<br /><a href=\"\">View previous updates>></a>");   
    $("#newscatlist").append(list);
    return list;

}

这假设您在成功函数中的后期处理是相同的。如果没有,你总是可以将它作为一个单独的函数传递给ajax调用。

答案 1 :(得分:0)

这有效:

    $.ajax({
        type: "POST",
        async: false,
        data: strParams,
        url: "/AJAXUtilities.aspx",      
        success: function(msg){

            msg = $.createXMLDocument(msg);      

            var li_count = 0;
            var results = [];

            $(msg).find('CONTENT_SEARCH').each(function(){

                if ($(this).find("CNT_AVAILABLE").text() == "T") {
                    var seqText = $(this).find("CNT_SEQ_ID").text();
                    var seqVal = parseInt(seqText);
                    var objidText = $(this).find("CNT_CONTENT_ID").text();
                    var titleText = $(this).find("CNT_TITLE").text();
                    results.push({
                        seq: seqText,
                        seqVal: seqVal,
                        objid: objidText,
                        title: titleText
                    }); 
                    results.sort(function(a, b){
                        return b.seqVal - a.seqVal;
                    });
                }
            });
            var html=[];
            html.push("<ul style=\"list-style: none; text-indent: -1.2em;\" class=\"clupdates-list\" id=\"clupdates-" + strCategoryID + "\">");
            $.each(results, function(){
                if (this.title.length >= 40){
                    var trimtitle = this.title.substring(0,39);
                    html.push("<li><a href=\"/MyOptions.aspx?id=" + this.objid + "\">" + trimtitle +"...</a></li><hr>");
                }else{
                    html.push("<li><a href=\"/MyOptions.aspx?id=" + this.objid + "\">" + this.title +"</a></li><hr>");
                }
                li_count = li_count+1;
                if (li_count == 3) {
                return false;
                }
            });
            html.push("</ul>");
            html.push("<br /><a href=\"/documentssimplesearch.aspx\">View previous clinical updates>></a>");
            $("#clinicalcatlist").append(html.join(''));
        }
    });
}