?
Solved

how to generate array based on page wise

Posted on 2012-04-11
23
Medium Priority
?
499 Views
Last Modified: 2012-05-03
Please find the attached file;

//how to generate array values based on page wise;key is pagenumber and values are checked values.
//for example if i click page number one then values then it should come like below;
//what is the best approach to form array  like this ;should i go for JSON array or normal JS approach;

"1",{1,3,4,5}
"2",{6,7,8,9}
"3",{12,13,15}
0
Comment
Question by:chaitu chaitu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 4
  • +2
23 Comments
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 37836219
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37836455
We will make an associative array for this where in MyArray is key value pair:-

var MyArray = {
1 : ["1","3","4","5"],
2 : ["2","6","7"],
3 : ["8","9","11"],
getValue : function(key) {
console.log(MyArray[key]);
}
};

MyArray.getValue(2);

Open in new window

0
 
LVL 9

Expert Comment

by:Sar1973
ID: 37836950
If you need to declare/define arrays in a recursive way, I suggest to use "eval" function associated with cycles. For example, if "myArray-k" is one of the N the arrays you want to define and you also have a sort of rule to set its contents (i.e. 4 sequential integers each), you can do like this:
for (k = 1; k < N+1; k++) {
      eval("var myArray"+k+"=new Array();");
      for (j = 1; j < 5; j++) {
            eval("myArray["+k+"]="+(k*4-4+j)+";");
      }
}
You can also declare one only array[k][j] if the length of its rows is constant.
0
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
LVL 20

Author Comment

by:chaitu chaitu
ID: 37837508
BuggyCoder,

.when i check checkbox i want array in below format.

how to create array in the below format?

var MyArray = {
1 : ["1","3","4","5"],
2 : ["2","6","7"],
3 : ["8","9","11"],
};
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37838111
Declare this javascript object as global object in you js file:-
var MyArray = {
1 : ["1","3","4","5"],
2 : ["2","6","7"],
3 : ["8","9","11"],
getValue : function(key) {
return MyArray[key];
}
};

Open in new window

Next on the click of check box call its getValue with your key(1/2/3) method as:-

MyArray.getValue(1);

Open in new window


This will return an array from the type....
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 37841040
buggycoder,

you didnt understand my question.first of all i need to prepare like this when user checked checkbox.how to push the values in MyArray .do i need to use MyArray .push method.how will you insert key and value pairs.
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 1002 total points
ID: 37847691
Your question is not clear at all

What do you need the values for? Do they need to be cleared when the checkbox is unchecked?

Also if they really go from 1 to x, change them to go from 0 to (x-1) since javascript arrays start at 0. Also it seems you can use nested arrays instead of associative array which you should never use with numeric indicii.


So

var vals = [
[1,3,4,5],
[6,7,8,19].
[12,13,15]
];

var currentArray;
function checkboxPage(checkbox) {
  currentArray = (checkbox.checked)?vals[(checkbox.value-1)]:null;
}

.
.
.
<input type='checkbox' id='empid' value='1' onclick='checkboxPage(this)'>
<input type='checkbox' id='empid' value='2' onclick='checkboxPage(this)'>
<input type='checkbox' id='empid' value='3' onclick='checkboxPage(this)'>
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37847814
> how will you insert key and value pairs.
// assuming MyArray hash already exists:

MyArray["42"] = ["43","44"];

> how to push the values in MyArray
// as MyArray is a hash, you can "push" values as described above, but you cannot use JavaScripts Array.push(); however, you probably meant:

MyArray["42"].push("45");
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37848133
If you use a normal array, you can push an array.
MyArray.push([45,46])
0
 
LVL 9

Assisted Solution

by:Sar1973
Sar1973 earned 498 total points
ID: 37848822
If you have a 1-dimension array, such as MyMonoArray=["1", "2", "3"] the syntax is:
MyArray.push("4")
If the array you have declared is multidimensional, such as MyMultiArray=[["1", "2", "3"], ["5", "6", "7"]] you will have obviously to push 2 elements (one for each row) with 2 commands:
MyMultiArray[0].push("4");
MyMultiArray[1].push("8");
or with a single command (I guess):
MyMultiArray.push({"Row1Name":"4","Row2Name":"8"});

Take a look at this also:
http://javascript.info/tutorial/array
http://www.kirupa.com/forum/archive/index.php/t-212054.html
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37848890
Nope
MyMultiArray[0].push("4");
MyMultiArray[0].push("8");
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 37848958
If you push them in the same line, the array becomes:
["1", "2", "3", "4", "8"], ["5", "6", "7", , ]]
which is to say that you get 2 undefined values.
Try it yourself by customizing yor code and calling a generic fruits[x][y] element in this demo:
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_push
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37849113
W3fools.com
Enough said
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 37905056
sorry for the late reply.actually am doing checkbox pagination.for each page 5 records will be displayed.so i want to know on each page howmany values has been checked.

see the attached example.
how to construct JSON in this format;

[{"1":"1,2,3"},{"2":"6,7,8"}]
checkbox.html
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 37905091
if JSON structure is like this;

var checkboxvalues=
[
    {
        "pageno": 1,
        "values": "1,2,3"
       
    },
    {
        "pageno": 2,
        "values": "6,7,8"
    }
]


how to retreive page:2 related values??
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37905184
checkboxvalues[1].values;

javascript arrays are 0 based
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 37905639
Can you answer my previous Q?

how to construct JSON in this format;

[{"1":"1,2,3"},{"2":"6,7,8"}]
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37905662
If you expect gaps in the pages, use object array

Pages={
  page1:[1,0,0,1,1],
  page5:[1,0,0,0,1]
}

If not, use simple array of arrays perhaps there is no need for JSON
Pages=[[1,0,0,1,1],[1,0,0,0,1]]
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 37905745
The correct syntax in a single explicit declaration should be:
var checkboxvalues = [ { "pageno": "1", "values": ["value1", "value2", "value3"] }, { "pageno": "2", "values": ["value4", "value5", "value6"] } ]
If you want a recursive/iterational declaration with "eval" function, you'll need single quotes, pluses and other separators and indexes.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37905830
"eval"  is VERY SELDOM necessary and should be avoided as much as possible

This is syntactically correct too
var checkBoxArrayPerPage={
  page1:[1,0,0,1,1],
  page5:[1,0,0,0,1]
}
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 37910276
how to construct JSON array in this format .when i checkbox values in page1 then how to append values in page 1 and simulataneously if user uncheck then how can you remove the values from page1.


var checkboxvalues = [ { "pageno": "1", "values": ["value1", "value2", "value3"] }, { "pageno": "2", "values": ["value4", "value5", "value6"] } ]
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 1002 total points
ID: 37910647
Pageno is irrelevant here
checkboxvalues[0].values[3]=obj.checked
will update the 4th value on page 1 (the 0th entry)
0

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question