【问题标题】:WooCommerce Select2 match US/ Canada, etc states by beginning of string not just any matchWooCommerce Select2 通过字符串开头匹配美国/加拿大等州,而不仅仅是任何匹配
【发布时间】:2019-10-23 09:43:07
【问题描述】:

WooCommerce 在州和国家/地区的送货和帐单地址字段中使用 select2。我想这样当你搜索 ka 时,最上面的结果是 Kansas 而不是 Alaska

我查看了 select2 的文档,发现:

https://select2.org/searching#customizing-how-results-are-matched

但该解决方案适用于有孩子的情况。我不知道如何编辑记录的示例以使其与 WooCommerce 一起使用。另一篇 SO 文章建议这样做:

function matchCustom(term, text) {
  console.log('matcher is running');
  if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
    return true;
  }
}
jQuery( document ).ready(function() {

  $(".state_select").select2({
    matcher: matchCustom
  });
});

使用上面的代码,当我搜索ka 时,顶部结果是Alaska 而不是Kansas

我查看了以下 SO 问题:

jquery select2 plugin how to match only beginning of word

jquery select2 plugin how to get only the result 'myString%'

select2 search - match only words that start with search term

【问题讨论】:

    标签: woocommerce jquery-select2 dropdown matcher


    【解决方案1】:

    你可以这样做:

    1. 脚本文件(我将其命名为my-wc-country-select.js):

      jQuery( function( $ ){
          if ( ! $().selectWoo ) {
              return;
          }
      
          // Based on <https://select2.org/searching#customizing-how-results-are-matched>
          function matchCustom(params, data) {
              // If there are no search terms, return all of the data
              if ($.trim(params.term) === '') {
                  return data;
              }
      
              // Do not display the item if there is no 'text' property
              if (typeof data.text === 'undefined') {
                  return null;
              }
      
              // `params.term` should be the term that is used for searching
              // `data.text` is the text that is displayed for the data object
              var s = $.trim( params.term ).toLowerCase();
              var s2 = $.trim( data.text ).toLowerCase();
              if ( s === s2.substr( 0, s.length ) ) {
                  return data;
              }
      
              // Return `null` if the term should not be displayed
              return null;
          }
      
          function country_select_select2() {
              // Apply the custom "matcher" to the country and state drop-downs.
              $( 'select.country_select:visible, select.state_select:visible' ).selectWoo( {
                  // We're just changing this option.
                  matcher: matchCustom
              } );
          }
      
          country_select_select2();
      
          $( document.body ).bind( 'country_to_state_changed', function() {
              country_select_select2();
          });
      } );
      

      Reference

    2. 在前端加载文件:(此代码将进入主题函数文件)

      wp_enqueue_script( 'my-wc-country-select', 'path/to/my-wc-country-select.js', [ 'wc-country-select' ] );
      

      确保将 wc-country-select 添加为依赖项。

    【讨论】:

    • 它有效,谢谢你,很抱歉延迟回复。这对我来说是一个待办事项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多