簡體   English   中英

選擇第1頁上的item2返回第2頁上的item1

[英]Selecting item2 on page 1 returns item1 on page2

我正在從數據庫動態創建行和表以顯示只讀項目列表。 單擊這些項目之一時,頁面應重新加載到其他窗口並顯示與單擊的項目相關的相關結果。

我已經完成了99%的工作,但是,當我單擊第一行(假設為“ item1”時,下一個窗口將顯示未定義)。當我單擊第二行(“ item2”)時,該窗口將顯示item1的結果。第三行(“ item3”)窗口反映了item2的結果。

我最了解的是,我可能沒有正確存儲變量。

我最接近的猜測是,這可能是queryString的解決方案,但是我不確定如何或在何處實現它。

我已經為所有可能遇到此問題的人提供了所有注釋過的相關代碼。

var customApp = function(){

// Establish Variables
var salonName
var salonDomainLink
var salonAddress
var salonPhoneNumber
var salonSelection

// Queries Salons class
var query1 = new Parse.Query("Salons");
query1.find({
    success: function(results) {

    // Changes search results from JSON Object to ?something readable?
        var searchResults = JSON.parse(JSON.stringify(results))

        // Loops through every result and creates variables with each specific result
        for ( var i in searchResults) {
            salonName = searchResults[i].name;
            salonDomainLink = searchResults[i].domainLink;

            // Creates a new row for each item in table
            var newrow = $('<tr>').text('').appendTo('#searchTable');

            // Adds a cell for each salonName in row
            var nameCell = $('<td>').text(salonName).appendTo(newrow);

            // Adds an id of the salon name + 'Id' to each row
            var cellId = nameCell.attr("id", salonName.replace(/\s+/g, '') + "Id")

            // Changes class of each cell
            nameCell.addClass("text-left font-w600 text-primary");
        }

        // Passes clicked salon name to a variable for use on another page
        $('#searchTable tr td').click(function(){

            // Acquires text value of the specific td of tr
            salonSelection = $(this)[0].childNodes[0].nodeValue

            // Saves the variable to local storage (can be retrieved with "localStorage.salonSelection")
            delete localStorage.salonSelection;
            window.localStorage.salonSelection = salonSelection;
            window.location = 'base_pages_schedule_1.html';
        });
    },
    error: function(error) {
        alert("Error: " + error.code + " " + error.message + ". Please contact Support.");
    }
});

query1.equalTo("name", localStorage.salonSelection);
query1.find({
    success: function(results) {
        var searchResults = JSON.parse(JSON.stringify(results))
        // Loops through every result and creates variables with each specific result
        for ( var i in searchResults) {
            window.localStorage.salonName = searchResults[i].name;
            window.localStorage.salonDomainLink = searchResults[i].domainLink;
            window.localStorage.salonAddress = searchResults[i].address;
            window.localStorage.salonPhoneNumber = searchResults[i].phoneNumber;
        }
    },
    error: function(error) {
        alert("Error: " + error.code + " " + error.message + ". Please contact Support.");
    }
});
$('#salon-name').text(localStorage.salonName);
$('#salon-address').text(localStorage.salonAddress);
$('#salon-phone-number').text(localStorage.salonPhoneNumber):};

query.find()是異步的,因此除非在dom中設置了text()之后,否則它不會設置新值

將dom更新移到您的find()成功回調中

query1.find({
    success: function(results) {
        var searchResults = JSON.parse(JSON.stringify(results))
        // process results

        // then update dom
        $('#salon-name').text(localStorage.salonName);

    },
    error: function(error) {
        alert("Error: " + error.code + " " + error.message + ". Please contact Support.");
    }
});

還要注意,您的for循環在循環的每次迭代中都將新值重寫到存儲鍵中,因此僅存儲最后一個值。 不確定您的存儲目標在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM