SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    Scotland
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to get a dynamic value into a literal array?, please help.

    There are two arrays named myContacts and getRes. When I use myContacts array it works fine. When I assign some value for getRes array which is exactly similar format like mycontacts array, but it’s a dynamic value which I already checked no error In the data content. But am facing problem with dynamic value which gave me not same as myContact. tq


    var myContacts = "";
    var getRes = "";

    myContacts = [
    { id: 0, fname: "Dan", lname: "Curtis", nname: "staff" },
    { id: 1, fname: "Dugald", lname: "Curtis", nname: "staff" },
    { id: 2, fname: "Janet", lname: "Sherrat", nname: "staff" },
    { id: 3, fname: "Caroline", lname: "Buxton", nname: "staff" },
    ];
    alert("Original: =" + myContacts.length);
    alert(myContacts[0]);

    getRes = response.split("~"); // Dynamic value same as above myContacts format

    //following is the actual data which i receive dynamically, just to show what i receive dynamically.
    //{ id: 0, fname: "Dan", lname: "Curtis", nname: "staff" },
    //{ id: 1, fname: "Dugald", lname: "Curtis", nname: "staff" },
    //{ id: 2, fname: "Janet", lname: "Sherrat", nname: "staff" },
    //{ id: 3, fname: "Caroline", lname: "Buxton", nname: "staff" }

    alert("Split: =" + getRes.length);
    alert(getRes[0]);

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Is the value of response a string before it is split? If so, then it will be an array of strings after it's split.

    You may want to investigate using JSON techniques to convert the data back in to array-like values.
    See: http://www.json.org/js.html
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    Scotland
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i try following method. its not working. please give me some tips on it.

    function Student(first, last, level, grades) {
    this.firstName = first;
    this.lastName = last;
    this.gradeLevel = level;
    this.grades = grades;
    }

    // Offer a different object structure for JSON serialization
    Student.prototype.toJSON = function () {
    var gpa = 0,
    i = this.grades.length;

    while (i--) {
    gpa += this.grades[i];
    }

    return {
    name: this.lastName + ', ' + this.firstName,
    gpa: ((gpa / this.grades.length) / 25).toFixed(1)
    };
    };

    var students = [];
    students.push( new Student("John", "Schmidt", 1, [95,90,92,87,100,100]) );
    students.push( new Student("Irene", "Gamel", 1, [72,70,88,76,100,95]) );
    students.push( new Student("Francis", "Yeh", 1, [53,67,100,0,78,88]) );

    var studentJSON = YAHOO.lang.JSON.stringify(students);

    // studentsJSON now contains the string
    // [{"name":"Schmidt, John","gpa":"3.8"},{"name":"Gamel, Irene","gpa":"3.3"},{"name":"Yeh, Francis","gpa":"2.6"}]


    Following is my full script

    <script type="text/javascript">
    YAHOO.example.FnMultipleFields = function () {
    var myContacts = "";

    //function Student(first, last, level, grades) {
    function user(idstr, fnamestr, lnamestr, nnamestr) {
    this.idval = idstr;
    this.fnameval = fnamestr;
    this.lnameval = lnamestr;
    this.nnameval = nnamestr;
    }

    // Offer a different object structure for JSON serialization
    user.prototype.toJSON = function () {
    return {
    id: this.idval,
    fname: this.fnameval,
    lname: this.lnameval,
    nname: this.nnameval
    };
    };

    var div = document.getElementById('container');
    var handleSuccess = function (o) {
    YAHOO.log("The success handler was called. tId: " + o.tId + ".", "info", "example");
    if (o.responseText !== undefined) {
    response = o.responseText;
    response = response.replace(/_/g, '"');
    myContacts = response.split("~");
    var users = [];
    for (var i = 0; i < myContacts.length; i++) {
    var value = myContacts[i];
    users.push(new user(myContacts[i]));
    }
    var usersJSON = YAHOO.lang.JSON.stringify(users);
    div.innerHTML = myContacts;
    }
    }

    var handleFailure = function (o) {
    YAHOO.log("The failure handler was called. tId: " + o.tId + ".", "info", "example");
    if (o.responseText !== undefined) {
    alert(o.statusText);
    }
    }

    var callback =
    {
    success: handleSuccess,
    failure: handleFailure,
    argument: { foo: "foo", bar: "bar" }
    };

    //var sUrl = "assets/get.php?username=anonymous&userid=0";
    var sUrl = "getdata.aspx";
    var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);

    // Define a custom search function for the DataSource
    var matchNames = function (sQuery) {
    alert("First" + myContacts.length);
    // Case insensitive matching
    var query = sQuery.toLowerCase(),
    contact,
    i = 0,
    l = myContacts.length,
    matches = [];

    // Match against each name of each contact
    for (; i < l; i++) {
    contact = myContacts[i];
    //alert(contact);
    if ((contact.fname.toLowerCase().indexOf(query) > -1) ||
    (contact.lname.toLowerCase().indexOf(query) > -1) ||
    (contact.nname && (contact.nname.toLowerCase().indexOf(query) > -1))) {
    matches[matches.length] = contact;
    }
    }

    return matches;
    };

    // Use a FunctionDataSource
    var oDS = new YAHOO.util.FunctionDataSource(matchNames);
    oDS.responseSchema = {
    fields: ["id", "fname", "lname", "nname"]
    }

    // Instantiate AutoComplete
    var oAC = new YAHOO.widget.AutoComplete("myInput", "myContainer", oDS);
    oAC.useShadow = true;
    oAC.resultTypeList = false;


    // Custom formatter to highlight the matching letters
    oAC.formatResult = function (oResultData, sQuery, sResultMatch) {
    var query = sQuery.toLowerCase(),
    fname = oResultData.fname,
    lname = oResultData.lname,
    nname = oResultData.nname || "", // Guard against null value
    query = sQuery.toLowerCase(),
    fnameMatchIndex = fname.toLowerCase().indexOf(query),
    lnameMatchIndex = lname.toLowerCase().indexOf(query),
    nnameMatchIndex = nname.toLowerCase().indexOf(query),
    displayfname, displaylname, displaynname;

    if (fnameMatchIndex > -1) {
    displayfname = highlightMatch(fname, query, fnameMatchIndex);
    }
    else {
    displayfname = fname;
    }

    if (lnameMatchIndex > -1) {
    displaylname = highlightMatch(lname, query, lnameMatchIndex);
    }
    else {
    displaylname = lname;
    }

    if (nnameMatchIndex > -1) {
    displaynname = "(" + highlightMatch(nname, query, nnameMatchIndex) + ")";
    }
    else {
    displaynname = nname ? "(" + nname + ")" : "";
    }

    return displayfname + " " + displaylname + " " + displaynname;

    };

    // Helper function for the formatter
    var highlightMatch = function (full, snippet, matchindex) {
    return full.substring(0, matchindex) +
    "<span class='match'>" +
    full.substr(matchindex, snippet.length) +
    "</span>" +
    full.substring(matchindex + snippet.length);
    };

    // Define an event handler to populate a hidden form field
    // when an item gets selected and populate the input field
    var myHiddenField = YAHOO.util.Dom.get("myHidden");
    var myHandler = function (sType, aArgs) {
    var myAC = aArgs[0]; // reference back to the AC instance
    var elLI = aArgs[1]; // reference to the selected LI element
    var oData = aArgs[2]; // object literal of selected item's result data

    // update hidden form field with the selected item's ID
    myHiddenField.value = oData.id;

    myAC.getInputEl().value = oData.fname + " " + oData.lname + (oData.nname ? " (" + oData.nname + ")" : "");
    };
    oAC.itemSelectEvent.subscribe(myHandler);

    return {
    oDS: oDS,
    oAC: oAC
    };
    } ();
    </script>


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
  •