how to wrap a button in an IF statement

how to wrap a button in an IF statement

samwsamw Posts: 15Questions: 7Answers: 0

I have a datable where the last column contains two buttons (summary and edit) but I only want the buttons to be visible if the flags in each of the columns 2 and 3 are set to true? so if column 2=true then show summary button, if column 3 =true then show edit button.

Is there a way to add an IF statement?

here's my code so far:

"columnDefs": [
{
"targets": [6],
"data": "EmployeeID",
"render": function (data, type, row) {
return '@Html.ActionLink("Summary", "EmployeeSummary", "Employee", new {EmployeeID = "replace" },null)'.replace("replace", data) + " | " +
'@Html.ActionLink("Edit", "EditEmployee", "Employee", new {EmployeeID= "replace" },null)'.replace("replace", data)); }
}
]

thanks
sam

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,145Questions: 26Answers: 4,736

    In the render function you can do anything that is legal in Javascript. Here is an example of using an if statement to determine whether to display the Age button:
    http://live.datatables.net/wejuhuwe/1/edit

    Kevin

  • samwsamw Posts: 15Questions: 7Answers: 0

    thank you for the example but it doesn't seem to work. when I use
    if(row[2] ==true )
    {
    '@Html.ActionLink("Edit", "EditEmployee", "Employee", new {EmployeeID= "replace" },null)'.replace("replace", data)); }
    }

    I've also tried comparing against a numeric field and it still didn't work
    if(row[6] >0)
    {
    '@Html.ActionLink("Edit", "EditEmployee", "Employee", new {EmployeeID= "replace" },null)'.replace("replace", data)); }
    }

    also, in your example what are:
    id=s-
    id=n-

    thanks

  • kthorngrenkthorngren Posts: 20,145Questions: 26Answers: 4,736
    edited September 2019 Answer ✓

    also, in your example what are:
    id=s-
    id=n-

    For my example these are the HTML ids for each button. The columns.render meta parameter contains the row index which I use to create unique ID's for each button. You may or may not need that for your solution.

    return '<input type="button" class="name" id=n-"' + meta.row + '" value="Name"/>
    <input type="button" class="age" id=s-"' + meta.row + '" value="Age"/>';
    

    It looks like you are using object based data: "data": "EmployeeID",. But in your render function you are trying to access the row data using array notation: row[2]. You will need to use object notation, something like row.EmployeeID.

    Kevin

This discussion has been closed.