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

Json get data

2 Answers 635 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Igor
Top achievements
Rank 1
Igor asked on 11 Aug 2012, 10:57 AM
My result is a JSON object that looks like: 

[{"pk": 1, "model": "adr.country", "fields": {"codetxt": "RUS", "codeint": 643, "name": "\u0420\u043e\u0441\u0441\u0438\u044f"}}, {"pk": 2, "model": "adr.country", "fields": {"codetxt": "UKR", "codeint": 443, "name": "\u0423\u043a\u0440\u0430\u0438\u043d\u0430"}}, {"pk": 3, "model": "adr.country", "fields": {"codetxt": "BLR", "codeint": 523, "name": "\u0411\u0435\u043b\u0430\u0440\u0443\u0441\u044c"}}]

My code:

$("#country").kendoComboBox({
                    placeholder: "Выберите страну...",
                    dataTextField: "codetxt", //not working
                    dataValueField: "codeint ", //not working
                    dataSource: {
                        serverFiltering: true,
                        type: "json",
                        transport: {
                            read: "http://localhost:8001/countries/"
                         }
                    }
                });

How can i get data in  kendoComboBox ?
Thanks! 

2 Answers, 1 is accepted

Sort by
0
John DeVight
Top achievements
Rank 1
answered on 13 Aug 2012, 05:08 PM
Hi Igor,

In the dataSource, the dataSource.schema.data can be set to a function to process the data first before binding it to the combobox.

This should work:
$("#country").kendoComboBox({
                    placeholder: "Выберите страну...",
                    dataTextField: "codetxt", //not working
                    dataValueField: "codeint ", //not working
                    dataSource: {
                        serverFiltering: true,
                        type: "json",
                        transport: {
                            read: "http://localhost:8001/countries/"
                         },
                         schema: {
                             data: function(response) {
                                 var dropDownListData = [];
                                 $.each(response, function(idx, country) {
                                     dropDownListData.push({
                                         pk: country.pk,
                                         model: country.model,
                                         codetxt: country.fields.codetxt,
                                         codeint: country.fields.codeint,
                                         name: country.fields.name
                                     });
                                 });
                                 return dropDownListData;
                             }
                         },
                    }
                });

Let me know if you have any problems...

Regards,

John DeVight
0
Phil
Top achievements
Rank 2
answered on 28 Aug 2012, 01:25 AM
Hi:

I think it should be simpler, as follows:
@{
    ViewBag.Title = "Combo DropDown Json #2";
}
<script type="text/javascript">
    $(document).ready(function () {
        $("#kjShipper").kendoComboBox(
            {
                dataTextField: "Text",
                dataValueField: "Value",
                index: 2,
                dataSource: {
                    transport: { read: { url: "/Input/GetShippers"} }
                }
            });
    });
</script>
<fieldset><legend> Combo Drop-down </legend>
    <br />
    <input id="kjShipper" />
</fieldset>
  
The index selects the second item.  The basic controller returns an empty view...
But the data is as follows:
public JsonResult GetShippers()
{
    var _shps =
        (from s in _db.Shippers
         select new
         {
             Value = s.ShipperID,
             Text = s.CompanyName
         }).ToList();
    return Json(_shps, JsonRequestBehavior.AllowGet);
}
Obviously, only for MVC.

Phil
Tags
ComboBox
Asked by
Igor
Top achievements
Rank 1
Answers by
John DeVight
Top achievements
Rank 1
Phil
Top achievements
Rank 2
Share this question
or