[solved] set styles on individual cells

[solved] set styles on individual cells

tehgreedotehgreedo Posts: 7Questions: 0Answers: 0
edited February 2011 in General
Let me start by saying that I love DataTables! It's made some of my projects VERY nice, especially with the server-side functionality.

What I have is a table being generated from a MySQL data source. Basically I'm displaying rows of website tickets that show multiple steps of completion (header images, building content, etc). I would like to apply a class (or some sort of styling) that sets the color of a cell based on data from the database, but I can't see a good way to pass anything like that along in the JSON string.

I've looked at some of the examples and other posts in the forum and have seen how to do an entire column or a cell based on specific text (making the "A" bold in one of the examples), but some of the text I'm outputting is dynamic so I can't use that method to set the style.

Anybody have any thoughts?

Replies

  • GerardoGerardo Posts: 66Questions: 0Answers: 0
    Check fnRender for individual cells, fnRowCallback for row-wide decisions.

    Hth,
    Gerardo
  • tehgreedotehgreedo Posts: 7Questions: 0Answers: 0
    It looks like fnRender will do styling on the cell level, but it lives in the initialization section of the code, yeah? Is there a way to pass fnRender info through the JSON when I pull the server-side data? I really don't want to do a lot of work on the javascript side if I don't have to... if I can do the work on the PHP end it would make my life easier.

    Thanks!
  • GerardoGerardo Posts: 66Questions: 0Answers: 0
    Right.

    You can also use sClass (in the js side of the code). Or use php to output the js code.

    If you really want to do it via json, you can add a styles array to the json output (alongside aaData), and reference if using json.styles.

    Hth,
    Gerardo
  • tehgreedotehgreedo Posts: 7Questions: 0Answers: 0
    I don't really need to do it via json, but since I'm doing data formatting on the PHP side, it's easier to set classes there while i'm dealing with the data anyway and the only way to get anything out of there with the data was through json.

    My other concern is that if I do it through the js side, I could be outputting a LOT of js to set backgrounds if somebody is displaying 100 rows. I have 6 or 7 columns that get styled and with 100 rows that's a lot of javascript to output. I can do it that way if it's my only option, I was just looking for something a little cleaner.

    Is there an example of using the json.styles that you could point me to?

    Thanks!
  • big-dealbig-deal Posts: 38Questions: 0Answers: 0
    If you want simple styling - you can send the data other:
    Insead of :
    "bla bla"
    just return:
    "bla bla"

    Hope it helps you.

    big-deal
  • tehgreedotehgreedo Posts: 7Questions: 0Answers: 0
    That's actually what I'm in the process of doing (but with div's instead of span's). The thing I was running into was that it doesn't fill the entire cell, only surrounds the text of the cell... so i gave the whatever class i wanted on the and used the following code:
    [code]$("div.approved").parent().addClass('approved');[/code]
    That gave the the class i wanted without having direct access to the table tags.

    Thanks for your help, everybody! I'm calling this fixed!
This discussion has been closed.