1
votes

I have three fields (docid,docname,docref) in my database. I am able to display docname starting with letter a successfully. The next step is to click any values from result, it should display other fields for that record.

For example I get alex, adrain, adams as a result and when I click alex it should show remaining fields (docid,docref) from the database.

Can anyone explain me the idea or samples or code? I have provided my code below, I need code or help for next step, I am stuck here, please.

Adapter

var sel = WL.Server.createSQLStatement("SELECT docname FROM docdb WHERE docname like ? ");
function getdoc(docname) {
    return WL.Server.invokeSQLStatement({
        preparedStatement : sel,
        parameters :[new String(doc + "%")]
    });
}

Adapter Invocation (client)

function searchfunc(){
    var invocationData = {
        adapter : 'docdb',
        procedure : 'getdoc',
        parameters : [$('#docname').val()]
    };

    WL.Client.invokeProcedure(invocationData,{
        onSuccess : loadSQLSuccess,
        onFailure : loadSQLFailure
    });
}

function loadSQLSuccess(result){
    displayresults(result.invocationResult.resultSet);
}

function displayresults(result){
    var ul = $('#table');

    for (var i = 0; i < result.length; i++) {
        var li = $('<li/>').html(result[i].DOCNAME);

        li.append($('<hr>'));
        ul.append(li);
    }
}

Search Form

To Search Based On DOCName <input type="text" id="docname" placeholder="name">
<input type="submit" value="Search"  onclick="searchfunc();"><br><br>
<ul id="table">  </ul>
<input type="button" id="backButtonPage2" class="backbutton" value="BACK" onclick="currentPage.back();" />
1

1 Answers

1
votes

Server/Adapter side:

You should modify your query to also return the docid for example change the SQL statement to

var sel = WL.Server.createSQLStatement("SELECT docid,docname FROM docdb WHERE docname like ? ");

The docname may or may not be unique it depends on how you have your db setup. Second, you should have a separate query to retrieve the document details by docid i.e.:

var getDetailsQuery = WL.Server.createSQLStatement("SELECT docid,docname,docref FROM docdb WHERE docid = ? ");

function getDocDetails(docid) {
    return WL.Server.invokeSQLStatement({
        preparedStatement : getDetailsQuery,
        parameters :[docid]
    });
}

Make sure you add the getDocDetails procedure to your adapter xml file

Client side:

Update your displayresults function as follow:

function displayresults(result){
    var ul = $('#table');

    // clear the list
    ul.html(null);

    for (var i = 0; i < result.length; i++) {
        var doc = result[i];
        // store document details as part of the element, i.e.: data()
        var li = $('<li/>').html(doc.docname).data(doc);

        li.append($('<hr>'));
        ul.append(li);
    }
}

$(document).on('click', 'li', function(){
    var data = $(this).data();

    var invocationData = {
        adapter : 'docdb',
        procedure : 'getDocDetails',
        parameters : [data.docid]
    };

    WL.Client.invokeProcedure(invocationData, {
        onSuccess : getDocDetailsSuccess,
        onFailure : getDocDetailsFailure
    });
});

function getDocDetailsSuccess(result) {
    // do your things
}

function getDocDetailsFailure(error) {
    // handle error
}