0

I am using Ignited datatables (for codeigniter) and I'm using the server side. I would like to show some values in RED or GREEN depending on their values.

This is how I call the function inside the edit_column:

->edit_column('releve_statut', '$1', $this->label_this('releve_statut'))

And this is the function I'm calling:

public function label_this($str) {

    if($str == "Réalisé"){
        $r =  label_badge('success', 'Réalisé'); 
    } else if ($str == "En cours"){
       $r = label_badge('warning', 'En cours'); 
    }

    return $r;

}

Any suggestion?

Thanks :)

3
  • Could I ask what does the label_badge function does, I'm tempted to put in a CSS solution but I don't really understand what markup that label_badge function creates, and where is it used Commented Feb 25, 2014 at 20:03
  • It's for formating the text. It creates a colored badge. function label_badge($type, $text){ return '<span class="badge bg-'.$type.'">'.$text.'</span>'; } The problem is that the comparison inside my function label_this doesn't work :/ Commented Feb 25, 2014 at 20:09
  • When I add a third else, it shows the value inside it: else { $r = 'Error'; } Commented Feb 25, 2014 at 20:12

3 Answers 3

1

If you want to use a callback on server side, an easy and painless solution is creating a Codeigniter Helper class.

So, in your code, change the third parameter of edit_column:

->edit_column('releve_statut', '$1', 'label_this(releve_statut)')

Finally, define the function label_this($str) in a helper class. Don't forget to load it before calling $this->datatables->generate().

Sign up to request clarification or add additional context in comments.

Comments

0
->edit_column('releve_statut', '$1', 'callback_label_this(releve_statut)');

Source

Not an expert on this but I know this is how its done elsewhere in codeigniter so although I can't test, makes sense that this should work

Original (still sort of relevant) What you need to do here is have a CSS style sheet as so

.bg-success{
    color: green;
}
.bg-warning{
    color: red;
}

this will style the text as coloured If you want a coloured background then replace color with background color.

3 Comments

The problem is that the comparison doesn't work. When I do this: if($str == "Réalisé"){ $r = 'AAA'; } else if ($str == "En cours"){ $r = 'BBB'; } else { $r = 'Error'; } The result is: 'Errror'! PS: The values in the database are: enum('','En cours','Réalisé').
I have edited it now so I think this is what it should be like
Thanks for the answer but it's still not working :/ when i put your code it shows this value inside my table: callback_label_this(releve_statut)
0

The callback function on the server side didn't work for me :/

The solution that I did is to add fnDrawCallback to the datatables initialisation (client side) which is is called on every 'draw' event.

"fnDrawCallback": function( oSettings ) {
                        var $green = $('.statut-style').filter(function() {
                                    return  ($(this).text() == 'Réalisé');
                                });
                        // Set the green elements to color green
                        $green.addClass("badge bg-success");
      },

If anyone know how to do it on the server side, I'll be glad if you share it ;)

Thanks!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.