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

adding condtional checkbox template to grid

2 Answers 522 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 07 Dec 2011, 01:05 AM
Need some guidance adding a conditional code within a template. Without the conditional logic below, all is well. Please see the code in bold below. Thanks for any feedback you might provide.

$("#mysgrid").kendoGrid({
dataSource: {
transport: {
       read: {
           url: "myurl",
           dataType: "json",
data: {
   eventclusterid: x,
   json: 'true'
        }
}
    } ,
schema: { 
data: function(roledata) {
return mydata;
}
}
},
columns:[
{ field: "description", title: "Role Description"},
{ field: "roleid", title: "Assigned", width: "75px",
   template: '#if (eventclusterid > 0) 
         {# <input type="checkbox" name="roleid" value="#= roleid #" checked="checked"> #}
       else
        {# <input type="checkbox" name="roleid" value="#= roleid #"> #} #'
    }

],
sortable: true, selectable: "multiple", navigatable: true
 });

2 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 07 Dec 2011, 09:46 AM
Hello Chris,

 Multiline JavaScript strings need a "\" at the end of each line:

 template: '#if (eventclusterid > 0) { \ #
                          <input type="checkbox" name="roleid" value="#= roleid #" checked="checked"> \
                    # } else { # \
                          <input type="checkbox" name="roleid" value="#= roleid #">' \
                     # } #';
 
Other than that the code looks correct. Perhaps an easier approach is to use a function which returns the html:

template: "#= checkbox(eventclusterid, roleid) "

<script>
function checkbox(eventclusterid, roleid) {
       if (...) {
           return '<input type="checkbox" value="' + roleid + '" checked="checked"';
       } else {
           return ...;
       }
}
</script>
Regards,

Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Chris
Top achievements
Rank 1
answered on 07 Dec 2011, 06:42 PM
I created a separate function as you suggested and that worked perfectly. Thanks for the idea!

Really nice job on the framework , btw. I'm evaluating it for a startup and I think this is the way we will go.
Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Chris
Top achievements
Rank 1
Share this question
or