sergiodlopes / jquery-flexdatalist

Flexible input autocomplete/datalist plugin for jQuery
http://projects.sergiodinislopes.pt/flexdatalist/
MIT License
365 stars 84 forks source link

Can't set an initial default value #165

Closed cescKam closed 6 years ago

cescKam commented 6 years ago

I'm using the latest version (2.2.4) and I can't set via html value or script a default value. When jquery starts it hides the value and don't set the refered item. Here is my code:

HTML

<input id="pickup" name="pickup" value="<?php echo $pickup ?>"
placeholder="<?php esc_html_e('Pick up location', 'transfers'); ?>" 
type="text" class="origin-data-list flexdatalist form-control search-input" />

JS

var flexdatalistOptionsDefault = {
        minLength: 0,
        valueProperty: "id",
        visibleProperties: ["name", 'is_airport'],
        textProperty: "{name}, {country}",
        groupBy: ["country"],
        searchIn: "name",
        searchByWord: true,
        selectionRequired: false,
        noResultsText: "No results"
}

$(document).ready(function () {
    dataListInit(".origin-data-list");
});
function dataListInit(target) {
    var value = $(target).val();
    $(target).flexdatalist(flexdatalistOptionsDefault);
    if(value.length === 0 || !value.trim())
        $(target).flexdatalist('value',value)
}

Thank you very much in advance, good job!

cescKam commented 6 years ago

Problem solved! The set value function of flexdatalist lib parse the value to string and doesn't find the id.