选择2 4自定义数据适配器

编程入门 行业动态 更新时间:2024-10-24 01:55:14
本文介绍了选择2 4自定义数据适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试根据此处的示例创建自定义数据适配器: select2.github.io/announcements-4.0.html#query-to-data-adapter 。 如何在函数外部使用DataAdapter的定义移动创建select2控件的行(参见下面的代码)?

I am trying to create a custom data adapter according to an example here: select2.github.io/announcements-4.0.html#query-to-data-adapter. How can I move the line that creates the select2 control outside the function with definition of DataAdapter (see the code below)?

<!DOCTYPE html> <head> <title></title> <link href="select2.css" rel="stylesheet" /> <script type="text/javascript" src="code.jquery/jquery-2.1.4.js"></script> <script type="text/javascript" src="select2.full.js"></script> <script type="text/javascript"> $.fn.select2.amd.require( ['select2/data/array', 'select2/utils'], function (ArrayData, Utils) { function CustomData ($element, options) { CustomData.__super__.constructor.call(this, $element, options); } Utils.Extend(CustomData, ArrayData); CustomData.prototype.query = function (params, callback) { var data = {results: []}; data.results.push({id: params.term, text: params.term}); data.results.push({id: 11, text: 'aa'}); data.results.push({id: 22, text: 'bb'}); callback(data); }; // Works if uncommented, but this line needs to be elsewhere (in $(document).ready()). //$("#my").select2({tags: true, dataAdapter: CustomData}); }); $(document).ready(function() { // This line does not work here. $("#my").select2({tags: true, dataAdapter: CustomData}); }); </script> </head> <body> <select id="my"></select> </body> </html>

推荐答案

你通过AMD-Pattern定义它:

you define it via AMD-Pattern:

$.fn.select2.amd.define('select2/data/customAdapter',[ 'select2/data/array', 'select2/utils' ], function (ArrayAdapter, Utils) { function CustomDataAdapter ($element, options) { CustomDataAdapter.__super__.constructor.call(this, $element, options); } Utils.Extend(CustomDataAdapter, ArrayAdapter); CustomDataAdapter.prototype.current = function (callback) { callback(...); }; return CustomDataAdapter; } ); var customAdapter=$.fn.select2.amd.require('select2/data/customAdapter'); $("#my").select2({ tags: true, dataAdapter: customAdapter });

更多推荐

选择2 4自定义数据适配器

本文发布于:2023-11-26 21:26:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635246.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   适配器   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!