This is a migrated thread and some comments may be shown as answers.

[Solved] Changing a bool field to display Yes No

8 Answers 155 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Shane P
Top achievements
Rank 1
Shane P asked on 07 Aug 2011, 06:03 AM
I am trying to display a boolean field as Yes?No vs true or false.

I have got this far

.Columns(columns =>
{
    columns.Command(commands =>
    {
        commands.Select().ButtonType(GridButtonType.Text);
    }).Width(80).Title("Publish");
    columns.Bound(o => o.Version).Width(80);
    columns.Bound(o => o.LastModified).Format("{0:dd-MM-yyyy hh:mm tt}");
    columns.Bound(o => o.Published).Template(o =>
                                             o.Published ? "Yes" : "No"
                                            );


The grid loads ok however the template hasn't been applied. Can anyone see what I am doing wrong?

Thanks
Shane

8 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 08 Aug 2011, 09:44 AM
Hi Shane P,

 Try this:

Template(o =>
{
%>

   <%= o.Published ? "True" : "False" %>

<%
});

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

0
Shane P
Top achievements
Rank 1
answered on 08 Aug 2011, 09:49 AM
Hi Atanas,
Sorry forgot to mention that im using razor. So your example wont work for me.
0
Atanas Korchev
Telerik team
answered on 09 Aug 2011, 02:06 PM
Hello Shane P,

 The Razor syntax is this:

.Template(@<text>
                @(item.Published ? "True" : "False")
</text>);

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

0
Shane P
Top achievements
Rank 1
answered on 09 Aug 2011, 08:54 PM
Hi Atanas,

Is this a bug with the latest release? as I have tried to use your suggestion however im only seeing either true or false

My binding of the columns looks like this

.Columns(columns =>
    {
        columns.Bound(o => o.ID).ClientTemplate("<a href='" + Url.Content("~/businessaccount/publish/<#= ID #>") + "' class='t-button t-grid-select'>Publish</a>").Title("Publish").Width(80);
        columns.Bound(o => o.Version).Width(80);
        columns.Bound(o => o.LastModified).Format("{0:dd-MM-yyyy hh:mm tt}");
        columns.Bound(o => o.Published).Template(@<text>
                                                 @(item.Published ? "True" : "False")
                                                 </text>);
    })
0
Atanas Korchev
Telerik team
answered on 10 Aug 2011, 05:28 AM
Hello Shane P,

There is no bug. The code is explicitly returning "True" or "False". I now see that you wanted "Yes" or "No" to be displayed. To do so just change the strings:

.Columns(columns =>
    {
        columns.Bound(o => o.ID).ClientTemplate("<a href='" + Url.Content("~/businessaccount/publish/<#= ID #>") + "' class='t-button t-grid-select'>Publish</a>").Title("Publish").Width(80);
        columns.Bound(o => o.Version).Width(80);
        columns.Bound(o => o.LastModified).Format("{0:dd-MM-yyyy hh:mm tt}");
        columns.Bound(o => o.Published).Template(@<text>
                                                 @(item.Published ? "Yes" : "No")
                                                 </text>);
    })

Best wishes,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

0
Shane P
Top achievements
Rank 1
answered on 10 Aug 2011, 05:44 AM
I must be doing something wrong as even if I use your example with True or False (note the capitals) the grid it returning only true or false in lowercase. 

My full code for my grid looks like

      @{ Html.Telerik().Grid<SP.Models.WebPageModel>()
.Name("Grid")
.DataKeys(dataKeys => dataKeys.Add(c => c.WebPageID))
.ClientEvents(e => e.OnLoad("Grid_OnLoad"))
.ToolBar(commands => commands.Custom().ButtonType(GridButtonType.Text).Action("create", "webpage").Text("Create Web Page").ImageHtmlAttributes(new { style = "margin-left:0" }))
.Columns(columns =>
{
    columns.Bound(o => o.ID).ClientTemplate("<a href='" + Url.Content("~/businessaccount/publish/<#= ID #>") + "' class='t-button t-grid-select'>Publish</a>").Title("Publish").Width(80);
    columns.Bound(o => o.Version).Width(80);
    columns.Bound(o => o.LastModified).Format("{0:dd-MM-yyyy hh:mm tt}");
    columns.Bound(o => o.Published).Template(@<text>
                                             @(item.Published ? "Yes" : "No")
                                             </text>);
})
 
 
.DataBinding(dataBinding => dataBinding
                              .Ajax()
                              .Select("GetWebPages", "BusinessAccount", new { id = (string)ViewData["id"] })
                              )
 
.Sortable()
.RowAction(row => row.Selected = row.DataItem.WebPageID.Equals(ViewData["id"]))
.Pageable()
.Footer(true)
.Render(); }



Do I need to reference some other js library or something to enable changing the template?
0
Atanas Korchev
Telerik team
answered on 10 Aug 2011, 05:54 AM
Hi Shane P,

 Your grid seems to be ajax bound (this was not specified before)- server templates are not applied during ajax binding and you need to use a client template:

.ClientTemplate("<#= Published? 'Yes' : 'No' #>")

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

0
Coding Machine
Top achievements
Rank 2
Iron
answered on 27 Feb 2013, 07:30 PM
Correct sample is (without tag brackets):

         columns.Bound(x => x.Active).ClientTemplate("#= Active? 'Yes' : 'No' #");
Tags
Grid
Asked by
Shane P
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Shane P
Top achievements
Rank 1
Coding Machine
Top achievements
Rank 2
Iron
Share this question
or