无法通过ajax在仪表板上加载数据

时间:2018-03-05 10:57:26

标签: javascript php ajax yii2 actioncontroller

我在我的仪表板中有一个名为SIM Balance的部分视图。此视图应显示发布给用户日期的SIM卡数量。

我已经设置了控制器

public function actionSimbalance()
{
    $sql = "SELECT user.`name` AS issued_to, COUNT(`sims`.`id`) AS sims_issued, sims.`operator_name` AS operator_name,
    CASE
    WHEN sims.`status` = 'Production Stored SIM' THEN 
    CAST(`sim_issueance_transaction`.`issued_at` AS DATE)
    WHEN sims.`status` = 'Testing Stored SIM' THEN 
    CAST(`sim_issueance_transaction`.`issued_at` AS DATE)
    WHEN sims.`operator_name` = 'Zong' THEN 
    CAST(`sim_issueance_transaction`.`issued_at` AS DATE)
    WHEN sims.`operator_name` = 'Mobilink' THEN 
    CAST(`sim_issueance_transaction`.`issued_at` AS DATE)
    ELSE CAST(`sims`.`created_at` AS DATE) END AS issued_date
    FROM `sims`
    INNER JOIN `sim_issueance_transaction` ON (`sims`.`id` = 
    `sim_issueance_transaction`.`sim_id`)
    INNER JOIN `user` ON (`sims`.`issued_to` = `user`.`id`)
    WHERE sims.`status` IN ('Testing Stored SIM','Production Stored SIM')
    GROUP BY user.`name`, sims.`status`, issued_date, sims.`operator_name`";


    $rows = Yii::$app->db->createCommand($sql)->queryAll();

    $output = [];
    $grandtotal = 0;

    foreach ($rows as $row) {
        $std = new \stdClass();
        $std->count = $row['sims_issued'];
        $std->issued_to = $row['issued_to'];
        $std->operator = $row['operator_name'];
        $std->issued_date = $row['issued_date'];
        $grandtotal += $std->count;
        $output[]= $std;
    }
    return $this->renderPartial('sim_progress', ['model' => $output, 'grandtotal' => $grandtotal]);
}

部分视图sim_progress位于

之下
<?php foreach ($model as $row){?>
<tr>
  <td><?=$row->issued_to?></td>
  <td><?=$row->count?></td>
  <td><?=$row->operator?></td>
  <td><?= $row->issued_date ?></td>
</tr>
 <?php } ?>
 <tr>
  <td><strong>Grand Total</strong></td>
  <td>
    <strong><?= $grandtotal ?></strong>
  </td>
  <td></td>
 </tr>

然后我设计了一个HTML sim-balance

<div class="box box-info">
<div id="af8a8d88334">
    <div class="print-header print-only">
        <center><img style="width: 100px" src="<?= 
     \yii\helpers\Url::to('@web/images/logo.png', true); ?>"/>
        </center>
        <br/>
        <hr/>
    </div>
    <div class="box-header with-border">
        <h3 class="box-title">SIM Balance</h3>
    </div>
    <div class="box-body">
        <div class="table-responsive">
            <table class="table no-margin">
                <thead>
                <tr>
                    <th>Issued To</th>
                    <th>Sims Issued</th>
                    <th>Operator Name</th>
                    <th>Issued At</th>
                </tr>
                </thead>
                <tbody id="dashboard-sim-balance">

                </tbody>
            </table>
        </div>
    </div>
</div>
<div class="box-footer clearfix">
    <a href="javascript:void(0)" onclick="$('#af8a8d88334').printThis();"
       class="btn btn-sm btn-default btn-flat pull-right">Print</a>
</div>

然后在我的主网站index视图中,我将其称为

.
.
.
  <?php if (!Yii::$app->user->isGuest && in_array(Yii::$app->user->identity->user_role,[1,6])) {
                echo $this->render('dashboard/sim-balance');
            } ?>
.
.
.
.

$url_sim_balance = Url::toRoute('/dashboard/simbalance');
.
.
.
.

在我的JS我正在创建一个ajax函数,该函数应显示详细信息。

$script = <<< JS
$(document).ready(function () {
   .
   .
   .
   .
       loadSimBalance();

   .
   .
   .
 });

 function loadSimBalance() {
 $('#dashboard-sim-balance').html('');
 $.ajax({
  url: '$url_sim_balance',
  data: data.val(), // also tried $('#dashboard-sim-balance').val()
  success:function(data) {
    $('#dashboard-sim-balance').html(data);
  },
   error: function() {

    }
  });
}
JS;
$this->registerJs($script);

但是当我运行我的项目时,我看不到详细信息,而是一个空表,如下所示

enter image description here

如何设置ajax来电以查看详细信息。

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:2)

更改

    function loadSimBalance() {
 $('#dashboard-sim-balance').html('');
 $.ajax({
  url: '$url_sim_balance',
  data: data.val(), // also tried $('#dashboard-sim-balance').val()
  success:function(data) {
    $('#dashboard-sim-balance').html(data);
  },
   error: function() {

    }
  });
}

  function loadSimBalance() {
 $('#dashboard-sim-balance').html('');
 $.ajax({
  url: '$url_sim_balance',

  success:function(data) {
    $('#dashboard-sim-balance').html(data);
  },
   error: function() {

    }
  });
}