Is it possible to iterate through DataGrid Items with JavaScript ?

Posted on 2006-06-14
Last Modified: 2008-02-07
how can this be accomplished ?
Question by:ZIVEM
  • 2
  • 2
LVL 15

Expert Comment

ID: 16901504
Whenever any tabular data are displayed on a page, it is possible to iterate through them using JS. However, the exeact implementation depends on where the component came from. Which DataGrid are you using? Something MS created? From JSF? You see the source of the component determines the exact markup that's displayed. If you could paste the generated source of the datagrid, it would help identify what to access.

Author Comment

ID: 16901537
i get table with TR and TD's like this one.
my Grid has first and last names.

       <table cellspacing="0" cellpadding="3" rules="all" border="1" id="MyDataGrid" style="border-color:Tan;font-family:Arial;font-size:9pt;width:800px;border-collapse:collapse;">
      <tr style="color:White;background-color:#336699;font-weight:bold;">
            <td align="center">Edit</td><td align="center">Delete</td><td><a href="javascript:__doPostBack('MyDataGrid$ctl02$ctl00','')" style="color:White;">id</a></td><td><a href="javascript:__doPostBack('MyDataGrid$ctl02$ctl01','')" style="color:White;">First Name</a></td><td><a href="javascript:__doPostBack('MyDataGrid$ctl02$ctl02','')" style="color:White;">Last Name</a></td><td><a href="javascript:__doPostBack('MyDataGrid$ctl02$ctl03','')" style="color:White;">permission</a></td>
      </tr><tr style="background-color:#EEEEEE;">
            <td align="center" style="width:70px;"><a href="javascript:__doPostBack('MyDataGrid$ctl03$ctl00','')"><img src='gui/icon/application_form_edit.gif' border=0 /></a></td><td align="center" style="width:40px;"><a href="javascript:__doPostBack('MyDataGrid$ctl03$ctl01','')"><img src='gui/icon/application_form_delete.gif' border=0 /></a></td><td valign="top" style="white-space:nowrap;">71</td><td>
      </tr><tr style="background-color:#EEEEEE;">
            <td align="center" style="width:70px;"><a href="javascript:__doPostBack('MyDataGrid$ctl04$ctl00','')"><img src='gui/icon/application_form_edit.gif' border=0 /></a></td><td align="center" style="width:40px;"><a href="javascript:__doPostBack('MyDataGrid$ctl04$ctl01','')"><img src='gui/icon/application_form_delete.gif' border=0 /></a></td><td valign="top" style="white-space:nowrap;">60</td><td>
      </tr><tr style="background-color:#EEEEEE;">
            <td align="center" style="width:70px;"><a href="javascript:__doPostBack('MyDataGrid$ctl05$ctl00','')"><img src='gui/icon/application_form_edit.gif' border=0 /></a></td><td align="center" style="width:40px;"><a href="javascript:__doPostBack('MyDataGrid$ctl05$ctl01','')"><img src='gui/icon/application_form_delete.gif' border=0 /></a></td><td valign="top" style="white-space:nowrap;">70</td><td>
      </tr><tr style="background-color:#EEEEEE;">
            <td align="center" style="width:70px;"><a href="javascript:__doPostBack('MyDataGrid$ctl06$ctl00','')"><img src='gui/icon/application_form_edit.gif' border=0 /></a></td><td align="center" style="width:40px;"><a href="javascript:__doPostBack('MyDataGrid$ctl06$ctl01','')"><img src='gui/icon/application_form_delete.gif' border=0 /></a></td><td valign="top" style="white-space:nowrap;">54</td><td>
      </tr><tr align="right" style="background-color:#C6C3C6;font-size:Smaller;">
            <td colspan="6"><span>1</span>&nbsp;<a href="javascript:__doPostBack('MyDataGrid$ctl08$ctl01','')">2</a></td>
LVL 15

Accepted Solution

bpmurray earned 250 total points
ID: 16902238
Try this. It takes the following params: ID of the datagrid, the number of rows in the header, the number of rows in the footer, the column numbers of the cells you're interested in.

For the grid above , use it as iterate("MyDataGrid", 1, 1, 4, 3) to pick up the lastnam & firstname from all rows except the first and last. It simply prints the data, but I guess you have bigger plans for the data :-)

      function iterate(id, hdrSize, ftrSize) {
            // We have to have the right niumber of args
            if (arguments.length <= 3)

            // Get the datagrid object in the DOM
            var grid;
            if (window.document.getElementById != null) {
                  grid = window.document.getElementById(id);
            } else if (window.document.all != null) {
                  grid = window.document.all(id);

            // Identify the interesting part of the table
            var firstRow = 0;
            if (hdrSize && hdrSize > 0)
                  firstRow = hdrSize;

            var lastRow = grid.rows.length - 1;
            if (ftrSize && ftrSize > 0)
                  lastRow -= ftrSize;
            if (lastRow < 0 || lastRow > grid.rows.length)

            // this is to xstore the strings ...
            var returnString = "";

            // Process rows
            var iX, iY;
            var row;
            for (var iX=firstRow; iX<lastRow; iX++) {
                  row = grid.rows[iX];
                  // Process cells
                  for (iY=3; iY<arguments.length; iY++) {
                        var cellX = arguments[iY];
                     var cell = row.cells[cellX];
                        returnString += cell.textContent;
                  returnString += "\n";

Author Comment

ID: 16902268
that's great!

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is about my first experience with programming Arduino.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now