JavaScript UI Libraries & Blazor Components Suite – Smart UI › Forums › DropDownList › Drop Down List With On Demand Ajax Calls
Tagged: angular drop down list, custom dataSource, custom element, react drop down list, remote filtering, smart elements, smart framework, smart-drop-down-list, web component, web components
- This topic has 3 replies, 2 voices, and was last updated 4 years, 11 months ago by Hristofor.
-
AuthorPosts
-
December 5, 2019 at 8:13 pm #100529adminKeymaster
Hey there,
Im wondering if there is a way to like up a remote data source to a drop down list using Ajax to gather the data. Im not seeing anything in the documentation that explains how to do this.
Being able to do so will play a major role in whether or not we decide to move forward with Smart for our ui developmentDecember 6, 2019 at 1:28 pm #100532HristoforMemberHi ctstrist,
you can set thedataSource
property in your Ajax success function in order to load the new data to the element. ThedataSource
property can be set at any time with new data.
AlsodataSource
property accepts a callback function which allows to load custom data according to the filter input. This means that by entering a new value inside the filter field anew dataSource according to it will be loaded. Fitlering can be enabled by setting thefilterable
property. Here’s an example:window.onload = function () { //Sample data const data = [ { "name": "Afghanistan", "code": "AF" }, { "name": "land Islands", "code": "AX" }, { "name": "Antarctica", "code": "AQ" }, { "name": "Antigua and Barbuda", "code": "AG" }, { "name": "Argentina", "code": "AR" }, { "name": "Armenia", "code": "AM" } ]; const dropDownList = document.querySelector('smart-drop-down-list'); //Setting properties to the element dropDownList.filterable = true; dropDownList.displayMember = 'name'; dropDownList.valueMember = 'code'; dropDownList.dataSource = function (query, callback) { let result = []; // Creating a new dataSource according to the new input for (let d = 0; d < data.length; d++) { if (data[d].name.toLowerCase().indexOf(query.toLowerCase()) > -1) { result.push(data[d]); } } //The Ajax request goes here let xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { // Call the callback function here with the new result callback(result); } }; xhttp.open("GET", "filename", true); xhttp.send(); } }
December 6, 2019 at 6:14 pm #100533adminKeymasterIs there anything that needs to be down to initiate the data binding? Below is my code and it does not appear to be working (none of the console.log messages are being shown)
groupDropDown.dataSource = function (query, callback) {
console.log(‘here’);
//The Ajax request goes here
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
console.log(‘here2’);
if (this.readyState == 4 && this.status == 200) {
console.log(result);
callback(result);
}
};
xhttp.open(“GET”, “/Ajax/Ajax_group.aspx/GetDeviceGroupsForSmartDropDown”, true);
xhttp.send();
}December 9, 2019 at 9:20 am #100534HristoforMemberHi ctstrist,
if the dataSource is set to a callback function it will be used only by the filtering logic which will call the callback when the value of filter input inside the drop down is changed to a new value (by default is empty). If not the dataSource function is never called. The ‘query’ argument represents the current filtering query that can be used to filter the dataSource to return a custom list of data. The ‘callback’ argument represents the function that handles the new data and populates the drop down.
Best Regards,
Christopher
Smart HTML Elements Team
https://www.htmlelements.com -
AuthorPosts
- You must be logged in to reply to this topic.