Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Is it possible to iterate through DataGrid Items with JavaScript ?

Posted on 2006-06-14
Medium Priority
Last Modified: 2008-02-07
how can this be accomplished ?
Question by:ZIVEM
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
  • 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 1000 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

Industry Leaders: 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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

730 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