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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adobe Customization Wizard XI issues 26 206
array11 challenge 16 59
How  do I get an older program to run in Windows 10? 20 102
move one pixel 4 58
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

930 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

12 Experts available now in Live!

Get 1:1 Help Now