Web Analytics Made Easy -
StatCounter Javascript array question?? - CodingForum

Announcement

Collapse
No announcement yet.

Javascript array question??

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Javascript array question??

    Hi Peers,
    I am capturing query data into different Javascript arrays and that is the only way i have for now...anyway ..i have what i want in JS arrrays >

    is there a war to perform CRUD operations on those arrays :

    my Arrays look like this :

    A1 : EmpID / A2 : Name / A3: Fistanme..
    A1[0] : 1 / A1[0] : Paul /A1[0] : Newman
    A1[1] : 2 / A1[1] : Paul /A1[1] : Simon

    i want to be able to display the information like and give the ability to create , update or delete the record based on the EmpID unique number:

    EmpID / Name / Firstname
    1 Paul Newman Update ( hyperlink) - Delete ( hyperlink)
    2 Paul Simon Update ( hyperlink) - Delete ( hyperlink)


    i am using Coldfusion CF 7 and i was told since i want to keep the data into JS array that the only way to do it is usinig JAvascript


    any thoughts how can this be done./???

    Thanks

  • #2
    once again, there's probably a fancier way of doing this, but is this what you're talking about?

    Code:
    <html>
    	<head>
    	</head>
    	<body>
    	<div id="links"></div>
    	<script type="text/javascript">
    	var names = [{EmpId:"1",name:"Paul",surname:"Newman"},{EmpId:"2",name:"Paul",surname:"Simon"}];
    	for (var i=0; i<names.length; i++) {
    	current=names[i];
          document.getElementById("links").innerHTML +=current.EmpId+" "+current.name+" "+current.surname+" <a href=http://www.google.com>Update Link</a> " + "<br>";
        } 
    		</script>
    		
    </body>
    </html>
    Last edited by xelawho; Sep 10, 2011, 08:53 PM. Reason: trying to be a little bit fancy

    Comment


    • #3
      Hi xelawho,

      Thanks for the quick reply . I would appreciate small explanation of what you wrote ..to undestand the logic behind...

      i ll try it and let you know if this is what i want

      I appreciate your help man..

      Comment


      • #4
        mmm... let me see if I understand it

        this bit:
        var names = [{EmpId:"1",name:"Paul",surname:"Newman"}, EmpId:"2",name:"Paul",surname:"Simon"}];
        sets up an array of objects

        this bit:
        for (var i=0; i<names.length; i++) {
        loops through those objects

        this bit:
        current=names[i];
        is just a bit of shorthand so you don't have to keep writing "names[i]" over and over

        this bit:
        document.getElementById("links").innerHTML +=current.EmpId+" "+current.name+" "+current.surname+" <a href=http://www.google.com>Update Link</a> " + "<br>";
        finds the div called "links" and adds a string to it - the bits inside the quotes are text, the others are the attributes of the object being examined. The += makes sure that the string is added to the existing contents of the div rather than overwriting it.

        Comment


        • #5
          Hi xelawho,

          thank you . Here whati am trying to do ..
          let say i have 3 arrays one A1 holds IDs 2nd one A2 holds names and 3rd one A3 holds lastnames

          Let suppose that A1[0] A2[0] A3[0] hols a signle record information
          empid1 / name1 / lastname1

          A1[1] A2[1] A3[1] holds 2 nd record :empid2 / name2 / lastname2

          how can i get the same view you showed me in the last post

          empid1 / name1 / lastname1 update link 1 ( with empid1 as variable)
          empid2 / name2 / lastname2 update link 1 ( with empid2 as variable)..
          ....


          i really appreciate your help bro..


          Thanks

          Comment


          • #6
            ah, three arrays. do they have names?

            Comment


            • #7
              Hi Xelawho,

              ok here is my real scenario :

              I have 4 arrays : PojectID, ProjectGate, ProjectGateName , ProjectgateDate

              i have already those arrays ready in Javascript and data loaded into them .
              what i want is :

              1 - i have a Dropdownlist with list of PojectID
              2- on PojectID item change, i should see all (ProjectGate, ProjectGateName and ProjectgateDate ) related to that PojectID
              3- for each record generated i want to have hyperlink where i can pass projectID / ProjectGate variables to a forms where i ll be able to update the information..

              Please, Let me know if i am unclear

              thanks alot for the help !!

              Comment


              • #8
                Something to consider ...

                Originally posted by xelawho View Post
                ah, three arrays. do they have names?
                Seems like a design that is courting disaster.
                Will be difficult to search and sorting will lead to total chaos.

                But, it is the OP's question.

                Just a guess:
                Code:
                <!DOCTYPE HTML>
                <html>
                <head>
                <title> Untitled </title>
                <script type="text/javascript">
                var A1 = ['1111','2222','3333','4444'];
                var A2 = ['John','Jacob','Jingle','Heimer'];
                var A3 = ['Jones','Astor','Bell','Smith'];
                var employee = []
                for (var i=0; i<A1.length; i++) {
                  str = A1[i]+': '+A3[i]+', '+A2[i];
                  employee.push(str);
                }
                for (var i=0; i<employee.length; i++) { alert(employee[i]); }
                
                </script>
                
                </head>
                <body>
                
                </body>
                </html>

                Comment


                • #9
                  jmrker: I am curious, not criticising - why not use concat here?

                  Code:
                  var A1 = ['1111','2222','3333','4444'];
                  var A2 = ['John','Jacob','Jingle','Heimer'];
                  var A3 = ['Jones','Astor','Bell','Smith'];
                  var employee = []
                  employee=employee.concat(A1,A2,A3);
                  alert(employee[6]);

                  Comment


                  • #10
                    Hi Jmrker,

                    Thanks for that ... i think my scenario is more complex i guess..

                    thanks for the suggestion

                    Comment


                    • #11
                      I think I get it now. Well, probably not, but let's pretend.

                      Now assuming that your arrays are all the same length and each item in a "column" matches all the items in its "row", maybe this will help:

                      Code:
                      <html>
                      	<head>
                      	</head>
                      	<body>
                      	
                      	<div><form name="myform">
                      	<select id="names" onChange="showRecord()"><option selected>Please choose a project</option></select>
                      	</form>
                      	</div>
                      	<div id="links"></div>
                      	<script type="text/javascript">
                      	var ProjectID = ['London 1242','Istandbul 7896','Paris 3858'];
                      	var ProjectGate = [{Place:'London'},{Place:'Istanbul'},{Place:'Paris'}];
                      	var ProjectgateDate = [{date:'June 1'},{date:'Dec 3'},{date:'Apr 20'}];
                      	for (var k=0; k<ProjectID.length; k++) {
                      var optn = document.createElement("OPTION");
                      optn.text = ProjectID[k];
                      document.myform.names.options.add(optn);
                      }
                      
                      function showRecord(){
                      var a=document.getElementById("names").selectedIndex-1;
                      if (a==-1){
                      document.getElementById("links").innerHTML =""
                      } else {
                      document.getElementById("links").innerHTML ="Project ID: "+ProjectID[a]+"<br>Project Gate: "+ProjectGate[a].Place+"<br>Project Date: "+ProjectgateDate[a].date
                      	}
                      }	
                      		</script>
                      		
                      </body>
                      </html>
                      Last edited by xelawho; Sep 11, 2011, 02:33 PM. Reason: fixed dummy option

                      Comment


                      • #12
                        Originally posted by xelawho View Post
                        jmrker: I am curious, not criticising - why not use concat here?

                        Code:
                        var A1 = ['1111','2222','3333','4444'];
                        var A2 = ['John','Jacob','Jingle','Heimer'];
                        var A3 = ['Jones','Astor','Bell','Smith'];
                        var employee = []
                        employee=employee.concat(A1,A2,A3);
                        alert(employee[6]);
                        No reason other than trying to keep the solution simple for the OP
                        since I'm not sure I really understand what they are trying to do!

                        Comment


                        • #13
                          Hi xelawh,

                          I appreciate your time..How i ll change that if i have more than 1 ProjectGate by ProjectID
                          var ProjectID = [1,2,3,1];
                          var ProjectGate = [gate1, gate2,gate1,gate3];
                          var ProjectgateDate = [{date:'June 1'},{date:'Dec 3'},{date:'Apr 20'}, {date:'May20'}];

                          let say i chose Project 1 from the Dropdown:
                          i need to get only :

                          ProjectID : 1
                          gate1 / June 1
                          gate3 / May20




                          thanks..

                          Comment


                          • #14
                            now I'm REALLY confused...

                            there can be two projects with the same Id?

                            or one project can have various gates?

                            the way that it works now is all about position within the array - so I guess we need to know if for example in bit that you showed, if you choose the first choice (1) will you always want to see the 4th choice as well?

                            or it it more a case of checking if there are other entries with the same ID and if there are, showing them and their respective data, too?

                            Comment


                            • #15
                              No a project keeps the same ID but it can have various gates?
                              gates : gate 1 to gate 3

                              example of records i may found in my table :


                              RecordId(unique) / projectid / gatename / gatedate
                              1/10/Gate1/01/01/2011
                              2/10/Gate2/03/01/2011
                              3/10/Gate3/04/15/2011
                              4/5/ Gate1/01/05/2011
                              5/5/ Gate2/03/10/2011
                              6/11/ Gate1/03/10/2011


                              as you van see project id = 10 has more than 1 gate ( gate1, 2 and 3)
                              project id = 5 has two gates and projectid=11 has only one gate .

                              in the dropdownlist , when i choose Distinct (projectid) i.e 10 it should give me only :
                              10
                              1/Gate1/01/01/2011
                              2/Gate2/03/01/2011
                              3/Gate3/04/15/2011


                              hope this is clear now...


                              Thanks !

                              Comment

                              Working...
                              X
                              😀
                              🥰
                              🤢
                              😎
                              😡
                              👍
                              👎