【问题标题】:Framework7 Single Searchbar for multiple Virtual Lists用于多个虚拟列表的 Framework7 单个搜索栏
【发布时间】:2018-02-16 01:34:49
【问题描述】:

任务

描绘典型的搜索栏和结果列表。使用我的 Cordova 应用程序中的 framework7,我们当然可以轻松地实现这一点,即使使用虚拟列表中的元素并未全部呈现。但是我要实现的是一个搜索栏,它将影响两个虚拟列表。这样做的原因是作为我正在创建的应用程序的一部分,将有一个新设备和翻新设备的列表。每个列表都位于同一页面上的不同滑块选项卡上,因此任何时候都只会看到一个列表。但我需要使用单个搜索栏来过滤两者。

到目前为止我做了什么

当前设置了两个单独的虚拟列表的滑块,启动了搜索栏。有没有什么好方法可以让搜索栏应用于两个列表,而不会因为 customSearch true 参数而发疯。如果这是唯一的选择,我将如何保留过滤列表的默认方法,我只想应用两次。

我还考虑过使用 display:none 创建第二个搜索栏,并让它从看到的搜索栏复制输入。然后隐藏的搜索栏可以应用于一个列表,而看到的搜索栏将应用于另一个。但这真的很老套,一点也不整洁。

对不起,如果这有点不清楚,我不确定如何最好地应对挑战,感谢您的帮助

【问题讨论】:

    标签: javascript list cordova search html-framework-7


    【解决方案1】:

    我初始化了我的两个虚拟列表,我使搜索功能可以从外部访问。然后我用 customSearch true 初始化我的搜索栏,在搜索时我使用我的虚拟列表 (vl) 数组并使用可用的过滤器和查询功能单独搜索它们。这有点痛苦,但这工作得很好。这个例子中的 vl[1] 只是 vl[0] 的一个副本,因为我还没有真正设置它。

        function virtualSearchAll(query,items){//search query
            var foundItems = [];
            for (var i = 0; i < items.length; i++) {
                // Check if title contains query string
                if (items[i].internal_descriptn.toLowerCase().indexOf(query.toLowerCase().trim()) >= 0) foundItems.push(i);
            }
            // Return array with indexes of matched items
            return foundItems;
        }
    
        var vl = [];
        vl[0] = myApp.virtualList('.vl0', {//initialise new products
            items: selectProd,
            template: '<li data-value="{{model_id}}" class="item-content"><div class="item-inner"><div class="item-title list-title">{{internal_descriptn}}</div></div></li>',
            searchAll: function(query,items) {
                return virtualSearchAll(query, items);
            }
        });
        vl[1] = myApp.virtualList('.vl1', {//initialise test/referb products
            items: [{internal_descriptn:"test desc",model_id:"wehayy"}],
            template: '<li data-value="{{model_id}}" class="item-content"><div class="item-inner"><div class="item-title list-title">{{internal_descriptn}}</div></div></li>',
            searchAll: function(query,items) {
                return virtualSearchAll(query, items);
            }
        });
    
        var mySearchbar = myApp.searchbar('.searchbar', {
            customSearch: true,
            searchIn: '.item-title',
            onSearch: function(s) {
                previousQuery = s.query;
                for (let n in vl) {
                    let vlItems = virtualSearchAll(s.query,vl[n].items);
                    vl[n].filterItems(vlItems);
                    if(vlItems.length === 0) {//if the search has no results then show no results element else hide it
                        $('.vl'+n+'-not-found').show();
                    } else {
                        $('.vl'+n+'-not-found').hide();
                    }
                }
                highlightRows();
            }
        });
    

    我应该补充一点,highlightRows() 是需要在每次搜索时刷新的不同功能的一部分。你可以忽略它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多