简体   繁体   English

Select2 在调用时使用动态 Ajax URL

[英]Select2 use a dynamic Ajax URL on call

I use the Select2 plugin (v 3.5.2) with Ajax to dynamically load elements in the list.我使用 Select2 插件 (v 3.5.2) 和 Ajax 动态加载列表中的元素。

I have an issue as between the initialization of the Select2 (where a url property is set in the ajax helper) and the time the ajax call is made, this url might need to be changed.我在 Select2 的初始化(其中在 ajax 帮助程序中设置了 url 属性)和进行 ajax 调用之间有一个问题,这个 url 可能需要更改。

So I have something like this :所以我有这样的事情:

$box.select2({
    containerCssClass: "form-control"
    minimumInputLength: 0,
    allowClear: true,
    ajax: {
       url: someUrl,
       dataType: 'json',
       quietMillis: 100,
...
}

I can't figure out how, when, where to change the ajax.url value before it launches.我不知道在启动之前如何、何时、在哪里更改ajax.url值。

The help of Select2 says: Select2的帮助说:

Select2 uses jQuery's $.ajax function to execute the remote call by default. Select2默认使用jQuery的$.ajax函数来执行远程调用。 An alternative transport function can be specified in the ajax settings, or an entirely custom implementation can be built by providing a custom query function instead of using the ajax helper.可以在ajax设置中指定替代transport函数,或者可以通过提供自定义query函数而不是使用 ajax 帮助程序来构建完全自定义的实现。

But I can't find any example on how to do it.但我找不到任何关于如何做到这一点的例子。

Thanks in advance for any help.在此先感谢您的帮助。 Much appreciated.非常感激。

I can't figure out how, when, where to change the ajax.url value before it launches.我不知道在启动之前如何、何时、在哪里更改 ajax.url 值。

The ajax.url option can be specified as a static string or a method returning one in both Select2 3.5.x and 4.0.0. ajax.url选项可以指定为静态字符串或在 Select2 3.5.x 和 4.0.0 中返回一个的方法。

$("select").select2({
  ajax: {
    url: function () {
      return UrlHelper.RemoteAPI();
    }
  }
});

This is useful for changing the base URL , for example when the URL is determined at runtime or is automatically generated in a different method.这对于更改基本 URL很有用,例如当 URL 在运行时确定或以不同的方法自动生成时。 If you need to change the query parameters , such as the one used for sending the search term, you need to override the ajax.data option.如果您需要更改查询参数,例如用于发送搜索词的参数,则需要覆盖ajax.data选项。

$("select").select2({
  ajax: {
    data: function (args) {
      // args is the search term in 3.5.x

      // args is an object containing the query parameters in 4.0.0
      // args.term is the search term in 4.0.0
      return {
        search: args.term || args;
      };
    }
  }
});

The data here will be appended as query parameters by default, and will be sent as the request body if the method type is changed from GET (the default) to anything else.默认情况下,这里的数据将作为查询参数附加,如果方法类型从GET (默认)更改为其他任何类型,则将作为请求正文发送。

Select2 uses jQuery's $.ajax function to execute the remote call by default. Select2默认使用jQuery的$.ajax函数来执行远程调用。 An alternative transport function can be specified in the ajax settings, or an entirely custom implementation can be built by providing a custom query function instead of using the ajax helper.可以在 ajax 设置中指定替代传输函数,或者可以通过提供自定义查询函数而不是使用 ajax 帮助程序来构建完全自定义的实现。

But I can't find any example on how to do it.但我找不到任何关于如何做到这一点的例子。

Select2 does allow for a different AJAX transport to be used by changing the ajax.transport option. Select2 确实允许通过更改ajax.transport选项来使用不同的 AJAX 传输。

In 3.5.2, this must be a $.ajax -compatible method, so it must be able to take an object containing the success and failure callbacks.在 3.5.2 中,这必须是$.ajax兼容的方法,因此它必须能够接受包含successfailure回调的对象。

$("select").select2({
  ajax: {
    transport: function (args) {
      // args.success is a callback
      // args.failure is a callback

      // should return an object which has an `abort` method.
      return $.ajax(args);
    }
  }
});

In 4.0.0, this must be a method which takes a params object (the same one passed to ajax.data ), a success callback, and a failure callback.在 4.0.0 中,这必须是一个接受params对象(与传递给ajax.data相同的对象)、 success回调和failure回调的方法。

$("select").select2({
  ajax: {
    transport: function (params, success, failure) {
      var $request = $.ajax(params);

      $request.then(success);
      $request.fail(failure);

      return $request;
    }
  }
});

Very Simple Javascript code to handle the same, can be used in Suitescript(Netsuite) also.处理相同的非常简单的 Javascript 代码,也可以在 Suitescript(Netsuite) 中使用。

// prepare your dynamic URL inside this method and return
function getURL() {
     return url + params;
}

// While binding the select2 with the dropdown set url to call a anonymous function which internally calls another function.
jQuery("select.itemDropDown").select2({
    placeholder: "Select an item",
    width: "200px",
    minimumInputLength: 3,
    ajax: {
        url: function() {
            return getURL()
        },
        dataType: 'json'
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM