【问题标题】:Move the dynamic created tag to the bottom of the list in select2将动态创建的标签移动到select2中的列表底部
【发布时间】:2020-09-15 02:18:41
【问题描述】:

我在 select2 中使用创建标签选项。但是,我想将“新类别”选项移到列表底部而不是第一个。

$(document).ready(function() {
    $('.select2').select2({
        escapeMarkup: function (markup) {
        return markup;
    },
    templateResult: formatResult,
    templateSelection: formatResult,
    tags: true,
    createTag: function (params) {
        // Don't offset to create a tag if there is no @ symbol
        if (params.term.match(/[a-z]/i)) {
            // Return null to disable tag creation
            return {
                id: params.term,
                text: params.term +' <span class="new-category-text">[New Category]</span>',
                tag: true
            }
        }
        return null;
    },
    matcher: matchCustom,
    });
});

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
    if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
        var modifiedData = $.extend({}, data, true);
        // modifiedData.text += ' (matched)';

        // You can return modified objects from here
        // This includes matching the `children` how you want in nested data sets
        return modifiedData;
    }

    // Return `null` if the term should not be displayed
    return null;
}

    function formatResult(state)
    {

        if (state.text === '-- Select --') {
            return '<span class="text-danger">'+state.text+'</span>';
        }
        if (!state.id || !state.element) {
            // console.log(state);
            return state.text;
        }

        if(state.element.dataset.global === '1'){
            // console.log(state);
            return '<span>'+state.text+'</span><span class="float-right">Standard</span>';
        }else{
            return '<span>'+state.text+'</span>';
        }
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2@4.0.5/dist/css/select2.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/select2@4.0.5/dist/js/select2.min.js"></script>

<div>
  <select class="select2" style="width:200px;" name="cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </select>
</div>

还有,这里是截图

【问题讨论】:

    标签: javascript html jquery jquery-select2 jquery-select2-4


    【解决方案1】:

    您可以使用 select2 的 sorter 功能并添加一个衬里来重新排列您的阵列

    for (var x in results) results[x].text.includes('[New Category]') ? results.push(results.splice(x, 1)[0]) : 0;
    

    $(document).ready(function() {
      $('.select2').select2({
        escapeMarkup: function(markup) {
          return markup;
        },
        templateResult: formatResult,
        templateSelection: formatResult,
        tags: true,
        createTag: function(params) {
          // Don't offset to create a tag if there is no @ symbol
          if (params.term.match(/[a-z]/i)) {
            // Return null to disable tag creation
            return {
              id: params.term,
              text: params.term + ' <span class="new-category-text">[New Category]</span>',
              tag: true
            }
          }
          return null;
        },
        matcher: matchCustom,
        sorter: function(results) {
          for (var x in results) results[x].text.includes('[New Category]') ? results.push(results.splice(x, 1)[0]) : 0;
          return results;
        },
      });
    });
    
    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
      if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
        var modifiedData = $.extend({}, data, true);
        // modifiedData.text += ' (matched)';
    
        // You can return modified objects from here
        // This includes matching the `children` how you want in nested data sets
        return modifiedData;
      }
    
      // Return `null` if the term should not be displayed
      return null;
    }
    
    function formatResult(state) {
    
      if (state.text === '-- Select --') {
        return '<span class="text-danger">' + state.text + '</span>';
      }
      if (!state.id || !state.element) {
        // console.log(state);
        return state.text;
      }
    
      if (state.element.dataset.global === '1') {
        // console.log(state);
        return '<span>' + state.text + '</span><span class="float-right">Standard</span>';
      } else {
        return '<span>' + state.text + '</span>';
      }
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/select2@4.0.5/dist/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdn.jsdelivr.net/npm/select2@4.0.5/dist/js/select2.min.js"></script>
    
    <div>
      <select class="select2" style="width:200px;" name="cars">
        <option value="volvo">Volvo</option>
        <option value="saab">Saab</option>
        <option value="mercedes">Mercedes</option>
        <option value="audi">Audi</option>
      </select>
    </div>

    【讨论】:

    • 感谢您的回答。您能否建议我如何设置完整下拉列表的背景颜色,包括 select2 字段以及图像中显示的颜色。 ibb.co/bBqTG6K
    • @SarojShrestha 是的,这是一个开始:jsfiddle.net/ct0hu4vz
    • 可能你的代码没有保存,根本没有一行CSS。
    • @SarojShrestha 正在使用 jQuery 添加 css。
    • 抱歉,您似乎解释错了。实际上,我说的是那种灰色。我想为所有其他选项添加颜色,例如ibb.co/DwfmNYn,除了那个`[新类别]。请检查原始图像,我说的是灰色选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 2011-08-28
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    相关资源
    最近更新 更多