How do I load CSV files from the Monaca app?

Asked 2 years ago, Updated 2 years ago, 120 views

The Monaca app reads CSV files and creates a program that displays data. I can't read the CSV file when I try to search.

Enter string → List search results in partial search.
After that, I'm thinking of a program with a flow like clicking on the results to transfer to the detail page.

I would like to study for the future, so please let me know.Please.

テキストIntegrate text search function
I copied and pasted HTML, JS, and CSV directly to monaca using ↑ as a reference.
It works on the preview, but on the terminal debug, when you search, it doesn't move from the search screen.
uncaught:list is not defined
An error appears.

I learned from the reference page that I would communicate with Ajax, so I studied in advance.
"Ajax communication is not available offline, so be sure to transfer the file to the server for testing."

[Preview works but terminal debugs don't]
I don't know how to change the code for
Please tell me the solution.

I was going to make a sample application that shows all my personal information.br/> It's only displayed on the reference page, sorry.
Valid plug-ins are
15 points: Contacts, Custom Config, Device, Device Motion, Device Orientation, File, Geolocation, InSppBrowser, Monaca Plugin, Network Information, Nifty, Notification, Splashscreen, Vibration, Whitelist.


Installed Components Remain Initial
There are two things: cordova(phonegap) and Monaca Core Utility.

The browser will be Chrome and the actual Xperia Z2SO-03F

Text Search

for(n=1;n<list.length;n++){ 

is now Uncaught ReferenceError:list is not defined...

Here is the code.
index.html

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Integrate Text Search </title>
<script type="text/javascript" src="xmlhttp.js"></script>
<script type="text/javascript"><!--
function loadTextFile (fName)
{
httpObj=createXMLHttpRequest(displayData);
if(httpObj)
{
httpObj.open("GET", fName, true);
httpObj.send(null);
}
}
function displayData()
{
if((httpObj.readyState==4)&(httpObj.status==200))
{
    // Load from CSV into array variables
	CSVtoAD (httpObj.responseText);
	<!--★Delete-->
}
}
// --></script>
</head>
<body onLoad="loadTextFile('sample3.csv')">
<h1>Integrating Text Search </h1>
<!--★-->
<form name="ajaxForm" onSubmit="output_result();return false">
<!--★-->
		<input name="searchtext" type="text"> 
		<input type="submit" value="search">
</form>
<divid="resultData">Examples of input: mac, 5,000, LCD </div>

<script type="text/javascript"><!--
// Load from CSV into array variables
function CSVtoAD(csv){
	varn,m;
	
	var count_rec = 0; // Count records
	var count_fld = 0; // Count the fields
	
	// Line feed code
	csv=csv.replace(/\r\n/g, "\n");
	csv = csv.replace(/\r/g, "\n");
	
	// " (corresponding to " in the field)
	csv=csv.replace(/"/g, "&quot;");
	
	list = new Array();
	
	// Acquisition processing for each data
	n = 0;
	do{
		// Next line feed position
		barrier=csv.indexOf("\n",n);
		
		if(eor>-1){
			if(csv.substring(n,eor)!=""){
				// Array for fields
				if(count_fld==0)list[count_rec] = new Array();
				
				// If " " is at the beginning
				varm=csv.indexOf("\"",n);
				if(m==n){
					// Use the following " " as the end of the data
					eod=csv.indexOf("\",n+1)+1;
					// Modifying the end position of the record (ignore line breaks in ")
					eor=csv.indexOf("\n", eod);
					
					m = 1;
				} else{
					m = 0;
					eod=n;
				}
				
				// Location of ", "
				eod=csv.indexOf(",",",eod);
				// Before a new line
				if((eod>-1)&(eod<eor)){
					// Just end the field
				} else{
					// Line feed position at the end of the field
					eod = eor;
				}
				
				// To Array Variables
				list [count_rec] [count_fld] = csv.substring(n+m, eod-m);
												
				// End of Record
				if(eod==eor){
					count_rec++;
					count_fld = 0;
				} else{
					count_fld++;
				}
				
				// Next Search Start Location
				n = eod+1;
					
			} else{
				count_fld = 0;
				n = eor+1;
			}		
		}
		
		if(n>csv.length)eor=-1;

	} while(eor>-1);
}

// ★ View search list
function output_result(){
	varn, m, r;
	// Get search string
	vars = document.ajaxForm.searchtext.value;
	// Search Results
	t=";
	
	// Text Search
	for(n=1;n<list.length;n++){
		// Search hit
		varbool=false;
		// [1] Product name ~ [6] Search description
		for(m=1;m<7;m++){
			// Regular expression (ignore case)
			reg = new RegExp(s, "i")
			// partial search
			r=list[n][m].match(reg);
			if((r!="")&&(r!=null)){
				// Get hit data
				t+ = output_text(n);
				// Do not search for subsequent items
				break;
			}
		}
	}
		
	// Result output
	document.getElementById("resultData").innerHTML=t;
}

// ★ Data Retrievaluation
function output_text(num){
	varn;
		
	vart="<p><a href='javascript:output_detail("+num+")'>"+list[num][1]+"+"+"+list[num][2]+"]<a>Price:"+list[3]+"Inventory:"+list[num][4]+"+"+list[4]>num+">">">list[4+">>>>>
	

	// Data Output
	return;
}

// View detailed data
function output_detail(idx){
	varn;
	
	US>vart=";
	
	if(idx>0){
		t+="<table border='2'>";
		t+="<tr><td bgcolor='#ffff99'>"+list[0][1]+"</td>td>"+list[idx][1]+"</td>>><td rspan='5'>>>>>iclist[lmg][7] + "'height='200'></td></tr>";
		t+="<tr><td bgcolor='#ffff99'>"+list[0][2]+"</td>"+list[idx][2]+"</td>>;;;;;;tr>";;;
		t+="<tr><td bgcolor='#ffff99'>"+list[0][3]+"</td>>td align='right'>"+list[idx][3]+"</td>>>/tr>";
		t+="<tr><td bgcolor='#ffff99'>"+list[0][4]+"</td>>td align='right'>"+list[idx][4]+"</td>>>/tr>";
		t+="<tr><td bgcolor='#ffff99'>"+list[0][5]+"</td>>td align='center'>"+list[idx][5]+"</td>>/tr>";
		t+="<tr><td bgcolor='#ffff99'>"+list[0][6]+"</td>>>td colspan='2'>"+list[idx][6].replace(/\n/g, "<br>")+">lt;lt;>>>>
		t+="</table>";
	}
	
	// Table Output
	document.getElementById("resultData").innerHTML=t;
}
// --></script>

</body>
</html>

xmlhttp.js

 function createXMLHttpRequest (cbFunc)
{
var XMLhttpObject = null;
try{
XMLhttpObject = new XMLHttpRequest();
}catch(e){
try{
XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
return null;
}
}
}
if(XMLhttpObject)XMLhttpObject.onreadystatechange=cbFunc;
return XMLhttpObject;
}

sample3.csv

javascript monaca html5 ajax csv

2022-09-30 21:21

1 Answers

uncaught:list is not defined

I am not familiar with Monaca's debugging environment, but this error appears to be the same as Uncaught ReferenceError: list is not defined for Chrome.
This means that you referenced the variable list before defining it.

If you look at http://www.shurey.com/js/labo/db/db05.html, the function CSVtoAD creates a global variable, list.
Other functions seem to refer to this.
If so, you can infer that other functions (output_result?) were called before calling CSVtoAD.

The CSVtoAD is called by the function loadTextFile, and the loadTextFile should be called by the onload attribute of the body tag, but it works differently in browsers and real machines.

However, there is a limit to what you can infer without looking at the code, and you need the code to make it a generally meaningful question.
For questions like this, please refer to Minimum, self-contained, and verifiable sample code writing .

Also, the error message should have information such as where it occurred, so that's also necessary.

jQuery is not used as far as http://www.shurey.com/js/labo/db/xmlhttp.js.

Note:

Are other functions called before the call?

At the beginning of each function, you can check the debug output of console.log.

 function CSVtoAD(csv){
    console.log("CSVtoAD:start");

If it's a difference between a browser and a real machine, how do you fill it in?

It's better to think about countermeasures only after you know the cause.
Also, the code in the question worked without any errors in my environment (cordova 6.3.1, Android 6.0.0).


2022-09-30 21:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.