如何在Framework7中加载多个自动完成功能?

时间:2018-10-28 11:31:43

标签: javascript html-framework-7

我正在学习框架7。我的问题是: 在我看来,我必须包括更多自动完成输入,但是我无法全部加载。

我正在使用带有Ajax-Data + Typeahead的下拉列表 https://framework7.io/docs/autocomplete.html

我的html

<li class="item-content item-input">
<div class="item-inner">
    <div class="item-title item-label">Language1</div>
    <div class="item-input-wrap">
        <input id="autocomplete-dropdown-ajax-typeahead" type="text" placeholder="" name="language1">
    </div>
</div>
</li>
<li class="item-content item-input">
<div class="item-inner">
    <div class="item-title item-label">Language2</div>
    <div class="item-input-wrap">
        <input id="autocomplete-dropdown-ajax-typeahead" type="text" placeholder="" name="language2">
    </div>
</div>
</li>

<li class="item-content item-input">
<div class="item-inner">
    <div class="item-title item-label">Language3</div>
    <div class="item-input-wrap">
        <input id="autocomplete-dropdown-ajax-typeahead" type="text" placeholder="" name="language3">
    </div>
</div>
</li>

我将js代码放在js / pages / languages / languages.js中,并以此方式将其包含在index.html中

<script type="text/javascript" src="js/pages/languages/languages.js"></script>

languages.js

const languages1 = () => {
 var autocompleteDropdownAjaxTypeahead = app.autocomplete.create({
inputEl: '#autocomplete-dropdown-ajax-typeahead',
openIn: 'dropdown',
preloader: true, //enable preloader
/* If we set valueProperty to "id" then input value on select will be set according to this property */
valueProperty: 'name', //object's "value" property name
textProperty: 'name', //object's "text" property name
//limit: 20, //limit to 20 results
typeahead: true,
dropdownPlaceholderText: 'Type...',
source: function (query, render) {
  var autocomplete = this;
  var results = [];
  if (query.length === 0) {
    render(results);
    return;
  }
  // Show Preloader
  autocomplete.preloaderShow();

// Do Ajax request to Autocomplete data
app.request({
  url: 'js/data/languages/languages1.json',
  method: 'GET',
  dataType: 'json',
  //send "query" to server. Useful in case you generate response dynamically
  data: {
    query: query,
  },
  success: function (data) {
    // Find matched items
    for (var i = 0; i < data.length; i++) {
      if (data[i].name.toLowerCase().indexOf(query.toLowerCase()) === 0) results.push(data[i]);
    }
    // Hide Preoloader
    autocomplete.preloaderHide();
    // Render items by passing array with result items
    render(results);
  }
});
}});}

const languages2 = () => {

var autocompleteDropdownAjaxTypeahead = app.autocomplete.create({
inputEl: '#autocomplete-dropdown-ajax-typeahead',
openIn: 'dropdown',
preloader: true, //enable preloader
/* If we set valueProperty to "id" then input value on 
select will be set according to this property */
valueProperty: 'name', //object's "value" property name
textProperty: 'name', //object's "text" property name
limit: 20, //limit to 20 results
typeahead: true,
dropdownPlaceholderText: 'Type...',
source: function (query, render) {
 var autocomplete = this;
 var results = [];
if (query.length === 0) {
  render(results);
  return;
}
// Show Preloader
autocomplete.preloaderShow();

// Do Ajax request to Autocomplete data
app.request({
  url: 'js/data/languages/languages2.json',
  method: 'GET',
  dataType: 'json',
  //send "query" to server. Useful in case you generate response dynamically
  data: {
    query: query,
  },
  success: function (data) {
    // Find matched items
    for (var i = 0; i < data.length; i++) {
      if (data[i].name.toLowerCase().indexOf(query.toLowerCase()) === 0) results.push(data[i]);
    }
    // Hide Preoloader
    autocomplete.preloaderHide();
    // Render items by passing array with result items
    render(results);
  }
});
}});}

const languages3 = () => {

var autocompleteDropdownAjaxTypeahead = app.autocomplete.create({
inputEl: '#autocomplete-dropdown-ajax-typeahead',
openIn: 'dropdown',
preloader: true, //enable preloader
/* If we set valueProperty to "id" then input value 
on select will be set according to this property */
valueProperty: 'name', //object's "value" property name
textProperty: 'name', //object's "text" property name
limit: 20, //limit to 20 results
typeahead: true,
dropdownPlaceholderText: 'Type...',
source: function (query, render) {
  var autocomplete = this;
  var results = [];
  if (query.length === 0) {
    render(results);
    return;
  }
// Show Preloader
autocomplete.preloaderShow();

// Do Ajax request to Autocomplete data
app.request({
  url: 'js/data/languages/languages3.json',
  method: 'GET',
  dataType: 'json',
  //send "query" to server. Useful in case you generate response dynamically
  data: {
    query: query,
  },
  success: function (data) {
    // Find matched items
    for (var i = 0; i < data.length; i++) {
      if (data[i].name.toLowerCase().indexOf(query.toLowerCase()) === 0) results.push(data[i]);
    }
    // Hide Preoloader
    autocomplete.preloaderHide();
    // Render items by passing array with result items
    render(results);
  }
});
}});}

然后我在my-app.js中调用自动填充元素

 {
    name: '',
    path: '',
    url: '',
    routes: [

      {
        name: 'languages',
        path: 'languages/',
        url: './pages/languages/languages.html',
        on: {
            pageInit: languages1,
                      languages2,
                      languages3

            }
      }]
 },

但是它不起作用。如果我对其他两个发表评论,则只能使用其中一个。

谢谢您的帮助!

1 个答案:

答案 0 :(得分:2)

您是ID选择器上的主要问题,您有3个ID,名称为autocomplete-dropdown-ajax-typeahead,ID选择器最多只能引用一项才能如实工作,以解决此问题:

<li class="item-content item-input">
<div class="item-inner">
    <div class="item-title item-label">Language1</div>
    <div class="item-input-wrap">
        <input id="autocomplete-dropdown-ajax-typeahead-1" type="text" placeholder="" name="language1">
    </div>
</div>
</li>
<li class="item-content item-input">
<div class="item-inner">
    <div class="item-title item-label">Language2</div>
    <div class="item-input-wrap">
        <input id="autocomplete-dropdown-ajax-typeahead-2" type="text" placeholder="" name="language2">
    </div>
</div>
</li>

<li class="item-content item-input">
<div class="item-inner">
    <div class="item-title item-label">Language3</div>
    <div class="item-input-wrap">
        <input id="autocomplete-dropdown-ajax-typeahead-3" type="text" placeholder="" name="language3">
    </div>
</div>
</li>

此外,您还需要编辑language.js,以将每个El引用到您的目标ID:

const languages1 = () => {
 var autocompleteDropdownAjaxTypeahead = app.autocomplete.create({
inputEl: '#autocomplete-dropdown-ajax-typeahead-1',...

const languages2 = () => {
     var autocompleteDropdownAjaxTypeahead = app.autocomplete.create({
    inputEl: '#autocomplete-dropdown-ajax-typeahead-2',...

异步路由:

routes: [
  {
    name: 'languages',
    path: 'languages/',            
    async(routeTo, routeFrom, resolve, reject) {
      // You can put you condition to make sure all languages are fill                  then resolve url
      // For test reason you can create setInterval function thats keep             check value, when success remove interval and go to resolve
      var checkTimer = setInterval(function(){ 
        if(languages1 && languages2 && languages3){
          clearInterval(checkTimer);
          resolve({
            url: './pages/languages/languages.html',
          });
        }
      }, 500);        
    }
  }]

您可以寻找直接的示例来解决您的问题,而无需更多选择:

Online Example Code