Click here to Skip to main content
15,886,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am developing a mobile app, which will show 2000 to 3000 data on search page. I have to add Button in td, let me know if any one has answer to this. Also I would Like to know how to add style to td and tr in such code.


XML
function successResponse(response) {
                    var tablecontents = "";
                    tablecontents = "<table>";
                    var buttonnode= document.createElement('input');
                    for (var i = 0; i < response.getsearchresultdataResult.length; i++) {
                        tablecontents += "<tr>";
                        tablecontents += "<td>" + response.getsearchresultdataResult[i].service_provider_name +"</td>";
                        tablecontents += "</tr>";
                        tablecontents += "<tr>";
                        tablecontents += "<td>" + response.getsearchresultdataResult[i].address_text + ", "+
                         response.getsearchresultdataResult[i].city_name + ", "+response.getsearchresultdataResult[i].state_name +
                         ", "+response.getsearchresultdataResult[i].postal_code+"</td>";
                        tablecontents += "<td>" <input"</td>";
                        tablecontents += "</tr>";
                        tablecontents += "<tr>";
                        tablecontents += "<td>" + response.getsearchresultdataResult[i].phone_no +"</td>";
                        tablecontents += "</tr>";

                    }
                    tablecontents += "</table>";
                    document.getElementById("showresult").innerHTML = tablecontents;
                }



Below is my sample json data returned from web service

JavaScript
{
    "getsearchresultdataResult": [
        {
            "address_text": "indore54 vijaynagar",
            "city_sk": 19,
            "country_name": "India",
            "email_id": "dheerajgoojar31@gmail.com",
            "phone_no": "",
            "postal_code": "2342",
            "service_provider_name": "skin glow ",
            "service_provider_sk": 22759,
            "state_sk": 13
        },
        {
            "address_text": "lkjlksjd lkjlksdf ",
            "city_sk": 19,
            "country_name": "India",
            "email_id": "dheeraj_goojar31@yahoo.com",
            "phone_no": "",
            "postal_code": "4660021",
            "service_provider_name": "skinrise",
            "service_provider_sk": 22769,
            "state_sk": 13
        }
    ]
}
Posted
Updated 21-Jun-13 1:34am
v4
Comments
Sunasara Imdadhusen 20-Jun-13 9:05am    
Can you please provide your result over here?

I guess the script is failing on the first line of your function:
JavaScript
var body = document.getElementsById("tblbody")[0];


The correct function name is "getElementById" with no 's' - it returns the single element that matches the ID (and multiple elements should not share the same ID). The "[0]" after the function is not required because the function does not return an array.
 
Share this answer
 
XML
/*
USING YOUR EXAMPLE AS THE BASE
*/
function successResponse(response) {
    var gsrdR = response.getsearchresultdataResult;
    var tC = "";
    var buttonnode = document.createElement('input');

    /* For css styling */
    var odd = true;

    for (var i = 0; i < response.gsrdR.length; i++) {
        if (odd)
            tC += "<tr class='oddTr'>";
        else
            tC += "<tr class='evenTr'>";
        odd = !odd;
        tC += "<td>" + gsrdR[i].service_provider_name + "</td>";
        tC += "<td>" + gsrdR[i].address_text + ", " + gsrdR[i].city_name + ", " + gsrdR[i].state_name + ", " + gsrdR[i].postal_code + "</td>";
        /*
        Adding the button here.
         */
        tC += "<td><input type='button' value='My Button' /></td>";
        tC += "<td>" + gsrdR[i].phone_no + "</td>";
        tC += "</tr>";

    }
    tC = "<table>" + tC + "</table>";
    document.getElementById("showresult").innerHTML = tC;
}
/*
Now you can use seperate css for alternating rows.
Here is a very crude example.
*/
.oddTr {
    color: white;
    background: black;
}
.evenTr {
    color: white;
    background: black;
}
 
Share this answer
 
<!--
If your are willing to add another small library (named kLib) in the page,
then you can use this solution.
You can download the library from
https://googledrive.com/host/0BwqsmtVQ7ry6a3hsUlZHeS1pTVE/kLib-1.1.0-min.js
Check out the CodeProject article about kLib for more details.
-->

<script type="text/javascript">
	function successResponse(response) {
		var data = response.getsearchresultdataResult,
		node = document.getElementById("showresult"),
		odd = true,
		tr;
		var table = node.addChElem('table');
		for (var i = 0; i < data.length; i++) {
			tr = (odd) ? 'tr.oddTr' : 'tr.evenTr';
			odd = !odd;
			var t = table.addChElemTree([tr,['nm=td','adr=td','td',['ip=input'],'ph=td']]);
			t.nodes.nm.addChTxtNd(data[i].service_provider_name);
			t.nodes.adr.addChTxtNd(data[i].address_text + ", " + data[i].city_name + ", " + data[i].state_name + ", " + data[i].postal_code);
			t.nodes.ip.setAttr({
				type: 'button',
				value: 'My Button'
			}).onclick = function (){
				/*
				Write the code that you want to be executed when the button is pressed.
				*/
			}
			t.nodes.ph.addChTxtNd(data[i].phone_no);
		}
	}
</script>
<style type="text/css">
.oddTr {
	color: black;
	backround: white;
}
.evenTr {
	color: white;
	backround: black;
}
</style>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900