?
Solved

JQuery Table Manipulation

Posted on 2010-01-09
6
Medium Priority
?
901 Views
Last Modified: 2012-06-27
Hi -

I create a table using jquery and a web service. The code is posted below.

I would like to add a checkbox column at the beginning of each row of the table and also several text boxes at the end. The ids can be something like checkbox1 - for row 1, checkbox2 - for row 2, etc....  

I am new to jquery, not sure how to do this

Any help would be appeciated.

Thanks
 
function buildTable(tableData) {
    var table = $("#varTest");
    // table.html("");  //clear out the table if it was previously populated
           
            eval("var data = " + tableData);  //load the data variable as an object array
            
            table.append('<thead><tr></tr></thead>');
            
            var thead = $('thead tr', table);  //create the table headers


            for (var propertyName in $(data)[0]) {
             
                thead.append('<th>' + propertyName + '</th>');
            }    //add the table rows

                  
            $(data).each(function(key, val) {
            table.append('<tr></tr>');
          
            var tr = $('tr:last', table);
               for (var propertyName in val) {
                tr.append('<td>' + val[propertyName] + '</td>');              
            }
         })
       ;}

Open in new window

0
Comment
Question by:doctor069
[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
  • 3
  • 2
6 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 26279867
.
function buildTable(tableData)
{
	var table = $("#varTest");
	// table.html("");  //clear out the table if it was previously populated
	eval("var data = " + tableData);  //load the data variable as an object array
	table.append('<thead><tr></tr></thead>');
	var thead = $('thead tr', table);  //create the table headers
	for (var propertyName in $(data)[0]){
		thead.append('<th>' + propertyName + '</th>');
	}    //add the table rows
	var counter=0;
	$(data).each(function(key, val)
	{	++counter;
		table.append('<tr></tr>');
		var tr = $('tr:last', table);
		tr.append('<td><input type="checkbox" name="chk'+counter+'" value="'+counter+'" id="chk'+counter+'" /></td>');
		for (var propertyName in val) {
			tr.append('<td>' + val[propertyName] + '</td>');              
		}
		tr.append('<td><input type="txt" name="text'+counter+'" value="'+counter+'" id="txt'+counter+'" /></td>');
	});
}

Open in new window

0
 

Author Comment

by:doctor069
ID: 26286469
Thanks - but that changes all the headers (they dont line up now)
0
 
LVL 82

Expert Comment

by:hielo
ID: 26286566
>>that changes all the headers (they dont line up now)
That makes no sense to me. I don't know what data you are passing. Provide a link to your page OR provide a fully function example of what you are using.
0
How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

 
LVL 82

Accepted Solution

by:
hielo earned 1400 total points
ID: 26287138
.
function buildTable(tableData) {
    var table = $("#varTest");
    // table.html("");  //clear out the table if it was previously populated
           
            eval("var data = " + tableData);  //load the data variable as an object array
            
            table.append('<thead><tr></tr></thead>');
            
            var thead = $('thead tr', table);  //create the table headers


            for (var propertyName in $(data)[0]) {
             
                thead.append('<th>' + propertyName + '</th>');
            }    //add the table rows

                  
            $(data).each(function(key, val) {
            table.append('<tr></tr>');
          
            var tr = $('tr:last', table);
               for (var propertyName in val) {
                tr.append('<td>' + val[propertyName] + '</td>');              
            }
         })
       ;}

Open in new window

0
 
LVL 2

Assisted Solution

by:HaloWebProjects
HaloWebProjects earned 600 total points
ID: 26289907
i just made sure that when adding a new cell to the table for checkboxes, we add an empty TH cell in the header as well, so that the table doesnt misalign
function buildTable(tableData)
{
	var table = $("#varTest");
	// table.html("");  //clear out the table if it was previously populated
	eval("var data = " + tableData);  //load the data variable as an object array
	table.append('<thead><tr></tr></thead>');
	var thead = $('thead tr', table);  //create the table headers
	thead.append('<th></th>');
	for (var propertyName in $(data)[0]){
		thead.append('<th>' + propertyName + '</th>');
	}    //add the table rows
	var counter=0;
	$(data).each(function(key, val)
	{	++counter;
		table.append('<tr></tr>');
		var tr = $('tr:last', table);
		tr.append('<td><input type="checkbox" name="chk'+counter+'" value="'+counter+'" id="chk'+counter+'" /></td>');
		for (var propertyName in val) {
			tr.append('<td>' + val[propertyName] + '</td>');              
		}
		tr.append('<td><input type="txt" name="text'+counter+'" value="'+counter+'" id="txt'+counter+'" /></td>');
	});
}

Open in new window

0
 

Author Closing Comment

by:doctor069
ID: 31675071
Worked perferct! Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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

765 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