Adding classes based on JSON value

Adding classes based on JSON value

richcomptonWFrichcomptonWF Posts: 7Questions: 0Answers: 0
edited January 2013 in General
I am gearing up to use the JEditable plugin, but have a dilemma. Only certain columns will be editable and it will be determined server side. So I need a way to set the class of that have a value of editable to 'edit' so that those fields become editable. A sample of the JSON is:

[code]
{
"Timeline": [
{
"ID": "1",
"ProjectName": "Project A",
"FocusArea": "Area 01",
"Function": "Task 01",
"ProductGroup": "Group 01",
"Week1": {
"value": "10",
"editable": "false"
},
"Week2": {
"value": "20",
"editable": "false"
},
"Week3": {
"value": "20",
"editable": "false"
},
"Week4": {
"value": "15",
"editable": "true"
}
}
}
]
[/code]

My JS thus far looks like this:

[code]
$(document).ready(function () {
$('#table_id').dataTable({
'bFilter': false,
'bLengthChange': false,
'bProcessing': true,
'bJQueryUI': true,
'sPaginationType': 'full_numbers',
"sDom": 'ript',
'sAjaxSource': 'testData.json',
'sAjaxDataProp': 'Timeline',
'aoColumns': [
{'mData': 'ID', 'bVisible': false},
{'mData': 'Week1.editable'},
{'mData': 'Week2.editable', 'bVisible': false},
{'mData': 'Week3.editable', 'bVisible': false},
{'mData': 'Week4.editable'},
{'mData': 'ProjectName'},
{'mData': 'FocusArea'},
{'mData': 'Function'},
{'mData': 'ProductGroup'},
{'mData': 'Week1.value'},
{'mData': 'Week2.value'},
{'mData': 'Week3.value'},
{'mData': 'Week4.value'}
],
"fnFooterCallback": function ( nRow, aaData, iStart, iEnd, aiDisplay ) {

var calculateTotal = function(column) {
var total = 0;
for(var i = 0; i

Replies

  • rixxierixxie Posts: 1Questions: 0Answers: 0
    You can add DT_RowId and DT_RowClass in your json for each row.

    [code]
    {
    "Timeline": [
    {
    "DT_RowId" : "1",
    "DT_RowClass" : "highlight",
    "ID": "1",
    "ProjectName": "Project A",
    [/code]

    http://datatables.net/release-datatables/examples/server_side/ids.html
  • richcomptonWFrichcomptonWF Posts: 7Questions: 0Answers: 0
    This would add an id or class on the entire row though wouldn't it? I need to be able to class individual cells.
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    There is currently no way using the built in options in DataTables to add a class to an individual cell. You can add it to the whole row using DT_RowClass as rixxie says, or use sClass to apply it to the whole column, but individual cells would need a call back such as fnCreatedCell to have a class added based on data.

    Allan
  • richcomptonWFrichcomptonWF Posts: 7Questions: 0Answers: 0
    Would it be possible to add a value to sClass from the JSON. such as
    [code]
    {'mData': 'Week1.value', 'sClass': 'Week1.editable'},
    [/code]
    where Week1.editable = the JSON value.
    The whole column either would be editable or not so that would work but I haven't had any luck getting it to read the value vs just reading Week1.editable as a string.
This discussion has been closed.