Solved

Is it possible to iterate through DataGrid Items with JavaScript ?

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

Expert Comment

by:bpmurray
Comment Utility
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.
0
 

Author Comment

by:ZIVEM
Comment Utility
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>
            john
          </td><td>
            weis
          </td><td>
            1
          </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>
            Dan
          </td><td>
            Kerr
          </td><td>
            1
          </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>
            Cristian
          </td><td>
            Luxford
          </td><td>
            2
          </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>
            Ben
          </td><td>
            Turner
          </td><td>
            3
          </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>
      </tr>
</table>
0
 
LVL 15

Accepted Solution

by:
bpmurray earned 250 total points
Comment Utility
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)
                  return;

            // 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)
                  return;

            // 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";
            }
            alert(returnString);
      }
0
 

Author Comment

by:ZIVEM
Comment Utility
that's great!
thanks
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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.
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 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

8 Experts available now in Live!

Get 1:1 Help Now