How to select value programmatically with MagicSearch jQuery Plugin?

前端 未结 1 792
被撕碎了的回忆
被撕碎了的回忆 2021-01-27 12:30

I am using this jQuery plugin MagicSearch

How can I programatically select a value (by ID for example)?

相关标签:
1条回答
  • 2021-01-27 12:57

    This is what I have. After a careful analysis of their callback functions, I have devised a method to extract the ID and Name from my search list.

    Please find the code to my attempt:

    <script>
    var idList="";
    var nameList = "";
    $(function () {
    
    var dataSource = "[{\"id\":1,\"name\":\"test\"}, 
    {\"id\":2,\"name\":\"example\"},{\"id\":3,\"name\":\"image\"}, 
    {\"id\":4,\"name\":\"election\"},{\"id\":5,\"name\":\"IPL\"}, 
    {\"id\":6,\"name\":\"Fashion\"},{\"id\":7,\"name\":\"Movies\"}, 
    {\"id\":8,\"name\":\"Pollution\"},{\"id\":9,\"name\":\"Modi\"}, 
    {\"id\":10,\"name\":\"Rahul\"},{\"id\":11,\"name\":\"Cricket\"}, 
    {\"id\":12,\"name\":\"Market\"},{\"id\":13,\"name\":\"TestKeyword\"}, 
    {\"id\":14,\"name\":\"testkeyword1\"},{\"id\":15,\"name\":\"testkeyword2\"}, 
    {\"id\":16,\"name\":\"testkeyword3\"}]";
    
    
        $('#basic').magicsearch({
            dataSource: dataSource,
            fields: ['name'],
            id: 'id',
            format: '%name%',
            hidden: true,
            multiple: true,
            focusShow: true,
            isClear: true,
            multiField: 'name',
            multiStyle: {
                space: 5,
                width: 200
            },
            success: function ($input, data) {
                idList = idList + data.id + ',';
                nameList = nameList + data.name + ',';
               // alert(data.id);
                return true;
            },
            afterDelete: function ($input, data) {
                idList = idList.replace(data.id + ',', "");
                nameList = nameList.replace(data.name + ',',"");
                //alert(data.id);
                return true;
            }
        });
    });
    

    Link to the fiddle: https://jsfiddle.net/yn0cmgjt/5/

    Excuse me for the bad interface. I had a difficult time getting the CDN link to this plugin and somehow it is not rendering the correct way. But play around with this and I hope it will suffice to your needs.

    The two lists (idList and nameList) that I have created are the id and name for each element concatenated in a single comma separated string. Once you have this string, you can extract out the required information.

    0 讨论(0)
提交回复
热议问题