2

I have pages of scrapped data coming from the db and displaying that in a table (which works fine). Then I have another query from the db to only get the error rows from that scrapped data and to display that data in a div in the header. This way the user knows what data to change.

I'm having trouble allowing the user to change pages and then get the first row of that page's error data. Right now what I'm doing is creating an index variable and using that as a count and allowing the user to click through rows one at a time and then it changes to the next page if there is no more error data for that page.

How can I get it so the user is able to change the pages freely and then get the correct rows for that data and be able to click up or down through the objects for that page?

Here is my header where the error data and page header data is stored:

    <div id="pageEditDiv">
        <div class="arrowIconsDiv">
            <img src="images/up-arrow.png" class="arrowIcons" id="arrowUpPage">
            <img src="images/down-arrow.png" class="arrowIcons" id="arrowDownPage">
        </div>
        <div id="pageSummary">
             <table id="headerPagesTable">
                 <thead><tr><th>Page Num</th><th>Type</th><th>Month</th><th>Name</th><th>Reg No.</th><th>Rrc District</th></tr></thead>
                 <tbody id="pagesTableBody"></tbody>
              </table>
        </div>
        <div id="pagesTable" class="hidden"></div>
    </div>
    <div id="rowEditDiv">
        <div class="arrowIconsDiv">
            <img src="images/up-arrow.png" class="arrowIcons" id="arrowUpRow">
            <img src="images/down-arrow.png" class="arrowIcons" id="arrowDownRow">
        </div>
        <div id="editableRowToEdit" contenteditable>
            <table id="editableRowTable">
                <tbody id="pagesRowToEdit"></tbody>
            </table>
        </div>
   </div>

Here is where I get the error row data:

    $.ajax({
        type: 'POST', 
        url: 'qry/getPageReceipts.php',
        async: true,
        data: {FileId: fileId, PageNum: getPageNum, RowNum: rowNum},
        success: function(response) {
            recPageData = JSON.parse(response);
            //check if data exists or not
            recPD = {};
            if(recPageData.length == 0) {
                recPageDateEmpty = 1;
            } else {
                //map the data
                recPD = recPageData.PageNum.map((x,i) => ({
                        pageNum: x,
                        rowNum: recPageData["RowNum"][i],
                        cName: recPageData["CustomerName"][i],
                        fName: recPageData["FacilityName"][i],
                        rrcNum: recPageData["RrcNum"][i],
                        rrcType: recPageData["RrcNumType"][i],
                        volume: recPageData["Volume"][i]
                }));
                //sort the data
                recPD.sort(function(a,b) {
                    if(a.pageNum == b.pageNum) {
                        return (a.rowNum - b.rowNum);
                    } else {
                        return (a.pageNum - b.pageNum);
                    }
                });
                for(var i=0; i<recPD.length; i++) {
                    recPD[i].index = i;
                }
            }
            drawPageForm();
            drawRowEditForm();
        }
    });

Here is where I draw the page summary data for the user to click up and down the pages:

    function drawPageForm() {
        //clear div to begin with
        $(".pagesMonth").html();
        $("#pagesTableBody").empty();

        var getPages = '<table><thead><tr><th>Page Num</th><th>Type</th><th>Month</th><th>Name</th><th>Reg No.</th><th>Rrc District</th></tr></thead><tbody>';

        for(var i=0; i<getPagesResponse.length; i++) {
            getPages += '<tr class="getPagesRowEdit"><td>' + getPagesResponse["PageNum"][i] + '</td><td class="pagesPageType">' + getPagesResponse["PageType"][i] + '</td><td class="pagesMonth">' + getPagesResponse["ReportingMonth"][i] + '</td><td class="pagesFilerName">' + getPagesResponse["FilerName"][i] + '</td><td class="pagesFilerRegNo">' + getPagesResponse["FilerRegNo"][i] + '</td><td class="pagesRrcDistrict">' + getPagesResponse["RrcDistrict"][i] + '</td></tr>';
        }
        getPages += '</tbody></table>';
        //add table to div
        $("#pagesTable").html(getPages);

        //PAGES
        //delcare single object for page summary
        gPT = {
            gPRE : $(".getPagesRowEdit").eq(0),
            pNum : $(".getPagesRowEdit").find("td").eq(0).text(),
            pTB : $("#pagesTableBody"),
            aUP : $("#arrowUpPage"),
            aDP : $("#arrowDownPage"),
            place : function(row) {
                gPT.pTB.empty();
                clone = row.clone(true);
                clone.appendTo(gPT.pTB);
            }
        }
        //add row to div
        gPT.place(gPT.gPRE);
        //arrow up
        gPT.aUP.on("click", function() {
            prev = gPT.gPRE.prev();
            gPT.gPRE = prev.is("tr") ? prev : gPT.gPRE;
            gPT.place(gPT.gPRE);
            gPT.pNum = $(".getPagesRowEdit").find("td").eq(0).text();
            pageNum = gPT.pNum;
            reDrawTextContentandPDF();
        });
        //arrow down
        gPT.aDP.on("click", function() {
            next = gPT.gPRE.next();
            gPT.gPRE = next.is("tr") ? next : gPT.gPRE;
            gPT.place(gPT.gPRE);
            gPT.pNum = $(".getPagesRowEdit").find("td").eq(0).text();
            pageNum = gPT.pNum;
            reDrawTextContentandPDF();
        });
}

Here is where I draw the row error data for the user to click up and down each row of data for that specific page:

function drawRowEditForm() {
    //get the current page type
    pageTypeValue = $(".pagesPageType").html();
    //empty row
    $("#pagesRowToEdit").empty();
    //find correct row
    recPD.find(findRecPageIndex);

    //match row with rawText row
    findMatchRowNum = $("#pagesRowToEdit").find("tr").eq(0).find("td").eq(0).text();
    findMatchRowNum = findMatchRowNum - 1;
    matchedRow = $(".rowToEdit").eq(findMatchRowNum);
    matchedRow.addClass("selected");
    //scroll div to visible row
    $("#textCodeDiv").animate({
        scrollTop: $(".selected").offset().top
    },'slow');

    //click arrow up
    $("#arrowUpRow").unbind("click").click(function() {
        clickRowArrowUp();
    });
    //click arrow down
    $("#arrowDownRow").unbind("click").click(function() {
        clickRowArrowDown();
    });
}

function clickRowArrowUp() {
            $("#pagesRowToEdit").empty();
            if($(".selected")) {
                $(".selected").removeClass("selected");
            }
            recRowIndex--;
            if(recPD.find(findRecPageIndex)) {
                drawRowEditForm();
            } else {
                prev = gPT.gPRE.prev();
                gPT.gPRE = prev.is("tr") ? prev : gPT.gPRE;
                gPT.place(gPT.gPRE);
                gPT.pNum = $(".getPagesRowEdit").find("td").eq(0).text();
                pageNum = gPT.pNum;
                reDrawTextContentandPDF();
            }
}

function clickArrowDown() {
            $("#pagesRowToEdit").empty();
            if($(".selected")) {
                $(".selected").removeClass("selected");
            }
            recRowIndex++;
            if(recPD.find(findRecPageIndex)) {
                drawRowEditForm();
            } else {
                next = gPT.gPRE.next();
                gPT.gPRE = next.is("tr") ? next : gPT.gPRE;
                gPT.place(gPT.gPRE);
                gPT.pNum = $(".getPagesRowEdit").find("td").eq(0).text();
                pageNum = gPT.pNum;
                reDrawTextContentandPDF();
            }
}

    //match the row to the error data and display in header
    function findRecPageIndex(el) {

        if(el.index === recRowIndex && el.pageNum === pageNum) {
            return $("#pagesRowToEdit").append("<tr id='recTR'><td class='hidden'>" + el.rowNum + "</td><td>" + el.cName + "</td><td>" + el.fName + "</td><td>" + el.rrcNum + "</td><td>" + el.rrcType + "</td><td>" + el.volume + "</td></tr>");
        }

    }
    function findDelPageIndex(el) {

        if(el.index === delRowIndex && el.pageNum === pageNum) {
            return $("#pagesRowToEdit").append("<tr id='delTR'><td class='hidden'>" + el.rowNum + "</td><td>" + el.cName + "</td><td>" + el.fName + "</td><td>" + el.rrcNum + "</td><td>" + el.rrcType + "</td><td>" + el.volume + "</td></tr>");
        }

    }

So a recap: The user can change pages, BUT then they can't click through the error data. The user can change rows and when there is no more error data on that page it will then change pages and display the first error data row.

What I need to know: is how to allow the user to click through the pages freely and then it be able to determine what page num it is and then display that first error row and allowing the user to click through the correct rows for that page.

1 Answer 1

1

Try adding this to your page up and down function:

    for(var i=0; i<recPD.length; i++) {
        if(pageNum == recPD[i].pageNum) {
            recRowIndex = recPD[i].index;
            break;
        }
    }

Here you are looping through the data, checking if the page numbers match and then updating the recRowIndex variable to the lowest index of the error row data.

Hope that works!

Sign up to request clarification or add additional context in comments.

1 Comment

Works perfect thank you! Can't believe that's all I was missing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.