Json get data

3 posts, 0 answers
  1. Igor
    Igor avatar
    5 posts
    Member since:
    Aug 2012

    Posted 11 Aug 2012 Link to this post

    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. John DeVight
    John DeVight avatar
    209 posts
    Member since:
    Jan 2010

    Posted 13 Aug 2012 Link to this post

    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
  3. Kendo UI is VS 2017 Ready
  4. Phil
    Phil avatar
    273 posts
    Member since:
    Jul 2008

    Posted 27 Aug 2012 Link to this post

    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
Back to Top