Display different value than actual data source value

Display different value than actual data source value

enjoypbenjoypb Posts: 26Questions: 9Answers: 0
edited March 2017 in Free community support

Is there a way to display a different value than the source data in my cell?

My data source is ajax:json and I'm displaying the tables great. I just have one column with about 6 different values that I need to display as more readable values.

For instance I want to display:

'Dog' whenever the value of a cell is 'puppy', or
'Cat' if the value is 'kitten'.

Thanks!

This question has accepted answers - jump to:

Answers

  • tangerinetangerine Posts: 2,294Questions: 18Answers: 254
  • enjoypbenjoypb Posts: 26Questions: 9Answers: 0

    Thanks tangerine. I've spent another couple hours on this and am butting my head on the wall. Here's what I've been trying (along with 100 different variations). I keep getting the datatables.net/tn/4 message but, can't find what I'm doing wrong. There may be null values (empty values) so I'm not sure if that's affecting this.

    Here's my latest iteration:

            { data: 'class'
                            render: function ( data, type, row, meta ) {
                              data: 'onsale_c', render:'On-Sale Common',
                              data: 'onsale', render:'On-Sale Other',
                              data: 'offsale_c', render:'Off-Sale Common',
                              data: 'offsale_c', render:'Off-Sale Common',
                              data: 'nonretail_c', render:'Non-Retail Common',
                              data: 'nonretail', render:'Non-Retail Other'
                              // data: '' , render: 'Unknown'
                             },
    
            },
    

    Do you see where I'm going wrong? Thanks again.

  • kthorngrenkthorngren Posts: 5,491Questions: 19Answers: 1,226
    Answer ✓

    You need to return a value to display. For example:

    { data: 'class'
                    render: function ( data, type, row, meta ) {
                        if (data == 'onsale_c') {
                               return 'On-Sale Common'
                        } else if (data == 'onsale') {
                                return 'On-Sale Other'
                      ........
    

    Hope the syntax is right but should give you the idea.

    Kevin

  • enjoypbenjoypb Posts: 26Questions: 9Answers: 0

    Awesome thank you kthorngren. I was successful with the following. I am getting a chrome error popup for a specific datatables row but, have checked my data and cannot find the flaw (every record has one of the six labels). I get that error with or without the last row active. Just off hand do you know if it's possible to go to a specific row of data that datatables is reflecting in that error?

    Thanks again. Here's what worked:

            { data: 'class',
                render: function ( data, type, row, meta ) {
                    if      (data == 'onsale_c') { return 'On-Sale Common'
                  } else if (data == 'onsale') { return 'On-Sale Other'
                  } else if (data == 'offsale_c') { return 'Off-Sale Common'
                  } else if (data == 'onsale') { return 'Off-Sale Other'
                  } else if (data == 'nonretail_c') { return 'Non-Retail Common'
                  } else if (data == 'nonretail') { return 'Non-Retail Other'
                  }
                  // else if (data == '') { return '' }
                },
            },
    
  • tangerinetangerine Posts: 2,294Questions: 18Answers: 254

    I am getting a chrome error popup

    What does it say?

    Also

    } else if (data == 'onsale') { return 'Off-Sale Other'
    

    you probably want to fix that line.

  • enjoypbenjoypb Posts: 26Questions: 9Answers: 0

    Hey thanks for the heads up on the typo Tangerine.

    I found the error is referring to empty fields. I tried the else/if with null or empty but, still get the error. Am I doing that correctly (if the above example were uncommented)?

    Thanks!

  • kthorngrenkthorngren Posts: 5,491Questions: 19Answers: 1,226
    Answer ✓

    I would try making it just a return ''. That way it will return something if none of the if's match.

    Kevin

  • enjoypbenjoypb Posts: 26Questions: 9Answers: 0

    Ok cool. Thank you both very much.

  • Imam TriznaImam Trizna Posts: 5Questions: 1Answers: 0
    edited February 12

    whats about show diferent data from database, not diferent value

    like

    if (data == 'onsale_c') { return { "data": "Name" },}

    can anyone help me?

  • colincolin Posts: 4,606Questions: 0Answers: 809

    @Imam Trizna - I replied to this on your thread here.

Sign In or Register to comment.