How to use JSON data with kendoAutoComplete

I’m learning to use Kendo with jQuery.

I have downloaded a sample Kendo UI from their website and am adding an autocomplete widget for employees which needs to be sourced from ’employees-list.json’

I have successfully implemented the autocomplete widget using hard coded data:

<p>Search Member: <input id="member-auto-complete" /></p>

<script>
  $('#member-auto-complete').kendoAutoComplete({
    dataSource: ["Ant", "Antilope", "Badger", "Beaver", "Bird"]
  });
  }
</script>

I need help to replace the dataSource with the json file. Here is an example of how Kendo uses employees.json as a data source in a different widget ‘kendoScheduler’:

              $("#employee-sales").kendoScheduler({
              autoBind:false,
              date: new Date("1996, 7, 1"),
              views: ["month"],
              editable:false,
              timezone: "Etc/UTC",
              dataSource:{
                  transport:{
                      read: {
                          url: "./content/employee-sales.json",
                          crossDomain: true,
                          dataType: "jsonp",
                          jsonp: false,
                          jsonpCallback: "callback_es"
                      }
                  },

And here is my attempt to do the same which is returning [object, Object] for each employee where instead I want their names.

          $('#member-auto-complete').kendoAutoComplete({
            dataSource: {
                transport: {
                        read: {
                            url: './content/employees-list.json',
                            crossDomain: true,
                            dataType: "jsonp",
                            jsonp: false,
                            jsonpCallback: "callback_el"
                        }
                    },
                }  
          })

And here is the content of employees.json

callback_el([{"EmployeeID":2,"FirstName":"Andrew","EmployeeName":"Andrew Fuller","LastName":"Fuller","Title":"Vice President, Sales","Country":null,"City":null,"Address":null,"HomePhone":"(206) 555-9482","Notes":"Andrew received his BTS commercial in 1974 and a Ph.D. in international marketing from the University of Dallas in 1981.  He is fluent in French and Italian and reads German.  He joined the company as a sales representative, was promoted to sales manager in January 1992 and to vice president of sales in March 1993.  Andrew is a member of the Sales Management Roundtable, the Seattle Chamber of Commerce, and the Pacific Rim Importers Association."},{"EmployeeID":9,"FirstName":"Anne","EmployeeName":"Anne Dodsworth","LastName":"Dodsworth","Title":"Sales Representative","Country":null,"City":null,"Address":null,"HomePhone":"(71) 555-4444","Notes":"Anne has a BA degree in English from St. Lawrence College.  She is fluent in French and German."},{"EmployeeID":3,"FirstName":"Janet","EmployeeName":"Janet Leverling","LastName":"Leverling","Title":"Sales Representative","Country":null,"City":null,"Address":null,"HomePhone":"(206) 555-3412","Notes":"Janet has a BS degree in chemistry from Boston College (1984).  She has also completed a certificate program in food retailing management.  Janet was hired as a sales associate in 1991 and promoted to sales representative in February 1992."},{"EmployeeID":8,"FirstName":"Laura","EmployeeName":"Laura Callahan","LastName":"Callahan","Title":"Inside Sales Coordinator","Country":null,"City":null,"Address":null,"HomePhone":"(206) 555-1189","Notes":"Laura received a BA in psychology from the University of Washington.  She has also completed a course in business French.  She reads and writes French."},{"EmployeeID":4,"FirstName":"Margaret","EmployeeName":"Margaret Peacock","LastName":"Peacock","Title":"Sales Representative","Country":null,"City":null,"Address":null,"HomePhone":"(206) 555-8122","Notes":"Margaret holds a BA in English literature from Concordia College (1958) and an MA from the American Institute of Culinary Arts (1966).  She was assigned to the London office temporarily from July through November 1992."},{"EmployeeID":6,"FirstName":"Michael","EmployeeName":"Michael Suyama","LastName":"Suyama","Title":"Sales Representative","Country":null,"City":null,"Address":null,"HomePhone":"(71) 555-7773","Notes":"Michael is a graduate of Sussex University (MA, economics, 1983) and the University of California at Los Angeles (MBA, marketing, 1986).  He has also taken the courses \"Multi-Cultural Selling\" and \"Time Management for the Sales Professional.\"  He is fluent in Japanese and can read and write French, Portuguese, and Spanish."},{"EmployeeID":1,"FirstName":"Nancy","EmployeeName":"Nancy Davolio","LastName":"Davolio","Title":"Sales Representative","Country":null,"City":null,"Address":null,"HomePhone":"(206) 555-9857","Notes":"Education includes a BA in psychology from Colorado State University in 1970.  She also completed \"The Art of the Cold Call.\"  Nancy is a member of Toastmasters International."},{"EmployeeID":7,"FirstName":"Robert","EmployeeName":"Robert King","LastName":"King","Title":"Sales Representative","Country":null,"City":null,"Address":null,"HomePhone":"(71) 555-5598","Notes":"Robert King served in the Peace Corps and traveled extensively before completing his degree in English at the University of Michigan in 1992, the year he joined the company.  After completing a course entitled \"Selling in Europe,\" he was transferred to the London office in March 1993."},{"EmployeeID":5,"FirstName":"Steven","EmployeeName":"Steven Buchanan","LastName":"Buchanan","Title":"Sales Manager","Country":null,"City":null,"Address":null,"HomePhone":"(71) 555-4848","Notes":"Steven Buchanan graduated from St. Andrews University, Scotland, with a BSC degree in 1976.  Upon joining the company as a sales representative in 1992, he spent 6 months in an orientation program at the Seattle office and then returned to his permanent post in London.  He was promoted to sales manager in March 1993.  Mr. Buchanan has completed the courses \"Successful Telemarketing\" and \"International Sales Management.\"  He is fluent in French."}]);

>Solution :

You simply need to supply the dataTextField option to let it know which field in your data source objects to use.

For example

$('#member-auto-complete').kendoAutoComplete({
  dataTextField: "EmployeeName"
  dataSource: {
    // ...
  }
});

Leave a Reply