How to select value programmatically with MagicSearch jQuery Plugin?

[亡魂溺海] 提交于 2019-12-20 07:24:06

问题


I am using this jQuery plugin MagicSearch

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


回答1:


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.



来源:https://stackoverflow.com/questions/53111915/how-to-select-value-programmatically-with-magicsearch-jquery-plugin

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