简体   繁体   English

Aurelia Select2,加载远程数据不加载下一页

[英]Aurelia Select2, load remote data not loading next page

I'm trying to use Select2 and load remote data (ajax) in Aurelia. 我正在尝试使用Select2并在Aurelia中加载远程数据(ajax)。 So I create a custom attribute called Select2 and follow the options in the documentation. 因此,我创建了一个名为Select2的自定义属性,并遵循文档中的选项。

But, I faced an issue when I scroll to the end of the drop down a "loading more results" message appears but doesn't load more data or call the API (using the same API in the Select2 documentation to make sure I'm not missing anything). 但是,当我滚动到下拉列表的末尾时,我遇到一个问题,即出现“正在加载更多结果”消息,但没有加载更多数据或调用API(使用Select2文档中的相同API来确保我不丢失任何内容)。

import { customAttribute, inject , bindable, bindingMode} from 'aurelia-framework';
import 'jquery';
import 'select2'; 

@customAttribute('select2')
@inject(Element)
export class Select2CustomAttribute {
    element;

    constructor(element) {
        this.element = element;
    }

    attached() {

               var self=this;

        $(this.element).select2(
            {
               // closeOnSelect: false,
                 allowClear: true,        
                ajax: {
    url: "https://api.github.com/search/repositories",
    dataType: 'json',
    delay: 250,
    data: function (params) {
      return {
        q: params.term, // search term
        page: params.page
      };
    },
    processResults: function (data, params) {
      // parse the results into the format expected by Select2
      // since we are using custom formatting functions we do not need to
      // alter the remote JSON data, except to indicate that infinite
      // scrolling can be used
      params.page = params.page || 1;

      return {
        results: data.items,
        pagination: {
          more: (params.page * 30) < data.total_count
        }
      };
    },
    cache: true
  },
                escapeMarkup: function (markup) { return markup; }, 
                minimumInputLength:0,
                templateResult: function( repo )
                            {
                                if (repo.loading) return repo.text;

      var markup = "<div class='select2-result-repository clearfix'>" +
        "<div class='select2-result-repository__avatar'><img src='" + repo.owner.avatar_url + "' /></div>" +
        "<div class='select2-result-repository__meta'>" +
          "<div class='select2-result-repository__title'>" + repo.full_name + "</div>";

      if (repo.description) {
        markup += "<div class='select2-result-repository__description'>" + repo.description + "</div>";
      }

      markup += "<div class='select2-result-repository__statistics'>" +
        "<div class='select2-result-repository__forks'><i class='fa fa-flash'></i> " + repo.forks_count + " Forks</div>" +
        "<div class='select2-result-repository__stargazers'><i class='fa fa-star'></i> " + repo.stargazers_count + " Stars</div>" +
        "<div class='select2-result-repository__watchers'><i class='fa fa-eye'></i> " + repo.watchers_count + " Watchers</div>" +
      "</div>" +
      "</div></div>";

      return markup;

                            },
                             templateSelection: function (repo) { return repo.full_name || repo.text; } ,

            }
        ).on('change', evt => {
                if (!evt.originalEvent) {
                    try{
                        this.element.dispatchEvent(new Event('change'));
                    }catch(e){
                        // IE 11+
                        try{
                            let evt = document.createEvent('HTMLEvents');
                            evt.initEvent('change', false, true);
                            this.element.dispatchEvent(evt);
                        }catch(e){
                            console.log(e);
                        }
                    }
                }
            });

}
}

I copied your code in to a gist.run and it worked fine: https://gist.run/?id=5b560eac6deff0f1b0896c30a3f12f79 我将您的代码复制到了gist.run中,并且运行良好: https ://gist.run/?id=5b560eac6deff0f1b0896c30a3f12f79

The only difference is I'm loading jQuery and select2 from a CDN and not from the bundle. 唯一的区别是我从CDN而不是捆绑包中加载jQuery和select2。 You can see a GIF of it working for me here: http://imgur.com/a/6uSs6 您可以在这里看到它对我有用的GIF: http : //imgur.com/a/6uSs6

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

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