Solved

DataTable: Change the class for a column conditionally

Posted on 2016-09-13
5
74 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
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Title # Comments Views Activity
How to delete "dots" above Bootstrap 3 navbar 4 63
RegEx with optional part 4 42
Adding items to a C# list incrementally 5 34
JQuery Detecting page post back 6 12
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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