optimization of a column render

optimization of a column render

MelodyNelsonMelodyNelson Posts: 213Questions: 33Answers: 2

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

Hi,

I have a column with a rendering as an image and a numerical sort to show Priorities like that :

The data is is french, the translation is :
- Néant = none, 0
- Basse = low, 1
- Moyenne = average, 2
- Haute = high, 3
- Urgent, 4

I've created this function for the rendering (display and sort), it works great but I'm wondering if I could have do it better (it seems long for a simple function).

Thanks for your advices.

function render_priorite_rapports( data, type, row ) {  
    
    if (type === 'display' || type === 'filter') {
        switch(data) {
            case "Néant":
                var image = "<img src='maquette/images/icones/priorite0.png' />";
                break;
            case "Basse":
                var image = "<img src='maquette/images/icones/priorite1.png' />";
                break;
            case "Moyenne":
                var image = "<img src='maquette/images/icones/priorite2.png' />";
                break;
            case "Haute":
                var image = "<img src='maquette/images/icones/priorite3.png' />";
                break;
            case "Urgent":
                var image = "<img src='maquette/images/icones/priorite4.png' />";
                break;
            default:
                var image = '';
        }
            return image;
    }
        
    if ( type === 'sort' ) {
        
        switch(data) {
            case "Néant":
                var sort = 0;
                break;
            case "Basse":
                var sort = 1;
                break;
            case "Moyenne":
                var sort = 2;
                break;
            case "Haute":
                var sort = 3;
                break;
            case "Urgent":
                var sort = 4;
                break;
            default:
                var sort = '';
        }
            return sort;
                
    }
    return data;
}

Replies

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    It's not bad, but I'd be tempted to join the two switch statements, something like:

    function render_priorite_rapports( data, type, row ) {  
      var image;
      var sort;
    
      switch(data) {
        case "Néant":
          image = "<img src='maquette/images/icones/priorite0.png' />";
          sort = 0;
          break;
        case "Basse":
          image = "<img src='maquette/images/icones/priorite1.png' />";
          sort = 1;
          break;
        ....
      }
    
      if (type === 'display') {
        return image;
      }
    
      if (type === 'sort') {
        return sort;
      }
    
      return data;
    }
    

    Colin

  • MelodyNelsonMelodyNelson Posts: 213Questions: 33Answers: 2

    Thank you Colin, it's so obvious that I didn't think of it :smiley:
    I will try that ASAP and also separate the filter.

  • MelodyNelsonMelodyNelson Posts: 213Questions: 33Answers: 2

    It works great !

    function render_priorite_rapports( data, type, row ) { 
        var image;
        var sort;
        var text = data;
        
        switch(data) {
            case "Néant":
                image = "<img src='maquette/images/icones/priorite0.png' />";
                sort = 0;
                break;
            case "Basse":
                image = "<img src='maquette/images/icones/priorite1.png' />";
                sort = 1;
                break;
            case "Moyenne":
                image = "<img src='maquette/images/icones/priorite2.png' />";
                sort = 2;
                break;
            case "Haute":
                image = "<img src='maquette/images/icones/priorite3.png' />";
                sort = 3;
                break;
            case "Urgent":
                image = "<img src='maquette/images/icones/priorite4.png' />";
                sort = 4;
                break;
            default:
                image = '';
                sort = '';
        } 
            if (type === 'display') {
                return image;
            }   
            if (type === 'sort') {
                return sort;
            }   
            if (type === 'filter') {
                return text;
            }
            
        return data;
    }
    
Sign In or Register to comment.