If-Else condition for specific naming in Columns using DataTables javascript

If-Else condition for specific naming in Columns using DataTables javascript

HermosaHermosa Posts: 3Questions: 1Answers: 0

Dear forum member,

i try to spefic naming on my own datatables using javascript MVC.

I am surfing net and read many the forum and
the guide on this site (https://datatables.net/forums/discussion/45381/if-and-else-condition-with-data-tables) to build my own datatable .

I'm hang for few days to solve "If and else conditions" issue .

could someone help me?

here my coding parts on below:

var dataTable;  
$(document).ready(function () {
            dataTable =  $("#fruitTable").DataTable({

                "ajax": {
                    "url": "@Url.Action("GetFruitsName", "fruits")",
                    "type": "GET",
                    "datatype": "json",
                    "contentType":"application/json;charset=utf-8"
                },  

"columns": [
{ "data": "FRUITS_ID", "autowidth": true },
 {
                         "data": "FRUITS_NAME",   "autowidth": true,
                         "render": function (data, type, row) {
                             if (type==='display'){
                             if (data === "A") { return "Apple"; }
                             else if (data === "B") { return "Banana"; }
                             else if (data === "C") { return "Cherries"; }
                             else if (data === "D") { return "Dragon fruit"; }
                             else if (data === "E") { return "Eggfruit"; }
                             else return "no fruits name";
                         }
                     }
 }

]

what I want is:
when the data:"FRUITS_ID" has on value: "A" is represent text to "Apple".
"FRUITS_ID" has on value: "B" is represent text to "Banana".
"FRUITS_ID" has on value: "C" is represent text to "Cherries".
"FRUITS_ID" has on value: "D" is represent text to "Dragon fruit".
"FRUITS_ID" has on value: "E" is represent text to "Eggfruit".
otherwise "no fruits name"

problem:
all FRUITS_ID is always displayed "no fruits name" Even i change "B" of FRUITS_ID
could you please help change my coding.

thanks in advance for your help.

Replies

  • colincolin Posts: 15,118Questions: 1Answers: 2,583

    One thing that is missing is a return value if the type isn't display. Otherwise it looks fine, so we'll need to see it. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • HermosaHermosa Posts: 3Questions: 1Answers: 0

    Dear Colin,

    i'm getting a result. Thank you.

This discussion has been closed.