Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当尝试为select2组合框创建自定义数据适配器时,是什么原因导致;未捕获的TypeError baseName拆分不是函数“;?_Javascript_Jquery Select2 4_Js Amd - Fatal编程技术网

Javascript 当尝试为select2组合框创建自定义数据适配器时,是什么原因导致;未捕获的TypeError baseName拆分不是函数“;?

Javascript 当尝试为select2组合框创建自定义数据适配器时,是什么原因导致;未捕获的TypeError baseName拆分不是函数“;?,javascript,jquery-select2-4,js-amd,Javascript,Jquery Select2 4,Js Amd,我想对本地数据数组使用带有分页的select2组合框(无ajax调用)。为此,我正在考虑。初始化自定义适配器的代码失败 我尝试过创建一个与此类似的自定义数据适配器 将自定义数据适配器添加到select2对象时 $.fn.select2.amd.require( 'select2/data/customAdapter', ['select2/data/array', 'select2/utils'] 我收到这个错误(在chrome和firefox中) 以及此警告(chrome、f

我想对本地数据数组使用带有分页的select2组合框(无ajax调用)。为此,我正在考虑。初始化自定义适配器的代码失败

我尝试过创建一个与此类似的自定义数据适配器

将自定义数据适配器添加到select2对象时

$.fn.select2.amd.require(
        'select2/data/customAdapter', ['select2/data/array', 'select2/utils']
我收到这个错误(在chrome和firefox中)

以及此警告(chrome、firefox、edge)

版本

  • select2:4.0.7(我无法将旧版本的select2与查询选项一起使用)
  • jquery:3.4.1

    • 只是一个猜测,但它不应该是这样的吗

      $.fn.select2.amd.require(
        'select2/data/customAdapter',
        function(customAdapter) {
          $('.dropdownbox').select2({
            dataAdapter: customAdapter,
          });
        }
      );
      
      或一次加载所有额外模块:

      $.fn.select2.amd.require(
        [
          'select2/data/customAdapter',
          'select2/data/array',
          'select2/utils',
        ],
        function(customAdapter, ArrayData, Utils) {
          $('.dropdownbox').select2({
            dataAdapter: customAdapter,
          });
          function CustomDataAdapter($element, options) {
            CustomDataAdapter.__super__.constructor.call(
              this,
              $element,
              options
            );
          }
          //rest of the code
      
      打字错误

      它应该是
      define
      ,而不是
      require

      $.fn.select2.amd.define(
          'select2/data/customAdapter', ['select2/data/array', 'select2/utils']
      
      在select2论坛和

      下面是工作示例(类似于链接中的示例代码)

      
      使用自定义数据适配器选择2
      abc
      ghi
      ghi
      jkl
      $(文档).ready(函数(){
      控制台日志(“之前”);
      $.fn.select2.amd.define(
      'select2/data/customAdapter',['select2/data/array','select2/utils'],
      函数(ArrayData、Utils){
      函数CustomDataAdapter($element,options){
      CustomDataAdapter.super.constructor.call(此$element,选项);
      }
      Extend(CustomDataAdapter,ArrayData);
      CustomDataAdapter.prototype.current=函数(回调){
      控制台日志(“当前”);
      };
      CustomDataAdapter.prototype.query=函数(参数,回调){
      console.log(“查询”);
      };
      返回CustomDataAdapter;
      });
      控制台日志(“之后”);
      var customAdapter=$.fn.select2.amd.require('select2/data/customAdapter');
      $(“#下拉框”)。选择2({
      dataAdapter:customAdapter
      });
      });
      
      <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Select2 With Custom Data Adapter</title>
        <link href="./css/select2.min.css" rel="stylesheet" />
      </head>
      <body>
      
        <select class="dropdownbox" name="state">
          <option value="abc">abc</option>
          <option value="def">ghi</option>
          <option value="ghi">ghi</option>
          <option value="jkl">jkl</option>
        </select>
      
        <script type="text/javascript" src="./js/jquery-3.4.1.js"></script>
        <script type="text/javascript" src="./js/select2.js"></script>
      
        <script>
      
          $(document).ready(function () {
      
            //$.fn.select2.defaults.set('amdBase', 'select2/');
      
            console.log("before");
            $.fn.select2.amd.require(
              'select2/data/customAdapter', ['select2/data/array', 'select2/utils'],
              function (ArrayData, Utils) {
                function CustomDataAdapter($element, options) {
                  CustomDataAdapter.__super__.constructor.call(this, $element, options);
                }
      
                Utils.Extend(CustomDataAdapter, ArrayData);
      
                CustomDataAdapter.prototype.current = function (callback) {
                  console.log("current");
                };
      
                CustomDataAdapter.prototype.query = function (params, callback) {
                  console.log("query");
                };
      
                return CustomDataAdapter;
      
              });
            console.log("after");
      
      
            var customAdapter = $.fn.select2.amd.require('select2/data/customAdapter');
      
      
            $('.dropdownbox').select2({
              dataAdapter: customAdapter
            });
          });
        </script>  
      </body>  
      </html>
      
      $.fn.select2.amd.require(
        'select2/data/customAdapter',
        function(customAdapter) {
          $('.dropdownbox').select2({
            dataAdapter: customAdapter,
          });
        }
      );
      
      $.fn.select2.amd.require(
        [
          'select2/data/customAdapter',
          'select2/data/array',
          'select2/utils',
        ],
        function(customAdapter, ArrayData, Utils) {
          $('.dropdownbox').select2({
            dataAdapter: customAdapter,
          });
          function CustomDataAdapter($element, options) {
            CustomDataAdapter.__super__.constructor.call(
              this,
              $element,
              options
            );
          }
          //rest of the code
      
      $.fn.select2.amd.define(
          'select2/data/customAdapter', ['select2/data/array', 'select2/utils']
      
      <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Select2 With Custom Data Adapter</title>
        <link href="./css/select2.min.css" rel="stylesheet" />
      </head>
      <body>
      
        <select id="dropdownboxid">
          <option value="abc">abc</option>
          <option value="def">ghi</option>
          <option value="ghi">ghi</option>
          <option value="jkl">jkl</option>
        </select>
      
        <script type="text/javascript" src="./js/jquery-3.4.1.js"></script>
        <script type="text/javascript" src="./js/select2.js"></script>
        <script>
      
          $(document).ready(function () {
      
            console.log("before");
            $.fn.select2.amd.define(
              'select2/data/customAdapter', ['select2/data/array', 'select2/utils'],
              function (ArrayData, Utils) {
                function CustomDataAdapter($element, options) {
                  CustomDataAdapter.__super__.constructor.call(this, $element, options);
                }
      
                Utils.Extend(CustomDataAdapter, ArrayData);
      
                CustomDataAdapter.prototype.current = function (callback) {
                  console.log("current");
                };
      
                CustomDataAdapter.prototype.query = function (params, callback) {
                  console.log("query");
                };
      
                return CustomDataAdapter;
      
              });
            console.log("after");
      
      
            var customAdapter = $.fn.select2.amd.require('select2/data/customAdapter');
      
      
            $('#dropdownbox').select2({
              dataAdapter: customAdapter
            });
      
          });
        </script>
      </body>
      </html>