admin管理员组文章数量:1415111
I have dataArr = ["1","Maths","2","Science"];
I need to display the values 1,2(S.No) in the first column and the (Subject name) Maths,Science in the second column of a dynamic table.
Any way to do this using jquery/javascript?
Sample table code:
$("#subjectTable").append("<table style='width:100%; height: 4em; border-spacing: 0px;'><tr><td style='width:25%'>"+OddpositionVal+"</td><td style='width:25%'>"+EvenpositionVal+"</td></tr></table>");
I have dataArr = ["1","Maths","2","Science"];
I need to display the values 1,2(S.No) in the first column and the (Subject name) Maths,Science in the second column of a dynamic table.
Any way to do this using jquery/javascript?
Sample table code:
$("#subjectTable").append("<table style='width:100%; height: 4em; border-spacing: 0px;'><tr><td style='width:25%'>"+OddpositionVal+"</td><td style='width:25%'>"+EvenpositionVal+"</td></tr></table>");
Share
Improve this question
edited Oct 20, 2016 at 11:00
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Sep 10, 2014 at 9:29
ShaliniShalini
2191 gold badge5 silver badges16 bronze badges
1
- Adding a note : How to display the values in the table odd value in the first col,even value in the second col respectively . – Shalini Commented Sep 10, 2014 at 9:42
5 Answers
Reset to default 2var dataArr = ["1","Maths","2","Science"];
Add a bare bones table to a div
.
$("#subjectTable").append('<table id="table"></table>');
For each table row loop over the array in steps of 2 (i+=2
). oddPositionVal
is the first element in the step, evenPositionVal
is the second element.
Build the row HTML and then append it to the table.
for (var i = 0, l = dataArr.length; i < l; i+=2) {
var oddPositionVal = dataArr[i];
var evenPositionVal = dataArr[i + 1];
var rowhtml = '<tr><td style="width:25%">' + oddPositionVal + '</td><td style="width:25%">' + evenPositionVal + '</td></tr>';
$('#table').append(rowhtml);
}
DEMO
var dataArr = ["1","Maths","2","Science"];
for (var i=0;i<dataArr.length;i++){
if(i%2==0){
//even index
console.log("sno="+dataArr[i]);
}
else{
//odd index
console.log("subject="+dataArr[i]);
}
}
UPDATE:-
DEMO
Try this :-
$.each(dataArr,function(index,value){
if(parseInt(index) % 2 == 0)
{
alert(value);
}
else
{
alert(value);
}
});
var dataArr = ["1","Maths","2","Science"];
var $table = $("<table style='width:100%; height: 4em; border-spacing: 0px;'>");
var $row = $("<tr>");
$.each(dataArr, function (index, value) {
$row.append("<td style='width:25%'>" + value + "</td>");
if (index % 2) {
$table.append($row);
$row = $("<tr>");
}
});
$table.appendTo("#subjectTable");
Here is what you want:
var dataArr = ["1","Maths","2","Science"],
$table = $("<table style='width:100%; height: 4em; border-spacing: 0px;'></table>").appendTo("#subjectTable");
var $tr = null;
$.each(dataArr, function(index) {
if(index % 2 == 0) {
$tr = $("<tr>").appendTo($table);
}
$tr.append("<td style='width:25%'>" + this + "</td>");
});
And here is the working example
本文标签: jqueryHow to get oddeven value from array and display in table using JavascriptStack Overflow
版权声明:本文标题:jquery - How to get oddeven value from array and display in table using Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745208204a2647739.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论