问题
I have been able to successfully build out a dynamic form using JSON and Kendo.Observable, however I cannot initialize the dropdownlist values successfully within the same JSON. The only way I can get it to work is by binding the dropdown lists to a separate json request after creation. See the example below....
Here is an example of some JSON that works (no dropdown list)
{"fields": [
{"name" : "FirstName", "label" : "First Name", "type" : "text", "css" : "test"},
{"name" : "LastName", "label" : "Last Name", "type" : "text", "css" : "test"},
{"name" : "Email", "label" : "Email", "type" : "text", "css" : "test"},
{"name" : "Phone", "label" : "Phone", "type" : "text", "css" : "test"},
{"name" : "Subscribed", "label" : "Subscribed", "type" : "checkbox", "css" : "test"}
]}
Here is an example where I have added a dropdown, not going to post the whole thing, I have tried a number of different variations on the below to try and populate the select but can't find any that work
{"fields": [
{"name" : "Email", "label" : "Email", "type" : "text", "css" : "test"},
{"name" : "FirstName", "label" : "First Name", "type" : "text", "css" : "test"},
{"name" : "LastName", "label" : "Last Name", "type" : "text", "css" : "test"},
{"name" : "Company", "label" : "Company", "type" : "text", "css" : "test"},
{"name" : "ddlCountry", "label" : "Country", "type" : "select", "dataTextField" : "text", "dataValueField" : "value", "dataSource":[{"text" : "AF","value" : "Afghanistan"},{"text" : "AL","value" : "Albania"},{"text" : "DZ","value" : "Algeria"},{"text" : "AS","value" : "American Samoa"},{"text" : "AD","value" : "Andorra"},...etc...
and here is the script to bind it
$.ajax({
url: "http://localhost/go/getformjson",
type: "GET",
dataType: "json",
success: function (model) {
// convert the JSON to observable object
var viewModel = kendo.observable(model);
// bind the model to the container
kendo.bind($("#example"), viewModel);
}
});
回答1:
You need to specify the data-text-field and data-value-field attributes:
<select data-bind="source: options" data-text-field="ddltext" data-value-field="ddlvalue" />
Here is updated version of your fiddle: http://jsfiddle.net/aUAJv/64/
来源:https://stackoverflow.com/questions/23145505/kendo-ui-creating-a-dynamic-form-via-json