更改表格中数据的位置

时间:2015-08-24 14:22:50

标签: javascript jquery html

我编写了JavaScript,它检索数据并将其设置为一系列表格,如下所示。

$(function()
{

$.ajax(
{
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists(guid'9BBF789F-E5BA-449D-A595-BAA326E2C8FF')/Items?$expand=Category&$select=Id,Related_x0020_Working_x0020_PracId,Title,Reference,Policy_x0020_Type,Category/Title&$orderby=Category/Title asc",
    type:"get",
    headers:    { "accept": "application/json;odata=verbose" },
    success: function(dataObj)
    {

        //to uniquely identifiy the accordion.
        var intCat = 0;

        var arrPolicies = [];
        var arrCategories = [];

        //Get the policies and seperate out the categories from the returned REST call
        for(var i =0; i < dataObj.d.results.length; i++)
        {
            var strCategory = dataObj.d.results[i].Category.Title;

            arrPolicies.push({
                "Id" :                  dataObj.d.results[i].Id,
                "Title" :               dataObj.d.results[i].Title,
                "Category" :            strCategory,
                "Ref" :                 dataObj.d.results[i].Reference,
                "PolicyType" :          dataObj.d.results[i].Policy_x0020_Type,
                "WorkingPracticeId" :   dataObj.d.results[i].Related_x0020_Working_x0020_PracId
            });

            //setting category if not found
            //in array
            if(arrCategories.indexOf(strCategory) == -1)
            {
                //Add the category to the list...
                arrCategories.push(strCategory);
            }
        }

        //Output the menu to the screen for each category - one by one
        arrCategories.forEach(function(varCategory)
        {
            var strCatIdentifier = "tblCategory_" + intCat;
            var strCatImgIdentifier = "tblCategory_image_" + intCat; 
            var strCategoryInfo = "<table>"+
                                "<tbody>" +
                                    "<tr>"+
                                        "<td class='category'>"+
                                            "<a href='javascript:ExpandCollapseRow(" + strCatIdentifier + ","+strCatImgIdentifier+")'>"+
                                                "<img id='"+strCatImgIdentifier+"' src='" + _spPageContextInfo.webAbsoluteUrl + "/SiteAssets/Images/expand-16.png' alt='expand category'/>"+
                                            "</a>&nbsp;&nbsp;"+
                                            varCategory +
                                        "</td>"+
                                    "</tr>"+
                                    "<tr><td>"+
                                    "<table id='" + strCatIdentifier + "' class='indent hidden'>";                                  


            //looping through policies - add the details into the category table's cell 
            arrPolicies.forEach(function(varPolicy)
            {
                        //checking the category attached to the policy is the same as what
                //category it is on
                if(varPolicy.Category == varCategory)
                {
                    //checking to see if the 
                    if(varPolicy.PolicyType == "Policy and Responsibility")
                    {
                        strCategoryInfo += "<tr>"+
                                            "<td class='policy'>" + 
                                                "<a href='#'>"+
                                                    "<img src='"+_spPageContextInfo.webAbsoluteUrl+"/SiteAssets/Images/arrowicon.png' alt='View Document'/>"+
                                                "</a>"
                                                 + varPolicy.PolicyType + ": "+varPolicy.Ref +" - " + varPolicy.Title + 
                                            "</td>"+
                                            "</tr>";
                    }

                    //If Working Practice - add in the sub-table (3rd level table) for attachments
                    if(varPolicy.PolicyType == "Working Practices")
                    {
                        var strCatWPIdentifier = "tblWorkingPractice" + varPolicy.Id;
                        var strCatWPImgIdentifier = "sub_level_image" + varPolicy.Id;
                        strCategoryInfo += "<tr>"+
                                                "<td class='working_practice'>"+
                                                    "<a href='javascript:ExpandCollapseRow(" + strCatWPIdentifier + ","+strCatWPImgIdentifier+")'>"+
                                                        "<img id='"+strCatWPImgIdentifier+"' src='" + _spPageContextInfo.webAbsoluteUrl + "/SiteAssets/Images/expand-16.png' alt='expand working practice'/>"+
                                                    "</a>&nbsp;&nbsp;"+
                                                    varPolicy.PolicyType + " - " + varPolicy.Title+
                                                "</td>"+
                                            "</tr>"; 

                        var intAttachmentCount = 0;
                        //Build a table by looping through the policies array AGAIN and only use the policies where the Policy Type is 'Attachment' AND the 'WorkingPracticeID' is the same as the pilocy ID
                        arrPolicies.forEach(function(varWPAttachment)
                        {

                            if(varWPAttachment.WorkingPracticeId == varPolicy.Id && varWPAttachment.PolicyType == "Working Practice Attachment")
                            {
                                intAttachmentCount++;
                                strCategoryInfo += "<tr>"+
                                                        "<td>"+
                                                            "<table id='"+strCatWPIdentifier+"' class='indent hidden'>"+
                                                                "<tr>"+
                                                                    "<td>"+
                                                                        varWPAttachment.PolicyType +" - "+ varWPAttachment.Title+ " - " + varPolicy.Title+
                                                                    "</td>"+
                                                                "</tr>"+
                                                            "</table>"+
                                                        "</td>"+
                                                    "</tr>";
                            }
                        });

                        if(intAttachmentCount == 0)
                        {
                            strCategoryInfo += "<tr>"+
                                                    "<td>"+
                                                        "<table id='"+strCatWPIdentifier+"' class='indent hidden'>"+
                                                            "<tr>"+
                                                                "<td>"+
                                                                    "Sorry, no attachments found for this practice."+
                                                                "</td>"+
                                                            "</tr>"+
                                                        "</table>"+
                                                    "</td>"+
                                                "</tr>";

                        }

                    }

                }
            });

            //Close the 'Category details' table
            strCategoryInfo += "</table>";

            //Close the table for the category...
            strCategoryInfo += "</td></tr>" +
                                "</tbody>"+
                            "</table>";


            intCat++;
            $('#divQualityFrameworkMenu').append(strCategoryInfo + "<br />");   

        });

    },
    error: function(error)
    {
        alert("Error");
    }       
});

    });

我希望能够组织它们,以便将相关数据组合在一起,即政策高于工作实践。

我该怎么做呢

1 个答案:

答案 0 :(得分:0)

这看起来很简单。在第一个for (var d = 0; [...] )循环之后,但在arrCategories.forEach([...])之前,只需按您选择的方式排序arrPolicies

arrPolicies.sort(function(policy1, policy2) {
    //Policies BEFORE Working Practicies:
    if (policy1.PolicyType === "Policies and Responsibilities" && policy2.PolicyType === "Working Practices") {
        return -1;
    }
    //Working Practicies AFTER Policies:
    if (policy1.PolicyType === "Working Practices" && policy2.PolicyType === "Policies and Responsibilities") {
        return 1;
    }
    //[Include any other orderings you might have...]
    //If you've reached the end here, then you must not care about the ordering of these policies, so just make them "equal":
    return 0;
});