Issue with pre-selected default value in AngularJS TypeScript Kendo UI DropDownList

I have successfully implemented a list of objects for items, but now I am facing a challenge in adding a default selected value.

Below is the HTML code for the drop-down:

<select kendo-drop-down-list 
        k-options="selectItems" 
        k-ng-model="selectedItem">
</select>

In my AngularJS (using TypeScript) implementation, I define the drop-down as follows:

this.itemsToSelect = [{id: 1, name: "One"}, {id: 2, name: "Two"}];

this.selectItems = {
    optionLabel: "Select items...",
    dataTextField: "name",
    dataValueField: "id",
    dataSource: new kendo.data.DataSource({
      transport: {
        read: (options) => {
          options.success(this.itemsToSelect);
        }
      }
    }),
    value: this.itemsToSelect[0]
  };

Despite this setup, the default value is not being selected.

I tested their documentation code which works with string items, and noticed that for object items, it required specifying the ID like this:

value: this.itemsToSelect[0].id

Their code snippet using jQuery looks like this:

<script>
    let items = [{id: 1, name: "one"}, {id: 2, name: "two"}];

    $("#dropdownlist").kendoDropDownList({
        dataTextField: "name",
        dataValueField: "id",
        dataSource: items,
        value: items[1].id
    });
</script>

However, implementing this in my code did not yield the desired result.

There are no errors in the code. Any suggestions on how to resolve this?

EDIT

As a workaround, I can manually set the selected item like this:

this.selectedItem = this.itemsToSelect[0];

Although this works by setting k-ng-model in the HTML, I am still curious if there's a way to achieve this through the value property within the this.selectItems object?

Answer №1

To create a local object that represents your default value, you can do something like this:

{ 
id: 1,
name: "Jane"
}

After creating the object, make sure to set autoBind to false on the control.

For more information, check out this link.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for removing residue from old watches

Our angularJS web components are integrated with a jqxGrid. Whenever a user makes edits in a cell, we implement a custom typeahead editor using Angular. However, I have noticed that after the editor is destroyed, my $watches array does not revert back to i ...

Is there a way for me to convert my (TypeScript Definition) .d.ts file into a (JavaScript) .js file?

It seems that there are plenty of guides available on converting a file from .js to .d.ts, but not the other way around. Specifically, I have some code in .d.ts format and I would like to convert it into JavaScript. Can anyone offer assistance with this t ...

Combining one item from an Array Class into a new array using Typescript

I have an array class called DocumentItemSelection with the syntax: new Array<DocumentItemSelection>. My goal is to extract only the documentNumber class member and store it in another Array<string>, while keeping the same order intact. Is th ...

Trouble displaying loaded JSON data with $http GET in ng-table

I have recently delved into learning angularjs and am currently experimenting with using ng-table to display the results of blast searches. Everything runs smoothly when I directly add the JSON data in the JavaScript script. However, I have been unsuccess ...

Angular version 7.2.1 encounters an ES6 class ReferenceError when attempting to access 'X' before it has been initialized

I have encountered an issue with my TypeScript class: export class Vehicule extends TrackableEntity { vehiculeId: number; constructor() { super(); return super.proxify(this); } } The target for my TypeScript in tsconfig.json is set to es6: ...

Retrieve the variable declared within the event

How can I access a variable set in an event? Here is the code snippet: $scope.$on('event_detail', function (event, args) { $scope.id = args; console.log($scope.id); // This prints the correct value }); console.log($scope.id); // ...

Literal Route Waypoints Guidance Service

I am currently working on an angularjs directive that utilizes the Google Maps Direction Service. In my controller, I have a getFunction that watches for changes in a scope variable. Once the scope variable changes, it triggers the calculateAndDisplayRoute ...

Having trouble implementing min and max date validation in Angular UI-Bootstrap datepicker with UI-Bootstrap version 1.3.3

My goal is to implement validation in my datepicker, preventing the user from selecting a date within 30 days before or after the current date. Here's the code snippet I'm currently using for the datepicker: <div class="form-group" ng-class=" ...

Mongoose encountered an error when attempting to cast the value "ObjectID" to an ObjectId at the specified path "red.s1"

My Mongoose schema is structured as follows: const gameSchema = new Schema({ matchNumber: { type: Number, required: [true, 'A match must have a number!'], unique: true }, red: { s1: { type: ...

Encountering issues with verifying login credentials in Angular 2

Greetings! I have designed a login page where the user will be logged in if the response is successful, otherwise an error message will be displayed. Below is the JSON data with email and password fields: { Email": "<a href="/cdn-cgi/l/email-protect ...

The Model.function method is not a valid function that can be used within a router

Currently facing a challenge with my router setup. I have exported the function as shown in the code snippet below. This is the Model I am using: "use strict"; var mongoose = require('mongoose'); var bcrypt = require("bcryptjs"); var Schema = m ...

HTML Code for Tracking Facebook Friends' Likes on a Page

I am looking to show the number of friends who have liked a page in HTML. Can someone please guide me on this? I am aware that we can get the page likes using a simple URL, but I specifically want to know how to retrieve the count of friends who liked a ...

Trigger event when ngModel changes

Currently, I am trying to perform a test on a select element... <select [ngModel]="selectedRouters" name="routerName" class="form-control" id="routersSelect" size="12" (ngModelChange)="selectRouters($event)" multiple> <option [value]="route ...

I am currently analyzing a JSON file that contains deeply nested JavaScript Objects. My goal is to rearrange the data so that objects containing a specific field value are placed at the top of the list

Currently, I am parsing a JSON file which contains a map of JavaScript objects. For instance: { offers : { "1":{"id":"1", "category":"a", "offerType":"LS"}, "2":{"id":"2", "category":"a", "offerType":"EX"}, ...

The type of 'username' cannot be determined without specifying the reference to '@angular/forms/forms' in the node modules

I'm encountering an issue with my application: forgot-password.component.ts(44,7): error TS2742: The inferred type of 'username' cannot be named without a reference to '.../node_modules/@angular/forms/forms'. This is likely not po ...

Troubleshooting the issue with UI-Router AngularJS controller not functioning properly in a

Trying to grasp the ins and outs of UI-Router in conjunction with Angular has proven to be quite challenging for me. In my setup, there's an index: <body> <div ui-view></div> <!--Location of file holding app--> <scri ...

What is the best way to assign the initial value in a dropdown menu populated by a database table?

I'm new to AngularJS and I need help writing a function that can populate a select element with data from a database table. While I am able to fill the select element, I am struggling to set the default value for it. Here is my code: HTML : <div ...

The significance of zone.js and rxjs within the context of Angular 2

As a newcomer to Angular2, I recently learned about zone.js and rxjs. I'm curious to know if they both serve the same purpose for handling asynchronous tasks, or if each has its own specific role. Can someone explain to me the exact reasons why zone.j ...

React and TypeScript warns about possible undefined object

certificate?: any; <S.Contents>{certificate[0]}</S.Contents> <S.Contents>{certificate[1]}</S.Contents> <S.Contents>{certificate[3]}</S.Contents> If I set the type of props to `any` and use it as an index of an array, e ...

Combine QuickBooks with a Rails and AngularJS application

I am currently working on creating an internal CRM system for a client that requires synchronization with QuickBooks for all financial transactions. However, I have hit a roadblock in the implementation process. I have been advised to start with webconnec ...