2

I am a classic ASP programmer and I have for some time now mixed the good old safe classic ASP with JQUERY with good results really. But I now have a project that requires more offline data processing than ever before. I'm learning as I go and this is my latest stumble...:

I need to take an array created server-side and populate it client-side ( and offline) in an html table which is powered by a jquery livesearch. Now I need to store the array (classic ASP) either in a cookie or local storage. Then be able to read it into an html table.

Here's how I would do it with classic ASP if I could rely on server-side connection:

  <tbody>
  <%
     For a = 0 to Pris_ant  
  %>                    
     <tr>
       <td><%=Priser(a,0)%></td>
       <td><%=Priser(a,1)%></td>
       <td><%=Priser(a,2)%></td>
       <td width="67"><%=Priser(a,3)%>,-</td>
       <td width="67"><%=Priser(a,4)%></td>
       <td width="70"><%=Priser(a,5)%></td>
  </tr>
     <% Next %>      
  </tbody>

OK, not the smoothest code but it works as long as I'm on a hard-line or in an area with good LTE connection. But the users are rarely on sites with good mobile data connection and I need to be able to generate this list from a local storage even IF there's no network. A.k.a I can open a locally stored HTML page and read the data from the local storage.

So the BIG question is how do I do this in the simplest manner possible ? I've searched high and low and i'm none the wiser. So any help would be greatly appreciated.

UPDATE:

ok so some background information. I am downloading this data with GetJson. And when I try the $each function on the "raw data" - it works like a charm.

However: In order to save the data in a localstorage I use JSON.stringify first. When I retrieve the data from the localstorage, I try using JSON.parse - but then all I get is: "JSON.parse is not a function"

Here's the data as it shows up after fetching it from localstorage:

Var JsonData = [{"rowNumber":563663,"hasWarning":true,"isInvoiceAccount":true,"phone":"","name":"Bertel AS","address1":"Co/Skanning","address2":"PB 52","attention":"","mobile":"","email":"faktura@bos","fax":"","zipCity":"N-1471 Lørenskog","invoiceAccount":"","account":"3","country":"NORGE","salesRep":"4","countryCode":"no"},{"rowNumber":563674,"hasWarning":false,"isInvoiceAccount":true,"phone":"","name":"LILLEHAMMER","address1":"POSTBOKS 1100","address2":"","attention":"","mobile":"","email":"","fax":"","zipCity":"N-2605 LILLEHAMMER","invoiceAccount":"","account":"14","country":"NORGE","salesRep":"4","countryCode":"no"},{"rowNumber":563676,"hasWarning":true,"isInvoiceAccount":true,"phone":"63929788","name":"Nygaard","address1":"Postboks 82","address2":"","attention":"","mobile":"","email":"karosseri","fax":"","zipCity":"N-2051 Jessheim","invoiceAccount":"","account":"16","country":"NORGE","salesRep":"4","countryCode":"no"},{"rowNumber":563686,"hasWarning":false,"isInvoiceAccount":true,"phone":"","name":"TRØGSTAD","address1":"POSTBOKS 165","address2":"","attention":"","mobile":"","email":"tkarosse","fax":"","zipCity":"N-1860 TRØGSTAD","invoiceAccount":"","account":"26","country":"NORGE","salesRep":"4","countryCode":"no"}]

When I try to use $.each I either get "undefined" OR I get a split for each letter in the variable string.

$.each( JsonData, function ( index, value ) {
  console.log( "index: " + index + ", name: " + value.name );
});

I have also tried this:

  $.each( JsonData, function ( index, value ) {
    console.log( "index: " + index + ", name: " + value[index].name );
  });

I have tried to use JSON.Stringify and JSON.PARSE, but nothing has helped me thus far.

// var JsonData = JSON.stringify(this.result);
// var JsonData = $.parseJSON(JSON.stringify(this.result));
// var JsonData = this.result;
// var JsonData = JSON.parse(JsonData);

1 Answer 1

1

One of possible solutions:

  1. Create an ASP page that returns your array for example in JSON:

    [
    <% For a = 0 to Pris_ant %>    
        { "prop1": <%=Priser(a,0)%>, "prop2": <%=Priser(a,1)%> //etc }, 
    <% Next %>
    ]
    
  2. Then on page load check your localstorage

  3. If it is empty make an ajax request ($.getJSON() to grab your array and store it in localstorage

Update.

Once you got this array from server you can iterate through it using jQuery.each():

var result = [
    { "prop1": 1 },
    { "prop1": 2 },
    { "prop1": 3 }
];

$.each( result, function ( index, value ) {
    alert( "index: " + index + ", prop1: " + value.prop1 );
});

JSFiddle

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

2 Comments

I've come up with something similar. But... How do I loop through the JSON array afterwords. i've looked at $.each, but I'm going nowhere, fast
also, it turns out the problem comes after i use JSON.stringify to save it to localstorage. I get JSON.parse is not a function when I try to reverse the "stringify effect"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.