Results 1 to 6 of 6

Thread: [Answers] JS

  1. #1
    Lianja MVP
    Join Date
    Dec 2012
    Location
    Croatia, Zagreb
    Posts
    1,135

    [Answers] JS

    Q:
    I have a javascript variable in my webview section by the name of "message". This variable has a string. How do I assign this value to one of my VFP variables?
    A:
    Strings can be delimited with double-quotes "", single quotes '', or square brackets []. This allows you to have quotes within quotes.
    Code:
    Lianja.execute("VFPVariable  = '" + message + "'");
    and
    Code:
    Lianja.execute("VFPVariable  = ''");


    Q:
    to include a (local) js file in my .jssp page. I have a file by the name of GpsGate.js in my app and it's under "Javascript Files". How and where do I include this file in the .jssp file?
    A:
    If the js file is in the App's directory (which it is if it's listing it under 'Javascript Files' in the Apps Workspace) you don't need to include the path on the file name



    JavaScript has access to the Lianja system object and its methods: Lianja.getelementbyid(), Lianja.evaluate(), Lianja.execute() etc.
    If it's JavaScript within an rsp script or html page, rather than a js script, call getelementbyid() using evaluate()/execute(), e.g.
    Code:
    Lianja.execute('Lianja.getelementbyid("page1.section1").caption = "New Caption"');        
    m_section = Lianja.evaluate('Lianja.getelementbyid("page1.section1").caption');        
    Lianja.showMessage(m_section);


    Q:
    I want to create a calendar section using the web client. There are no button to click to add the delegate
    A:
    you need to create a javascript custom library for this app (as its a web app) and then just type in the name of the click delegate. This will be called with one argument which is an object containing properties for the cell clicked in the calendar.



    Q:
    otool.addObject(page1);
    A:
    In JavaScript you must use the standard addObject(name, classname) call.



    It seems the names of the objects need to be lowercase not mixed case.



    Don't forget semi colons.
    Also - add the curly brackets in the JavaScript function. If you don't, you may have a problem when you reload the app.



    In dynamic server side javascript pages (.jssp pages) you don't need to use Lianja.getCursor() as you have total control of the Recordset.



    Q:
    Is it possible to reference a textbox in a JavaScript canvas section when called with showDialog()?I know the click event on the button is working via a messagebox() and the code is in the library lib:/lib_dlgJSCanvas1_section1.js.
    A:
    How are you calling Lianja.showDialog()?Yes you can reference the UI control directly after addObject() just as you would in VFP.e.g.dlg_textBox.text = "Hello world";Open the web inspector by right clicking and in the console type window.dlg_textBox to verify the name is correct.The Web Client does not currently handle UI page library pages with Lianja.showDialog() or Lianja.showDialogPanel().You need to use either a .rsp page, a .jssp page or a javascript function e.g "createMyCanvas()" to populate the dialog contents.Also messageBox() is non blocking in the Web Client. You have to use callbacks with a JavasCript function closure to handle the "Ok" "Cancel" buttons.For a blocking messagebox() you can use confirm() in JavaScript.
    Code:
    var r = confirm("Are you sure you want to continue?");
    if (r) {    Lianja.writeOutput("You pressed OK!");} 
    else {Lianja.writeOutput("You pressed Cancel!");}
    And for an inputBox() you can use prompt(). The trouble with all these is that they have a "JavaScript" title which cannot be changed.
    Code:
    var name = prompt("Please enter your name", "Herb");


    Q:
    I am testing the simplest webview I can come up with, but Lianja is not liking it.The inspector is telling me it has a parsing error.
    A:
    What file extension did you use for that file? If it is a dynamic server side JavaScript page it should have a .jssp file extension.
    In JavaScript you should terminate a statement with a ';' After I added ';' to the code no parse errors occurred. I have highlighted these in red. Although some javascript parsers will ignore the fact that the ; is missing, some will complain and if the code in minified then this causes an issue with the parser.
    Code:
    var  updatetext = function() {
        document.getElementById("herbinput").value = "test";};
    
    window.onload = function(){    
       document.getElementById("herbbutton").onclick =  updatetext ;
    };


    Q:
    am I correct in thinking that Lianja.evaluateJavaScript() when called from a client-side delegate will be able to fire server-side code and receive results + errors just as Lianja.evaluate() does?
    A:
    Yes that's how Lianja.evaluateJavaScript() will work. Its the same as Lianja.evaluate() but for running server side code in different scripting languages.



    All topics in [Answers] alphabetically: http://www.lianja.com/community/show...ll=1#post12352
    Last edited by josipradnik; 2016-11-22 at 02:21.

  2. #2
    Lianja MVP
    Join Date
    Dec 2012
    Location
    Croatia, Zagreb
    Posts
    1,135
    Q:
    I put some js functions (f.e. for click or change events) in my WebApp (all in a lib_page1.js file)
    In these functions, I need data access and I intend to do it such way: x = rs.fields("fieldname").value
    Have I to open the database (var db) and to declare the record set (var rs) in every function?
    Or is it possible to have global vars?
    And where had I to put the global vars and the openDatabase and openRecordSet commands?
    A:
    You can access the data in use in your App using 'local data cursors'.
    For example, if my App uses the southwind database and the customers table (in Page1.Section1), I don't need to open the southwind database or a customers recordset in my JavaScript event delegates, I can use Lianja.getCursor() and update a value:
    Code:
    custcurs = Lianja.getCursor("customers");
    custcurs.setData("Region","West");
    custcurs.update();
    custcurs.refresh();
    Lianja.get("page1.section1").refresh();
    You can also use Lianja.createCursor(database as Character, table as Character) if you need a cursor to a table not currently open in your App.
    The Lianja.OData_XXX functions can also be used for custom CRUD operations.
    JavaScript variables declared as document.varname can be accessed globally, e.g.:
    Code:
    document.myvar = "Hello World";
    elsewhere in App:

    Code:
    messageBox(document.myvar);
    If you want to access the data currently displayed in the client, you can use Lianja.get("page.section.field").value (or Lianja.get("page.section.field").text) for formitems in a Form Section and you can use name.value or name.text for controls in a Canvas Section (change the name from fieldXX to make sure they have a unique name).

    If you want to access the buffered data, use Lianja.getCursor("table") as I mentioned before.

    If you want to access a table not currently open in your App, you can use Lianja.createCursor(database as Character, table as Character).

    If you want to update data outside the client UI, you can call a server-side program (Lianja/VFP non-UI code) with Lianja.evaluate("myprog()") then requery the local cursor and refresh UI client display if affected.



    When building Web Apps in Lianja the norm is to write your client side delegate code in JavaScript and your server side procedures in Lianja/VFP.

    In Lianja v2.0 we have implemented a new LianjaWebFramework client side function called Lianja.evaluateJavaScript() which is used for calling server-side JavaScript code.

    Note that the Google V8 JavaScript engine is embedded into the Lianja Cloud Server so this is the same JavaScript engine that is used in node.js. This provides you with the ability to write end-to-end JavaScript code on the client and server and generate dynamic pages with .jssp pages also. So if you are a JavaScript guy this is for you.

    Code:
    // Server: The file myfunction.js should be in the app or library directory and contain a function with the 
    //         same name as the file.
    function myfunction(arg)
    {
        // do something useful...
        return arg;
    };
    
    // Client: Call a server-side JavaScript function called myfunction from a client delegate
    var result = Lianja.evaluateJavaScript("myfunction('hello world')");
    Note that if the file myfunction.js file does not exist in the app or library directory and the file server_functions.js exists then the function myfunction is assumed to be defined in the server_functions.js file.



    Provide transparent remote function calls to server-side code in Lianja/VFP, JavaScript, PHP or Python by setting up an app.conf file (in the App directory) which exposes the functions that exist on the server and what scripting language that they are written in.

    It will provide you with the ability to call functions in your client code that execute seamlessly on the server without having to use Lianja.evaluate() or Lianja.evaluateJavaScript() or Lianja.evaluatePHP() or Lianja.evaluatePython().



    You escape string quotes in JavaScript by prefixing them with \ e.g \' or \".
    This is standard JavaScript.
    I would not recommend this convoluted way of calling functions contained within a library.
    I would wrap them by a separate .prg file.



    "javascript variable scope".

    Lianja.evaluate() needs to be able to access the variable based on where it is declared.

    Write your own JavaScript function and experiment with trying to access variables outside of it.



    Q:
    In my app, I have a canvas section that contains a combo box, textbox and command buttons.
    Is it possible to reference the text in the textbox from a command button?
    A:
    A UI control that is in a javascript canvas section can be referenced using the "id" of the UI control.
    So if you have a text field with the id mytextfield you can just reference myfield.text



    Lianja v2.0 Beta16 introduces a simplified approach for calling server-side business procedures directly from the JavaScript client.
    This is accomplished by defining the libraries, functions and scripting language for the functions in the exports.conf file which can reside in the app and/or the library directory.
    Example exports.conf file:

    Code:
    # This is an example exports.conf file
    #
    # Each line describes a function that will be available directly from JavaScript code in the client
    #
    #  library, function, type (source library, name of the function, language vfp or javascript)
    #  (php and python will be added later)
    #
    # or
    #
    # library, function (implied type of vfp)
    #
    # or
    #
    # function (file expected to exist with a .dbo extension)
    #
    mylib,myfunc1,vfp
    myjslib,myfunc2,javascript
    myfunc3,vfp
    myfunc4
    Now in your App logic running in the Lianja JavaScript Web/Mobile App you can call the server-side functions directly:

    Code:
    // Note that arguments are automatically converted into the correct format for the 
    // remote function call and the function call is proxied
    // using a synchronous  Lianja.evaluate() call
    var result = myfunc1("hello world", 2015, true);
    var result2 = myfunc2();
    var result3 = myfunc3();
    
    // When calling remote functions with objects as parameters the object is automatically 
    // converted to JSON and send base64 encoded
    // This allows you to send and receive complete JSON encoded objects between the 
    // client and the server
    var result4 = myfunc4( {"name":"barry", "company":"Lianja" });


    Q:
    I would like to change the section header.
    I would like to update a canvas label with one of the parsed values.

    Code:
    Lianja.get("page1.section1.lblName").caption = alltrim(parseddata.name);
    How do I refresh the section or the label in the web view/browser to reflect the changed value?
    A:
    The ID (name) of the UI object in a JavaScript canvas section is a javascript object. So if you have a label called m_label you can set the text using:

    Code:
    m_label.text = "Hello world";
    "m.varname" is a VFP convention, not JavaScript. Try removing the "m.".



    Q:
    In the section header caption, I add something similar to {m.headercaption}.
    In the main .js file, I add a declaration before all code similar to var headercaption = '';
    When I want to update the caption, I would set the value headercaption = "some text" and refresh the section.
    The header remains as {headercaption}
    how I should handle the macros?
    In the .js file before any code, the following is used:

    Code:
    var headercaption = " ";
    A:
    remove var.
    Code:
    window.headercaption = "hello world";
    Macros work when bound to server-side expressions.
    The macros in section headers use Lianja.evaluate().
    These don't handle client side javascript variables.
    I have implemented support for section captions in the Web/Mobile client now in v2.0. See below.




    You can also get/set the caption of a page also in the next v2.0 beta build. This provides you with availability to programmatically change the page and sections headers in JavaScript code.



    All topics in [Answers] alphabetically: http://www.lianja.com/community/show...ll=1#post12352
    Last edited by josipradnik; 2016-11-22 at 02:13.

  3. #3
    Lianja MVP
    Join Date
    Dec 2012
    Location
    Croatia, Zagreb
    Posts
    1,135
    Q:
    How would I do the following in Javascript?
    Code:
    oGrid = Lianja.get("page1.section2").grid
    nActiveRow = oGrid.ActiveRow
    nCommNum = oGrid.item(oGrid.ActiveRow,3)

    if we need to get the value for a particular column on the current row, how might we do that?
    My grid is bound to a virtual table.
    Say VT name is QWERTY, active row is 5 and column data is in column 8.
    A:
    Web Client grid does not yet support the accessing of individual cells of the grid like that. furthmore it always uses pagination by design to provide acceptable performance on web and mobile devices.
    The grid columns are data bound. Selecting a row makes that record active in the cursor. So you reference the columns from the cursor.
    Code:
    var orders = Lianja.getCursor("orders");
    var customerid = orders.getData("customerid");
    Double clicking a row in the web client will edit the row.
    Clicking a grid row will select it. The record is made current in the cursor. The related sections are then refreshed.
    You can click on a row and use the JavaScript console to access the cursor as I previously described to satisfy yourself.



    JavaScript console in the browser. You can inspect the client environment



    Q:
    If I want to not worry with web compability it is much better to develop everything in Javascript. Is this also applicable to the development for mobile devices?
    A:
    Python, PHP and JavaScript are case sensitive. Lianja/VFP is case insensitive, so Lianja.showDocument() is what you need to use.
    Web and Mobile use JavaScript on the client (delegates) but the client side code can call server side code written in Lianja/VFP or JavaScript. We will be adding PHP and Python into the cloud server in a point release after v2.0 is released.



    Lianja framework includes most of the VFP built in functions in JavaScript and also that the LOM (Lianja Object Model) is available in desktop, web and mobile apps in all of the supported scripting languages.
    The visual nature of Lianja development does not require extensive JavaScript coding so let's not make it look like it does.
    Apps are built out of pages and pages are built out of sections, all visually setting attributes to affect the appearance and behavior of the UI.
    The only place you really need to do any JavaScript programming is if you need to handle special actions through delegates that act as a proxy to call server side Lianja/VFP procedures. This too was simplified in v2.0 with automatic proxying to server side business procedures.



    Any server side business logic procedures can be used in validation expressions, used in {...} macros, called using Lianja.evaluate() or setup as remote procedure calls
    Building Web and/or mobile Apps requires a separation between the UI presentation and the server side business logic.



    JavaScript variables are scoped just like local in VFP.

    if in a JavaScript function you type

    Code:
    var var1;
    then its its local to that function.

    It is good practice to place your own global variables in a global object called public. This prevents any references to the wrong variable from being made. It also simplifies debugging as you can just type public in the JavaScript console which will display the public object and all its members.
    Code:
    public = {};
    public.var1 = "hello world";
    public.var2 = 12345;


    Q:
    When I click any menu item, the console reports the following error:
    **** Lianja JavaScript error ****
    Traceback (most recent call last):
    <global>() at 1ReferenceError: Can't find variable: Client_MainHeader_customsectionmenu

    Here is the code in the function:
    Code:
    function client_mainheader_customsectionmenu(cItem)
    {
            cItem = ALLTRIM(UPPER(cItem));
            messagebox("Menu item is: " + cItem);
    };

    Here are the errors being reported:
    Traceback (most recent call last):
    client_mainheader_customsectionmenu(cItem = 'Reset') at 3
    <global>() at 1
    ReferenceError: Can't find variable: ALLTRIM

    **** Lianja JavaScript error ****
    Traceback (most recent call last):
    client_mainheader_customsectionmenu(cItem = 'Reset') at 3
    <global>() at 1ReferenceError: Can't find variable: UPPER

    A:
    Most likely would be the '}' missing from the end of a function or a line that needs to be terminated missing the ;.
    Lianja functions need to be in lowercase in order to work properly.
    JavaScript is a case sensitive scripting language whereas Lianja/VFP is case insensitive so you need to use the exact function name as it is documented.



    Q:
    I am trying to check if a section is visible or not and the boolean comparison seems to be the issue.

    Code:
    if Lianja.get("client.secSearch").visible == false

    How should boolean values be fully written for comparison?
    I have tried various ways including: = .F., == false, === false
    A:
    Try this:
    Code:
    if (Lianja.get("client.secSearch").visible == 0)


    All topics in [Answers] alphabetically: http://www.lianja.com/community/show...ll=1#post12352
    Last edited by josipradnik; 2016-11-22 at 02:04.

  4. #4
    Lianja MVP
    Join Date
    Dec 2012
    Location
    Croatia, Zagreb
    Posts
    1,135
    Q:
    Say I have a .prg file set as:
    Code:
    proc A()
    ...
    endproc
    
    proc B()
    ...
    endproc
    
    proc C()
    ...
    endproc
    
    ...
    In VFP, if it's not a library file, I can use something similar to:
    Code:
    set procedure to some_prgfile
    C()
    release procedure

    How do I call a specific VFP procedure from a .prg file from javascript, such as C()?

    I know that if I have a file named code.prg, I can use Code:
    Code:
    Lianja.execute("do code.prg");

    This seems to only work if 'proc' is not declared and is to execute all code in the file (registration web app has an example of this).

    A:
    You could do this:
    Code:
    //some_prgfile.prg
    proc A()
    return 'retval from a'
    endproc
    
    proc B()
    return 'retval from b'
    endproc
    
    proc C()
    return 'retval from c'
    endproc
    
    parameters funcname
    return &funcname()
    //end

    Then in JS:

    Code:
    var retval = Lianja.evaluate('some_prgfile("c")');

    passing further parameters if required by the individual functions.
    Here, some_prgfile is the .prg function library, compiled and deployed as some_prgfile.dbo to the Lianja Cloud Server. You pass in the name of the function (here c), which is picked up by the PARAMETERS statement in the function library and the function is called. If the function itself requires parameters, then you pass them in too. PCOUNT() will tell you how many parameters have been passed.
    It needs to be in C:\Lianja\cloudserver\tenants\public\wwwroot\apps\<app_name> for the Web Client - is it there too?

    You should be able to enter your equivalent of

    Code:
    Lianja.evaluate('some_prgfile("c")')

    in the browser console and see what it comes back with.



    I would put a check in to test whether the code is running on the desktop or live in the Web Client and just use the {} macro in the Web Client.

    This is being called from a commandbutton click. I'm passing the txtsearch.text directly. The [] can be used as string delimiters 'in dev' as this is handled by the local Lianja/VFP scripting.
    Code:
    
    ////////////////////////////////////////////////////////////////
    // Event delegate for 'click' event
    function page1_section1_field2_click()
    {
            if (typeof LianjaAppBuilder === 'object')
            // in dev
            {
            var result = Lianja.evaluate('lib_webtest("get_brnum(['+txtsearch.text+'])")');
            }
            else
            // in live web/cloud
            {
            var result = Lianja.evaluate('lib_webtest("get_brnum(\'{txtsearch.text}\')")');
            }
    
            Lianja.writeLog(result);
    };
    In my original post about passing parameters into the library prg, when I mentioned PCOUNT(), I envisaged passing the parameters in individually to the library, rather than including them as a single parameter.

    For example, if this is my procedure library, lib_webtest.prg:
    Code:
    proc get_brnum
    parameters ptext
    return ptext + " was passed"
    endproc
    
    parameters funcname, para1
    do case
            case pcount() = 1
                    return &funcname()
            case pcount() = 2
                    return &funcname(para1)
    endcase

    I can pass 2 parameters to lib_webtest and it will treat the 2nd as a parameter to the function name in the 1st. Additional parameters can be added and handled with further PCOUNT() cases.

    So my button click code becomes:
    Code:
    ////////////////////////////////////////////////////////////////
    // Event delegate for 'click' event
    function page1_section1_field1_click()
    {
            if (typeof LianjaAppBuilder === 'object')
            {
            var result = Lianja.evaluate('lib_webtest("get_brnum","' + txtsearch.text + '")');
            }
            else
            {
            var result = Lianja.evaluate('lib_webtest("get_brnum","{txtsearch.text}")');
            }
    
            Lianja.writeLog(result);
    };

    and is probably more readable.



    All topics in [Answers] alphabetically: http://www.lianja.com/community/show...ll=1#post12352
    Last edited by josipradnik; 2016-11-22 at 01:57.

  5. #5
    Lianja MVP
    Join Date
    Dec 2012
    Location
    Croatia, Zagreb
    Posts
    1,135
    Q:
    I assume the server-side javascript file is '.js' but how do I create a js file from the app builder? no matter where I click in the 'Files' section and choose 'new file', I always get a .prg file. I ended up creating the js file manually in the file system and then had to close + open the app to see the file under 'JavaScript Files' (clicking 'refresh' didn't work).
    A:
    When entering the filename postfix .js



    Q:
    Once I had the js file & named it properly I could call my function just fine using evaluateJavaScript but what makes this js server-side as it is just another js file in the same folder as my click-handler js file?
    Does evaluateJavaScript need the cloud server to work?
    A:
    This is for web/mobile apps so look at web app view and test in there using the JavaScript console if need be to test call your server-side functions, you don't use this functionality in a desktop app and that us not a client/server web or mobile app, it's all running in one exe.



    Q:
    Is there any way to debug server-side js files
    A:
    Test from the JavaScript console. No there is no remote debugger.



    Q:
    to import an external JS library into lianja
    A:
    Code:
    var mylib = require("some_filename.js");
    var mylib = require_once("some_filename.js");
    include("some_filename.js");
    include_once("some_filename.js");
    require() and require_once() return the module.exports object for consistency. require_once() keeps an internal cache and knows if the library has been loaded already so its module.exports object is used rather than loading the library again. The same goes for include_once().
    Also remember that filenames can be prefixed with app:/ or lib:/ which is expanded for you to the full filename path.



    Q:
    I understand the concept of calling a server script from the client and I can see that evaluateJavacsript will allow js code on both sides to work just fine.
    What I don't understand is which files are sent to the client and which stay on the server because both are defined in app builder in exactly the same way so there is nothing so say which is a 'server' file and which is a 'client' file.
    A:
    In development mode all JavaScript is referenced in the app or library directories as the "server" is embedded in the app builder.

    When you switch to web app view and then "preview" your server side JavaScript is called using a RPC to the cloud server.

    You can use Lianja.evaluateJavaScript() or setup the exports.conf file to handle the mapping from client to server function calls.
    When using data binding you don't need to perform any CRUD operations programmatically as it is done for you.
    The only time I would perform evaluate() or evaluateJavaScript() RPCs is for validation or custom actions that require specialized CRUD code to be run on the server.

    When binding URLs to webviews you can use {...} macros which are substituted as a Webview is refreshed.
    The data binding hides a lot of the complexity of client/server operations.



    Q:
    In a js delegate this code gives the error "TypeError: undefined is not a constructor (evaluating 'poSection.add()') "
    Digging around in the web debug I can see that poSection appears to be a valid section object and I can see a few other properties but most fail eg '.count'
    Code:
    var poSection = Lianja.get("New_Purchase_Order.section1");
    poSection.add();
    A:
    A best practice is to use Lianja.showDocument() which abstracts above all of this coding.



    Q:
    this line of code:
    lianja.execute('lianja.get("sup_Search.sectionPara meter.Valore").text = "' + "Test" + '"');
    type on command line, work fine
    in a javascript script inside a rsp page no...
    A:
    Lianja.execute() runs a command on the server.

    Lianja.get() is a client side API call.

    You are executing Lianja.execute() on the client inside a script tag. The command it executes runs on the server. How can it access the client?
    Work! the only problem?
    Javascript is case sensitive... :-)



    Q:
    Looking at the apps settings in v2.1Beta12, I noticed some options I don't recall seeing.
    HTML editor - has an option for Desktop, web or Mobile.
    There is also another setting option called include in HTML Head.

    So that got me thinking.....

    Is there a combination that I could use to embed Jquery in a custom javascript section?
    A:
    A custom JavaScript section is created dynamically and has no meaningful HTML content.
    To use jquery you would just need a WebView section.



    Lianja v3.0 now includes a GUI JavaScript debugger.

    This will be triggered when an error occurs in any of your JavaScript delegates, when you choose "Debug..." from the "Program" menu (or ModeBar button) and choose a JavaScript file, or when a debug() function call is encountered in any JavaScript code. Note that debug() is a noop in the Web/Mobile clients.





    Q:
    In a jssp page, I have created a simple navigation section.
    All I am looking to do is step through a table.
    I am missing something though.

    I can MoveFirst(), MoveLast() without an issue.

    MoveNext(), MovePrior(), MoveRelative() all seem to start from the first record each time they are clicked.

    How do I keep the record pointer from resetting itself.

    Below is my very simple jssp page. I am purposely mixing the Lianja and javascript code so I can understand how they interact.

    Code:
    <%@ Language=JavaScript %>
    <html>
    <head>
    <script>
    <%
    db = Lianja.openDatabase("southwind");
    rs = db.openRecordSet("example");
    
    
    %>
    
    
    var moveLast = function(){
    <%
    rs.moveLast();
    firstname = rs.field("FIRST_NAME").value;
    lastname = rs.field("LAST_NAME").value;
    print('document.getElementById("firstname").value = "'+firstname+'";')
    print('document.getElementById("lastname").value = "'+lastname+'";')
    
    %>
    };
    
    
    var moveFirst = function(){
    <%
    rs.moveFirst();
    firstname = rs.field("FIRST_NAME").value;
    lastname = rs.field("LAST_NAME").value;
    print('document.getElementById("firstname").value = "'+firstname+'";')
    print('document.getElementById("lastname").value = "'+lastname+'";')
    %>
    };
    
    
    var moveNext = function(){
    <%
    rs.moveNext();
    firstname = rs.field("FIRST_NAME").value;
    lastname = rs.field("LAST_NAME").value;
    print('document.getElementById("firstname").value = "'+firstname+'";')
    print('document.getElementById("lastname").value = "'+lastname+'";')
    %>
    };
    
    
    var movePrevious = function(){
    <%
    rs.movePrevious();
    firstname = rs.field("FIRST_NAME").value;
    lastname = rs.field("LAST_NAME").value;
    print('document.getElementById("firstname").value = "'+firstname+'";')
    print('document.getElementById("lastname").value = "'+lastname+'";')
    %>
    };
    
    var moveRelative= function(){
    <%
    rs.moveRelative(5);
    firstname = rs.field("FIRST_NAME").value;
    lastname = rs.field("LAST_NAME").value;
    print('document.getElementById("firstname").value = "'+firstname+'";')
    print('document.getElementById("lastname").value = "'+lastname+'";')
    %>
    };
    
    
    window.onload = function(){
    document.getElementById("btnMoveLast").onclick = moveLast;
    document.getElementById("btnMoveFirst").onclick = moveFirst;
    document.getElementById("btnMoveNext").onclick = moveNext;
    document.getElementById("btnMovePrevious").onclick = movePrevious;
    document.getElementById("btnMoveRelative").onclick = moveRelative;
    
    };
    
    
    
    </script>
    
    </head>
    
    <body>
    
    <%
    
    print('<label for ="firstname" >First Name</label>')
    print('<input type = "text" id = "firstname" >')
    print('<br>')
    print('<label for ="lastname" >Last Name</label>')
    print('<input type = "text" id = "lastname">')
    print('<br>')
    print('<label for ="recno" >Recno </label>')
    print('<input type = "text" id = "recno">')
    print('<br>')
    %>
    <input type ="button" id = "btnMoveFirst" value ="First">
    <input type ="button" id = "btnMoveLast" value ="Last">
    <input type ="button" id = "btnMoveNext" value ="Next">
    <input type ="button" id = "btnMovePrevious" value ="Previous">
    <input type ="button" id = "btnMoveRelative" value ="Relative 5">
    
    
    </body>
    
    
    
    </html>



    How do I create a JavaScript function that can step through a table and populate my html <input> tags.
    A:
    Everything between <% and %> is processed and executed on the server as the html is being generated and sent to the WebView.

    so the html you are generating does not look correct to me.
    You build it as an App with one WebView that has the LianjaWebFramework in it and you can use Lianja.getCursor() etc to navigate the data.



    Q:
    I Have created a simple JavaScript function that I am using to populate a ShowDialog window.
    I am having an issue trying to get a click event working properly.

    I have tried both an anonymous function on a named function, but no luck.

    Below is the code with the named function.

    What seems to occur is that click events is triggered when dialog function is called, but then does not respond when I click the button.


    Code:
    function myDialog(){
    
    var buttontest = function(){
    myTextBox.text = "test";
    }
    
    
    var cont = createObject("myContainer", "container");
    cont.backcolor = "silver";
    cont.autosize = 1;
    cont.layout = "horizontal";
    cont.addObject("myTextBox", "textbox"); 
    
    cont.addObject("myButton", "commandbutton");
    myButton.text = "Update";
    myButton.click = buttontest();
    
    
    return cont;
    };


    Is there a different way I should be calling the click event on the button?
    A:
    you should be assigning buttontest not buttontest()

    buttontest is the address of the function whereas buttontest() executes it and assigns the return value which in your case is undefined.
    names of the objects need to be lowercase not mixed case.



    There are times when you may need to use a percentage sign in your app.

    In my case, I am filtering a grid using the like statement against SQL Server.
    since the % sign is a mod function in javascript, you need to escape it out using %25.
    So in my case, I concatenate the value to my textbox value.

    Code:
    function page1_section1_field2_click()
    {
    var m_filter1 = txtfilter.value;
    var m_filter = '%25'+m_filter1+'%25';
    
    Lianja.get("page1.Al_SQLServers").filter ="computername like '"+m_filter+"'";
    Lianja.get("page1.Al_SQLServers").refresh();
    
    };


    Q:
    I noticed you switched from VFP to JSCRIPT, any particular reason why?
    A:
    I chose JavaScript since I want the app to be able to run in a web browser.
    Since I used txt1.value and txt2.value as parameters to the call, I wanted to make sure the browser would understand those values.



    VFP and JS, side-by-side
    http://www.lianja.com/community/show...ll=1#post14016



    If you want to test some web code snippets on the fly look at https://jsfiddle.net/
    CSS
    HTML
    JS (w/ jQuery)

    Although you have not LOM in this case, it can help to learn JavaScript, CSS and HTML syntax.



    All topics in [Answers] alphabetically: http://www.lianja.com/community/show...p?2717-Answers
    Last edited by josipradnik; 2016-11-22 at 01:53.

  6. #6
    Lianja MVP
    Join Date
    Dec 2012
    Location
    Croatia, Zagreb
    Posts
    1,135
    Q:
    I created a file called javascriptfuntions.js.
    In that file, I copied the example from the road map.
    Code:
    function page1_myDialog()
        var cont = createObject("myContainer", "container");
        cont.backcolor = "white";
        cont.addObject("myTextBox", "textbox");
    return cont;
    In the console - I tried to loadlibrary('javascriptfunctions.js') - but it threw a parse error.
    So I added the curly brackets around the javascript function as you would normally do, but still no luck.

    A:
    Yes, it should have the curly brackets - I have corrected the example in the roadmap.
    Using a JavaScript generated container in a Lianja.showDialog() or Lianja.showDialogPanel() is supported in the web client. Your post title mentions VFP



    Q:
    add a blank grid section to an application.
    In a JavaScript application, I am trying to add a blank grid and then populate it later from an array using additems().

    A:
    Here's what I did in on app:
    1) created a table with the required fields and also had a cSession field of C(26).
    2) in the UI, I added a property to a public var oApp of cSessionID
    3) this property was, on the INIT of the UI app, set to guid() (this was in VFP, but should work here)
    4) I then set a filter on the grid of: cSessionID = oapp.cSessionID -- which meant the grid was showing empty
    5) Then I could "create" records from anywhere, making certain to use oapp.cSessionID as the cSessionID of the table.
    The same could be done with a VT and a parameter for cSessionID of course.
    In the close of the database (assuming you don't close it in the UI), you can delete for cSessionID = oapp.cSessionID.



    all objects in a JavaScript canvas must be named uniquely, throughout the application
    . Thus, from any page you are on, the lbltext object will be available.
    This is due to the global nature JavaScript: either a var is scoped within the function, or is global everywhere.


    Q:
    For an RSP page - I can use the following code
    HTML Code:
    Code:
    private m_embedded = getParameter("embedded", "false")
    private m_libpath
    set macros on
    set fixed on 
    if isServer()
        m_libpath = "../../library/"
    else
        m_libpath = "lib:/"
    endif
    if m_embedded = "false"
        ? ''
        ? ''
        ? ''
        ? 'http://&m_libpath/jquery-1.10.2/jquery-1.10.2.min.js'
        ? 'http://&m_libpath/bootstrap-3.3.4/js/bootstrap.min.js'
        ? ''
        
    endif
    How do I accomplish the same thing for a .JSSP?

    A:
    Instead of using ? to output text you use print() pr() or echo() in JavaScript.
    The following associative arrays are available to you in JavaScript on each page request.
    $_REQUEST
    $_SERVER
    $_COOKIE
    $_ARGS
    $_GET
    $_POST
    $_FILES
    $_SESSION
    So for example you can reference $_ARGS["embedded"]
    As per your requirements I have added:
    Lianja.getUrl( "url..." ) to Lianja 3.4.1 which returns a string which is returned from the remote server.
    This is available in desktop and web framework.
    Note that you can also use this on the client.
    Lianja.OData_Read(url, callback);
    e.g.
    Code:
    Lianja.OData_Read("http://xxxx...", function(result)
     {
     });
    This function returns JSON and can handle any URL.
    You can use Lianja.evaluate( "vfp_expression" ) to evaluate any function not available in jssp pages.



    Q:
    I tried adding a "hello word" into a commandbutton.click delegate, but it keeps returning a "unterminated string" error"



    A:
    You are writing JavaScript in a .prg library file.
    You should write JavaScript in a .js library file.



    Lianja provides many built-in Javascript functions. In terms of using Javascript libraries the CommonJS module standard is handled in desktop, web and mobile.
    The CommonJS group defined a module format to solve JavaScript scope issues by making sure each module is executed in its own namespace.
    This is achieved by forcing modules to explicitly export those variables it wants to expose to the “universe”, and also by defining those other modules required to properly work.

    To achieve this CommonJS gives you two tools:


    1. the require() function, which allows to import a given module into the current scope.
    2. the module object, which allows you to export something from the current scope.


    Further details of the Lianja/VFP functions available in JavaScript/TypeScript can be found at:
    https://www.lianja.com/doc/index.php.../VFP_functions

    The documentation on JavaScript Server Pages (.jssp) includes an example for require() with module.exports.
    https://www.lianja.com/doc/index.php...t_Server_Pages




    All topics in [Answers] alphabetically: http://www.lianja.com/community/show...ll=1#post12352

    These answers are also systematized on the site "Lianja developer": https://lianjadeveloper.wordpress.com/category/js/

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Journey into the Cloud
Join us