【问题标题】:GET JSON and build HTML table on a different page (Phonegap + Framework7)获取 JSON 并在不同页面上构建 HTML 表(Phonegap + Framework7)
【发布时间】:2019-08-22 17:58:46
【问题描述】:

我正在使用 Framework7 构建一个移动应用程序,该应用程序扫描包含 JSON URL 并获取相关数据的 QR 码(使用 this Phonegap plugin)。然后使用数据生成和填充表格。这是我的代码的样子:

$$(document).on('page:init', '.page[data-name="scanner"]', function (e) { 
  $$('.page-content').find('#scanCode').on('click', function (e) { 
    cordova.plugins.barcodeScanner.scan(          
      function (result) {        
       app.request.json(result.text, function (data) {           
         var tableHtml = '';
         for(var i = 0; i < data.length; i+=1){
          tableHtml+= '<tr><td class="label-cell">Brand</td> <td class="numeric-only" id="brand">' +data[0].brand+ ' </td> </tr>';
          tableHtml+= '<tr><td class="label-cell">Model</td> <td class="numeric-only" id="model">' +data[0].model+ ' </td> </tr>';
         }          

         $$('.data-table table').html(tableHtml);         
       })  
     }
    );
  });
});

到目前为止,这很好用,只要数据表与触发扫描仪功能的按钮在同一页面上:

<div class="data-table card">        
  <table>                        
    <tbody>
    </tbody>
  </table>
</div>

但是,我需要打开另一个页面并在那里创建表格的功能。我已经尝试添加..

mainView.router.navigate('/results/')

.. 到脚本,但该函数只打开新页面并且不创建表。新页面可以看到控制台记录的JSON数据,但是不知道为什么建不了表。

【问题讨论】:

    标签: javascript phonegap html-framework-7


    【解决方案1】:

    我认为您必须通过路由将参数传递到页面(componentUrl)。

    scan_result.html

    <template>
        .......
        <div class="data-table card">
            <table>
                <tr>
                    <td class="numeric-only" id="brand">{{ $route.params.brand }} </td>
                </tr>
                <tr>                
                    <td class="numeric-only" id="model">{{ $route.params.model }} </td>
                </tr>
            </table>
        </div>
    
    </template>
    <script>
        return {
            on: {
    
                pageAfterIn: function (e, page) {
    
                    //or you can call params here
                    var result_brand = this.$route.params.brand;
                    var result_model = this.$route.params.model;
    
                }
            }
        }
    </script>
    

    routes.js

    {
        path: '/results/:brand?/:model?/',
        componentUrl: 'scan_result.html',
    },
    

    导航

    mainView.router.navigate('/results/'+data[0].brand+'/'+data[0].model+'/')
    

    参考

    https://framework7.io/docs/router-component.html#single-file-component https://framework7.io/docs/view.html#view-parameters

    【讨论】:

    • 还有很多其他方式,比如调用json后使用localStorage或者回调函数
    猜你喜欢
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多