Display a string in place of true/false

I have a field that is true/false. I want to Display Passed/Failed instead. I also want the field to show Passed/Failed on pdf and excel export as well as in the filters.
So Status should Display Passed instead of true and Failed instead of false.

Also, the filter should display Passed/Failed instead of true false.



When the data is exported to excel or pdf it should also display the boolean value as Passed/Failed instead of true/false.


6 Replies 1 reply marked as answer

RS Rajapandiyan Settu Syncfusion Team March 16, 2021 12:45 PM UTC

Hi Danyelle, 
 
Thanks for contacting Syncfusion support. 
 
Query: I have a field that is true/false. I want to Display Passed/Failed instead. I also want the field to show Passed/Failed on pdf and excel export 
 
We have validated your query at our end. You can achieve your requirement by using valueAccessor property of EJ2 Grid’s Column. 
 
By using valueAccessor, you can customize the data as you want in the Grid column. But, this is used only for the display purpose. All the Grid actions like Filtering, Searching, Sorting, etc., are performed based on the value in the Grid’s dataSource. Find the below code example and documentation for more information. 
 
 
 
[index.cshtml] 
 
@{ 
    var valueAccessor = "valueAccessorFn"; 
} 
 
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowFiltering="true" allowExcelExport="true" allowPdfExport="true" toolbar="@(new List<string>() { "PdfExport", "ExcelExport" })" toolbarClick="toolbarClick" > 
    <e-grid-filterSettings type="Excel"></e-grid-filterSettings> 
    <e-grid-columns> 
        <e-grid-column field="OrderID" headerText="Order ID" width="150"></e-grid-column> 
        <e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column> 
        <e-grid-column field="Verified" headerText="Status" valueAccessor="valueAccessor" width="150" filter="@(new { itemTemplate="#itemTemplate" })" allowEditing="false"></e-grid-column> 
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column> 
    </e-grid-columns> 
</ejs-grid> 
 
--- 
<script> 
    function valueAccessorFn(field, data, column) { 
        var value = data[field] 
        if (value) { 
            return "Passed" 
        } else { 
            return "Failed" 
        } 
    } 
</script> 
 
 
The same value returned from the valueAccessor will be exported to the excel and pdf file. 
 
Query #2: Also, the filter should display Passed/Failed instead of true false. 
  
By using filterItemTemplate, you can customize the filter choice text as you want. Refer to the below code example for more information. 
 
[index.cshtml] 
 
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowFiltering="true" allowExcelExport="true" allowPdfExport="true" toolbar="@(new List<string>() { "PdfExport", "ExcelExport" })" toolbarClick="toolbarClick" > 
    <e-grid-filterSettings type="Excel"></e-grid-filterSettings> 
    <e-grid-columns> 
        <e-grid-column field="OrderID" headerText="Order ID" width="150"></e-grid-column> 
        <e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column> 
        <e-grid-column field="Verified" headerText="Status" valueAccessor="valueAccessor" width="150" filter="@(new { itemTemplate="#itemTemplate" })" allowEditing="false"></e-grid-column> 
        <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column> 
    </e-grid-columns> 
</ejs-grid> 
 
 
<script id="itemTemplate" type="text/x-template"> 
    <div> 
        ${if(Verified)} 
        <span>Passed</span> ${else} 
        <span>Failed</span> 
        ${/if} 
    </div> 
</script> 
 
 
We have prepared a sample for your reference. You can get it from the below link, 
 
 
Please get back to us if you need further assistance with us. 
 
Regards,   
Rajapandiyan S 


Marked as answer

DA Danyelle March 16, 2021 03:04 PM UTC

How do I do this when my field is complex?

<e-grid-column field="LatestTest.Passed" headerText="Status" valueAccessor="valueAccessor" width="100"></e-grid-column>

The value is showing as undefined for this function I think it is because my field is complex:

  function valueAccessorFn(field, data, column) { 
        var value = data[field] 
        if (value) { 
            return "Passed" 
        else { 
            return "Failed" 
        } 
    } 


DA Danyelle March 16, 2021 06:31 PM UTC

I figured it out.
 function valueAccessorFn(field, data, column) {

        if (field === "LatestTest.Passed") {
            field = "LatestTest";
        }
        var value = data[field]["Passed"];
        if (value) {
            return "Passed";
        } else {
            return "Failed";
        }
    }




RS Rajapandiyan Settu Syncfusion Team March 17, 2021 04:51 AM UTC

Hi Danyelle, 

We are glad that you have resolved your problem. 

Please get back to us if you need further assistance with this. 

Regards, 
Rajapandiyan S 



DA Danyelle March 18, 2021 11:55 AM UTC

This is not working when exporting to pdf. When I print or export to excel ti shows Passed/Failed instead of true false but on pdf, it is still showing true/false.


RS Rajapandiyan Settu Syncfusion Team March 19, 2021 05:34 AM UTC

Hi Danyelle, 
 
Thanks for your update. 
 
Query: This is not working when exporting to pdf. When I print or export to excel ti shows Passed/Failed instead of true false but on pdf, it is still showing true/false. 
 
We have already logged a bug task (“value returned from the valueAccessor is not exported correctly in PDF”) for the reported issue at our end. At Syncfusion, we are committed to fixing all validated defects (subject to technical feasibility and Product Development Life Cycle ) and will include the defect fix in our upcoming patch release which will be rolled out on Mar 31st, 2021. 

You can now track the current status of your request, review the proposed resolution timeline, and contact us for any further inquiries through this link.  


Please get back to us if you need further assistance with this. 
 
Regards,   
Rajapandiyan S 


Loader.
Up arrow icon