Site front-end _jquery-Select plugins. Select2 with remote data to implement the default tags selection?

Source: Internet
Author: User

Brief description:

Description: Select2 is the extension of the dropdown box plugin chosen, which makes the ugly long select box look better, more convenient, supports search, remote data, and unlimited scrolling effects.


Related requirements:

1. As a result of the work needs, decided to write a more flexible general CMDB with Python+flask, on the asset editing page you want to be able to search the matching asset group in real time and edit the page to automatically load the last assigned group


Remote Data:

<script type= "Text/javascript" >    $ (". Mgrup"). Select2 ({         ajax: {             url:  "{{ url_for (' Asset.asset_ag_json ')  }}",             dataType:  ' JSON ',             delay: 250,            data : function  (params)  {                 return {                     s: params.term,                     p: params.page                 };             },             processresults: function  (Data, params)  {                 params.page = params.page | |  1;                return  {                     results: data.items,                     pagination: {                          more:  (params.page * 10) &NBSP;&LT;&NBsp;data.total_count                     }                 };            },             cache: true         },        escapeMarkup: function  (data)  {            var markup =  data            return markup         },        minimuminputlength:  1,        templateResult: function  (data)  {             var markup =  ' <div class= "Clearfix" >  +                            ' <div class= ' col-sm-6 > '  + data.id +  ' </ Div> '  +                           ' <div class= ' col-sm-6 ' > '  +  data.name +  ' </div></div> '              return markup        },         templateSelection: function  (data)  {             var markup = data;             return markup.name        },         initSelection: function  (Element, callback) {             $.ajax ({                 url:  ' {{ url_for (' Asset.asset_ag_json ')  } ',                 dataType:  ' JSON ',                 data: {u:   "{{ request.args.get (' uuid ')  }}"  },             }). Done (function  (data) {                 callback (Data.items)              }) &NBSP;&Nbsp;      }    });</script> 

Note: As in the code above, Templateresult is primarily used to render the results returned by the Ajax real-time search, while templateselection is primarily used for callback writing. The tags name of the mgrup corresponding select box, Initselection is mainly used for the first open when the initial state of all tags, check for a long time ~ I rub ...


There are pictures and phases:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/8A/29/wKiom1gpW9PzlbLJAAB4naXaaXE820.png "title=" Select2.png "alt=" Wkiom1gpw9pzlbljaab4naxaaxe820.png "/>




This article is from the "Li-Yun Development Road" blog, please be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1872644

Site front-end _jquery-Select plugins. Select2 with remote data to implement the default tags selection?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.