0

I have some code like this:

var data = // coming in from AJAX and confirmed working, don't need to wory about this...

var row = // cloning an existing HTML structure in the DOM

for (i = 0; i < data.length; i++) {
    var rowclone = row.clone();
    var orderLastChangedTime = new Date(data[i].createDate);
    var diffDays = Math.round(Math.abs((currentTime.getTime() - orderLastChangedTime.getTime())/(oneDay)));
    rowclone.find(".home-order-calc").text(diffDays);
    rowclone.find(".home-order-status").text(data[i].status);
    rowclone.find(".home-order-po-number").text(data[i].poNumber);
    rowclone.find(".home-order-number").text(data[i].orderId);
    rowclone.find(".home-order-last-changed").text(orderLastChangedTime);
    rowclone.find(".home-order-lines").text(data[i].itemsCount);
    rowclone.find(".home-order-cost").text(data[i].cost);
    var rowstatus = rowclone.find(".home-order-status").text();
    rowstatus = rowstatus.toUpperCase();
    openJSONitems = [];
    closedJSONitems = [];
    otherJSONitems = [];
    if (status[rowstatus] == "open") {
        openJSONitems.push(rowclone);
    }
    else if (status[rowstatus] == "closed") {
        closedJSONitems.push(rowclone);
    }
    else {
        otherJSONitems.push(rowclone);
    }
    console.log(openJSONitems);
    openJSONitems.appendTo("#home-table-orders");
}

I am trying to create 3 new JavaScript arrays and array push data into them based on sort criteria from the JSON payload. Once they are sorted I want to hang on to them and attach them to the DOM on some user actions... what am I doing wrong?

1
  • openJSONitems is an array, it doesn't have the appendTo method, you'll have to iterate over that array and append its elements to "#home-table-orders". Besides, you're creating a new array in each iteration. Commented Jan 6, 2015 at 20:01

3 Answers 3

1

openJSONitems is an array, it doesn't have the appendTo method, you'll have to iterate over that array and append its elements to "#home-table-orders". Besides, you're creating a new array in each iteration. I think this changes would fix the problem. You could also avoid the last loop inserting the element directly when status[rowstatus] == "open" if you liked.

var openJSONitems = [],
closedJSONitems = [],
otherJSONitems = [];    
var data = // coming in from AJAX and confirmed working, don't need to wory about this...

var row = // cloning an existing HTML structure in the DOM

for (i = 0; i < data.length; i++) {
    var rowclone = row.clone();
    var orderLastChangedTime = new Date(data[i].createDate);
    var diffDays = Math.round(Math.abs((currentTime.getTime() - orderLastChangedTime.getTime())/(oneDay)));
    rowclone.find(".home-order-calc").text(diffDays);
    rowclone.find(".home-order-status").text(data[i].status);
    rowclone.find(".home-order-po-number").text(data[i].poNumber);
    rowclone.find(".home-order-number").text(data[i].orderId);
    rowclone.find(".home-order-last-changed").text(orderLastChangedTime);
    rowclone.find(".home-order-lines").text(data[i].itemsCount);
    rowclone.find(".home-order-cost").text(data[i].cost);
    var rowstatus = rowclone.find(".home-order-status").text();
    rowstatus = rowstatus.toUpperCase();

    if (status[rowstatus] == "open") {
        openJSONitems.push(rowclone);
    }
    else if (status[rowstatus] == "closed") {
        closedJSONitems.push(rowclone);
    }
    else {
        otherJSONitems.push(rowclone);
    }
}

console.log(openJSONitems);
for (i = 0; i < openJSONitems.length; i++) {
  $(openJSONitems[i]).appendTo("#home-table-orders");
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could add then as a data element onto a DOM object.

$('body').data('openItems', openJSONitems);

And retrieve them later:

var items = $('body').data('openItems');

Comments

0

Have you considered using localStorage?

localStorage.setItem('openJSONitems', openJSONitems );

And retrieving it with...

var openJSONitems = localStorage.getItem('openJSONitems');

Comments

Your Answer

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