Solved

Is it possible to iterate through DataGrid Items with JavaScript ?

Posted on 2006-06-14
4
348 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
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.
0
 

Author Comment

by:ZIVEM
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>
            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
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)
                  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
ID: 16902268
that's great!
thanks
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help Required 2 39
some keys in my laptop is not working. any suggestion 6 41
jboss 7.1 start up error 1 42
incorrect syntax near the order by 10 43
This article will show, step by step, how to integrate R code into a R Sweave document
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.

821 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