How can I set a default value for Flexdatalist using jQuery

核能气质少年 提交于 2021-01-29 05:26:35

问题


I am using Flexdatalist to provide auto completion functionality. My script is something like the following:

$('#locationKeyword').flexdatalist({
        url: 'serverAction',
        cache: false,
        searchIn: ["ProvinceName", "CityName", "TownName"],
        visibleProperties: ["ProvinceName", "CityName", "TownName"],
        groupBy: 'ProvinceName',
        value:'1,1,1',
        selectionRequired: false,
        focusFirstResult: true,
        minLength: 1,
        maxShownResults: 20,
        valueProperty: '{Value}',
        textProperty: '{ProvinceName}, {CityName}, {TownName}',
        searchContain: true
    });

The server side code(Asp.NET MVC) returns an array of objects like the following:

return Json(new
        {
            ProvinceName = item.Province.Name,
            CityName = item.City.Name,
            TownName = item.Town.Name,
            Value = item.ProvinceId + "," + item.CityId + "," + item.TownId
        });

All item.ProvinceId, item.CityId, item.TownId are int;

The problem: How can I set a default value for it. For example, I want to one combination of Province,City,Town to be selected.


回答1:


Thanks all!

I found the solution as below:

Html:

<input name="BusinessLocation" type="text" class="locationKeyword" />

Script:

$('[name=BusinessLocation]').flexdatalist({
        url: options.businessLocationUrl,
        cache: false,
        searchIn: ["ProvinceName", "CityName", "TownName"],
        visibleProperties: ["ProvinceName", "CityName", "TownName"],
        groupBy: 'ProvinceName',
        selectionRequired: true,
        focusFirstResult: true,
        minLength: 1,
        maxShownResults: 20,
        textProperty: '{ProvinceName}, {CityName}, {TownName}',
        searchContain: true,
        valueProperty: 'Value'
    });        

    $('.locationKeyword').each(function (index) {
        if ($(this).hasClass('flexdatalist-alias')) {
            $(this).val('تهران, تهران, یوسف آباد');
        } else {
            $(this).val('1,1,1');
        }
    });

    $('[name=BusinessLocation]').val('1,1,1');


来源:https://stackoverflow.com/questions/43187255/how-can-i-set-a-default-value-for-flexdatalist-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!