Turn checkbox into Yes or No & prevent text from disappearing
Turn checkbox into Yes or No & prevent text from disappearing
Hi, I used the solution at this post (https://datatables.net/forums/discussion/comment/121148/#Comment_121148) to update my dataTable to allow my checkboxes to show a Yes or No value when exporting to Excel. However, this solution also removes the text I have showing in those fields, I am showing an expiration date if the checkbox comes back true.
Can someone help me figure out how to keep the yes/no values, and also the text?
Here is my dataTable including Razor syntax:
<td role="gridcell">
@Html.CheckBoxFor(modelItem => item.NotaryPublic, new { @id = "Notary Public-" + item.PID, @title = "Notary Public", @disabled = "disabled" })
@if (item.NotaryPublic == true)
{
var npExpDate = string.Concat(string.Format(item.NotaryPublicExpirationDate.AsDateTime().ToString("MM-dd-yy")));
var text = String.Concat(" (Exp Date: " + npExpDate + ")");
@Html.DisplayFor(modelItem => text)
}
</td>
Or, if you prefer, here is the HTML:
<td role="gridcell">
<input checked="checked" id="CPR Trained-929" name="item.CPRTrained" title="CPR Trained" type="checkbox" value="true" /><input name="item.CPRTrained" type="hidden" value="false" />
(Exp Date: 05-20-21)
</td>
columnDefs: [
{ orderable: false, targets: 'no-sort' },
{
render: function (data, type, row) {
var i = (type === 'export' ? ($(data).prop("checked") === true ? 'Yes' : 'No') : data);
return i;
},
targets: [12, 13, 14, 15, 16]
}
]
Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide. (note, back ticks should be on their own lines)
This question has an accepted answers - jump to answer
Answers
I'm guess the checkbox is shown in a field with a date as well. So in the
columns.render
, where you check for$(data).prop("checked") === true
, you'll also need to append that date fromdata
too.If that doesn't help, we're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. 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
Hi Colin, I created a test case here: http://live.datatables.net/rexuwegi/1/edit?html,js,output
I hope that helps show what is happening. When you click on Export to Excel, you will see the data in Notary Public, CPR Trained, and AED Trained as Yes, but it does not include the expiration dates. Thanks for the help. I will keep looking into my jquery as well.
You just need to add the
data
to the "Yes" or "No", for example:var i = (type === 'export' ? ($(data).prop("checked") === true ? 'Yes' + data : 'No' + data) : data);
Here is the updated example:
http://live.datatables.net/rexuwegi/3/edit
Kevin
Thanks Kevin! I knew I had to add something but didn't realize it was as easy as adding a plus sign to the jquery. Coming from a .NET background, I was thinking I needed to add string.concat to the beginning or something similar. This is perfect. Thanks again!