facebook

Load local text file into Text field?: CLOSED

  1. MobiOne Archive
  2.  > 
  3. Getting Help – General
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #341368 Reply

    decten76
    Member

    say I have a form with
    1- a text input, to get a local text file.
    2- a text widget for display content of the text file.
    3- a button to run a javascript or so to load the text file named in text input and displays the content in the text widget.

    I assume it is very simple, but I am not quite familiar with Javascript. Can some one pls help to give a sample code?
    Thanks.

    #341372 Reply

    Proffie
    Member

    hi decten76,
    This is rather easy but I encourage you to read through the jQuery Select Element cheat sheet and the MobiOne Widgets documentation in the learning center. It is well explained there and not a lot to read. I have beed using examples from this forum myself, but in the end it was better to understand the coding by heart, as it gives you confidence in what you do.
    I assume,

    $('m1-yourpage-yourtextwidgetId').val('#m1-yourpage-yourtextinputid');

    would do the job. or

    var text = $('#m1-yourpage-yourtextinputid').val(); $('m1-yourpage-yourtextwidgetId').val(text);

    if you want to use the variable way.

    #341396 Reply

    decten76
    Member

    I assume,

    $('m1-yourpage-yourtextwidgetId').val('#m1-yourpage-yourtextinputid');

    would do the job. or

    var text = $('#m1-yourpage-yourtextinputid').val(); $('m1-yourpage-yourtextwidgetId').val(text);

    if you want to use the variable way.[/quote]
    —–
    Thanks for your help.
    I tried. test attached. I dont know why It doesnt work with the code above.
    https://www.dropbox.com/s/918xd2f0sflmfrh/test1.rar
    If you could look at the sample and correct my mistake it would be great.
    Thanks.

    #341417 Reply

    Proffie
    Member

    Ok, reading your example file, You wanted something a little more complicated. I was assuming, you wanted to move the content of the input field to the output text tag, but you want to display the content of an external text file into the text field frame. (in other words make a notepad.)
    first, let me explain some on your code:

    $('#m1-test1-text1').val('C:\temp\test1\test.txt')

    is not going to work, as I am sure, your mobile phone does not have a C:\temp\ directory…right? you have to stay within the boundaries of your design space.
    if you change your button code to:

    var txt = $('#m1-test1-textField1').val();
    $('#m1-test1-text1').text(txt);
    

    at least the text will now match your input field. (sorry for the misunderstanding)
    now, as to how you convert the content of an external text file into HTML and put it into the widget, You need to dig deeper into jQuery. I am no crack at that. Maybe the .data object will be helpful?

    #341421 Reply

    decten76
    Member

    The idea is to read a test.txt file and show the content into the text field. The content display is the idea. Maybe I didnt make myself clear from beginning. I want to read the text file and display the content of it into the text field.
    I found quite a few samples how to read local text file. However as stated, I need a quick help to get the code merge into the test sample. Guess as soon as it works then I can continue further from there.
    Thanks for your help.

    #341455 Reply

    Hi decten76,

    Next example should be helpful. It shows how to load a txt file (must be included in your app):
    http://www.genuitec.com/support-genuitec/viewtopic.php?p=6612#p6612

    If this is not what you are looking for, please share more details such target platform, if it is a native app or a web app and any other information that you think would help us understand your requirements.

    #341471 Reply

    decten76
    Member

    Hi Octavio,
    Thanks. The sample is great help. I downloaded n played around with getItem and putItem. Now I understand every entry in localstorage has a key type string and value as another string.
    The issue with the textfile from GET is, it is presented as a single string to putItem to localstore with key ‘data’.
    My text file is something looks like this

    item2=bla bla 2
    item3=bla bla 3

    itemn=bla bla n

    I need to put such content in to localstorage in the form of keyS and valueS, so that I can retrieve them via

    function get_item_no(i) { //i is integer
    var itemKey = localStorage.key(i);
    var values = localStorage.getItem(itemKey);
    values = values.split(“=”);
    var return_data = values[1];
    return return_data;
    }

    lets say, I would like to show value associated with item by browsing, or by selecting in the list of items.
    It seems to me that I need something to convert the text file into an array of strings and then run a loop to put them into localstorage.
    Any sample code could be most grateful.

    btw, where the localstorage is actually stored? say in a browser env like firefox.

    #341474 Reply

    Proffie
    Member

    you could read the txt file into sql with

    LOAD DATA LOCAL INFILE '/text.txt' INTO TABLE tablename FIELDS TERMINATED BY '='

    and depending if you used windows to generate your text.txt file, add

     LINES TERMINATED BY '\r\n'

    after that, you can use all database functionailty to browse through your data.
    on the Pro and contra of SQL, you can read thousands of posts here in the forum. I am not going to add to that religion 🙂

    #341476 Reply

    decten76
    Member

    Hi Profile.
    I am not going to step into database religion.
    Thanks.

    #341595 Reply

    decten76
    Member

    Guys,
    I got it work by adding this func so it can read local or remote file regardless of form status.

    function readTextFile(file)
    {
    var rawFile;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    rawFile=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    rawFile=new ActiveXObject(“Microsoft.XMLHTTP”);
    };
    rawFile.open(“GET”, file, true);
    rawFile.onreadystatechange = function ()
    {
    //alert(rawFile.readyState);
    if(rawFile.readyState === 4)
    {
    if(rawFile.status === 200 || rawFile.status == 0)
    {
    var allText = rawFile.responseText;
    // here what u want with allText
    }
    }
    }
    rawFile.send(null);
    }

    so call it like

    —–
    readTextFile(“mytextfile.txt”) or readTextFile(“http://whatever.com/textfile.txt”);

    —-
    Just in case someone might have same problem.

    #341707 Reply

    Brandon
    Member

    Its pretty easy to load an external file:

    var logfile;
    $.get(“myfile.txt”, function(respons) {
    logfile = respons;
    });

    then you can assign the logfile variable anywhere you need it, like:
    $(‘#m1-yourForm-textField1’).val(logfile);

    #342175 Reply

    decten76
    Member

    Hi Mod, Pls close the case. It works. the app used the code has been on ggplay, appstore. thxs

Viewing 12 posts - 1 through 12 (of 12 total)
Reply To: Load local text file into Text field?: CLOSED

You must be logged in to post in the forum log in