Solved

DataTable: Change the class for a column conditionally

Posted on 2016-09-13
5
81 Views
Last Modified: 2016-09-19
I have a Datatable with the following column:

{ title: "Status", data: "StatusName", class: "centered-text" }


I need to change the font color conditionally.  I created classes for each condition.  How could I change the class based on the data?  

Thank You,
Crystal
0
Comment
Question by:Crystal Rouse
[X]
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
  • 3
  • 2
5 Comments
 
LVL 43

Accepted Solution

by:
zephyr_hex (Megan) earned 500 total points
ID: 41796317
By DataTable, I assume you mean in C# ?

One way to do this would be to select the rows in the DataTable that meet you condition, and modify the class.

//find the rows that meet your condition
DataRow[] rows = myDataTable.Select("title Like 'A%'");

//loop over those rows and add a new class
//note:  myDataTable is updated by this as well
for(int i = 0; i < rows.Length; i ++)
{
      rows[i]["class"] = rows[i]["class"] + " myNewClass";
}

Open in new window

0
 

Author Comment

by:Crystal Rouse
ID: 41796343
I'm using JavaScript and C#.  Trying to create a function to change the class based on the data.
0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41796376
ok, the code I gave you above is C#

If you want to change the class in JavaScript, then you actually would be modifying an HTML table (client side structure) and not a DataTable (server side stucture) because JavaScript is a client side language.
0
 

Author Comment

by:Crystal Rouse
ID: 41797899
I need to write a JavaScript function that I can call when the page loads to change the font of a column based on the data.  I can figure out most of this, just don't know how to find the column. I can get the entire table's class to change, just not the individual column.
0
 
LVL 43

Assisted Solution

by:zephyr_hex (Megan)
zephyr_hex (Megan) earned 500 total points
ID: 41798131
Here is a Fiddle Demo.

Given the following HTML:

<table id="myTable">
  <thead>
    <tr>
      <th>Col 1</th>
      <th>Col 2</th>
      <th>Col 3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>a</td>
      <td>b</td>
      <td>c</td>
    </tr>
    <tr>
      <td>d</td>
      <td>e</td>
      <td>f</td>
    </tr>
    <tr>
      <td>g</td>
      <td>h</td>
      <td>i</td>
    </tr>
  </tbody>
</table>
<input type="text" id="myTxt" placeholder="input column number">
<input type="button" id="myBtn" value="Do It">
<span id="mySpan"></span>

Open in new window


CSS:
.red {
  background-color: red;
}

Open in new window


You can access the column values and modify the CSS from jQuery:
$('#myBtn').click(function() {
  $("#mySpan").text("");
  $("#myDiv").remove();
  var colNum = $("#myTxt").val();
  if ($("#myTxt") === "") return;

  var col = $('#myTable tr > td:nth-child(' + colNum + '), table tr > th:nth-child(' + colNum + ')');

  col.addClass("red");
  $("#mySpan").before("<div id='myDiv'>Values from column:</div>");
  var span = "";
  col.each(function() {
    span = span + $(this).text() + " ";

  });
  $("#mySpan").text(span);
});

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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