chaitu chaitu
asked on
how to generate array based on page wise
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}
//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}
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);
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.
for (k = 1; k < N+1; k++) {
eval("var myArray"+k+"=new Array();");
for (j = 1; j < 5; j++) {
eval("myArray["+k+"]="+(k*
}
}
You can also declare one only array[k][j] if the length of its rows is constant.
ASKER
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"],
};
.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"],
};
Declare this javascript object as global object in you js file:-
This will return an array from the type....
var MyArray = {
1 : ["1","3","4","5"],
2 : ["2","6","7"],
3 : ["8","9","11"],
getValue : function(key) {
return MyArray[key];
}
};
Next on the click of check box call its getValue with your key(1/2/3) method as:-MyArray.getValue(1);
This will return an array from the type....
ASKER
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.
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> 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");
// 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");
If you use a normal array, you can push an array.
MyArray.push([45,46])
MyArray.push([45,46])
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Nope
MyMultiArray[0].push("4");
MyMultiArray[0].push("8");
MyMultiArray[0].push("4");
MyMultiArray[0].push("8");
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
["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
W3fools.com
Enough said
Enough said
ASKER
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
see the attached example.
how to construct JSON in this format;
[{"1":"1,2,3"},{"2":"6,7,8
checkbox.html
ASKER
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??
var checkboxvalues=
[
{
"pageno": 1,
"values": "1,2,3"
},
{
"pageno": 2,
"values": "6,7,8"
}
]
how to retreive page:2 related values??
checkboxvalues[1].values;
javascript arrays are 0 based
javascript arrays are 0 based
ASKER
Can you answer my previous Q?
how to construct JSON in this format;
[{"1":"1,2,3"},{"2":"6,7,8 "}]
how to construct JSON in this format;
[{"1":"1,2,3"},{"2":"6,7,8
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]]
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,
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.
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.
"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]
}
This is syntactically correct too
var checkBoxArrayPerPage={
page1:[1,0,0,1,1],
page5:[1,0,0,0,1]
}
ASKER
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"] } ]
var checkboxvalues = [ { "pageno": "1", "values": ["value1", "value2", "value3"] }, { "pageno": "2", "values": ["value4", "value5", "value6"] } ]
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
CHECKBOXPAGING.html