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

How do you display Boolean column as a readonly checkbox

3 Answers 366 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.
Phil
Top achievements
Rank 1
Phil asked on 26 Apr 2012, 08:40 AM
I've been googling for an hour. Can't find the answer to this seemingly straightforward question.

How do you display a model's boolean column as a readonly checkbox instaed of the default text (true or false)?

My model contains a boolean property called IsInternal.
Here's my code snippet.

.Columns(columns =>

   {             

     columns.Bound(o => o.IsInternal)
            .ClientTemplate("<input type='checkbox' name='IsInternal' value='<#= IsInternal #>' readonly /> ");


This does not work. It results in a column of unchecked editable checkboxes that are not bound to my model.

Can someone please help.
Regards

Phil

3 Answers, 1 is accepted

Sort by
0
Phil
Top achievements
Rank 1
answered on 26 Apr 2012, 10:33 AM
Okay, I've answered my own question. I had got a few bum-steers from other forum entries - like the readonly bit.

The correct syntax is:

columns.Bound(o => o.IsInternal)
    .ClientTemplate("<input type='checkbox' name='IsInternalCb' <#= IsInternal? 'checked' : '' #> disabled /> ");

Basically I've:

1. Given it a name attribute incase i need to reference it later. (Probably won't need to)
2. Conditionally added the 'checked' attribute if my boolean column IsInternal = true, else added empty string
3. Added 'disabled' attribute to make it read only

Hope this saves other people some time.

Phil

0
jerin
Top achievements
Rank 1
answered on 22 Jun 2012, 07:57 AM
thanks Its Working 
columns.Bound(o => o.IsInternal)
    .ClientTemplate("<input type='checkbox' name='IsInternalCb' <#= IsInternal? 'checked' : '' #> disabled /> "); 
0
Thomas
Top achievements
Rank 1
answered on 24 Sep 2012, 04:41 PM
Thank you, just what I needed.

A little tweak for MVC users:

columns.Bound(o => o.IsInternal)
    .ClientTemplate("<input type='checkbox' name='IsInternalCb' #= IsInternal? 'checked' : '' # disabled /> "); 

Leave out the brackets in <#= IsInternal? 'checked' : '' #> for it to work.
Tags
Grid
Asked by
Phil
Top achievements
Rank 1
Answers by
Phil
Top achievements
Rank 1
jerin
Top achievements
Rank 1
Thomas
Top achievements
Rank 1
Share this question
or