如何从json数组计算并在视图中显示

时间:2017-01-11 08:39:01

标签: json ajax codeigniter

需要帮助我的ajax代码才知道为什么我的$ totprice无法显示在我的视图中

public function ajax_basket($kode)public function ajax_basket($kode)
{
    $this->load->helper('url');
    $list = $this->infobasket->get_by_id3($kode);
    $totprice = 0;
    $data = array();
    $no = $_POST['start'];
    foreach ($list as $infobasket) {
        $no++;
        $row = array();
        $row[] = "<td style='width:1px; vertical-align:middle;'><center><p style='font-size:10px;'>".$no."</p><center></td>";
        $row[] = '<td ><center><p style="font-size:10px;">'.number_format("{$infobasket->qty}",2,",",".").'</p></center></td>';
        $row[] = '<td ><center><p style="font-size:10px;">'.number_format("{$infobasket->price}",2,",",".").'</p></center></td>';
        $row[] = '<td ><center><p style="font-size:10px;">'.number_format("{$infobasket->subtotal}",2,",",".").'</p></center></td>';
        $row[] = '<td ><center><p style="font-size:10px;">'.number_format("{$infobasket->grandtotal}",2,",",".").'</p></center></td>';          
        $data[] = $row;
        $totprice += $infobasket->price;}
    $output = array(
        "draw" => $_POST['draw'],
        "recordsTotal" => $this->infobasket->count_all3($kode),
        "recordsFiltered" => $this->infobasket->count_filtered3($kode),
        "data" => $data,
        "totprice" => $totprice,
    );echo json_encode($output);}

这就是我想要在我的视图中显示我在哪里输入我想要我的$ totalprice

    <table id="table_id3" class="table table-striped table-bordered" cellspacing="0" width="100%"><thead><tr><th style="width:1px; font-size:9px; vertical-align: middle;" bgcolor="#00a65a"><center><FONT COLOR="#FFFFFF">NO</FONT></th>  
              <th style="width:100px; font-size:9px; vertical-align: middle;" bgcolor="#00a65a"><center><FONT COLOR="#FFFFFF">QTY</FONT></center></th><th style="width:100px; font-size:9px; vertical-align: middle;" bgcolor="#00a65a"><center><FONT COLOR="#FFFFFF">PRICE</FONT></center></th></tr></thead><tbody></tbody><tfoot></tfoot></table> 
    <div class="row form-group">
                <label class="col-md-2 control-label"></label>  
                <div class="col-md-2">              
                </div>
                <label class="control-label col-md-1" for="totprice">TOTAL PRICE</label>  
                <div class="col-md-2">
                  <input id="totprice" name="totprice" class="form-control input-md" type="text" readonly>
                </div>
    <!-- -->    </ul>
                </div>            
              <!-- /.end box ke 3-->
    </div>
    { table3 = $('#table_id3').DataTable({ 
"serverSide": true, //Feature control DataTables' server-side processing mode.         
"searching": false, "orderable": false, "paging": false,"destroy" : true,"orderable": false,     
"processing": true, //Feature control the processing indicator.
"language": {"processing": "<span><img src='images/Preloader_3.gif'/></span>"},
"order": [], //Initial no order.
"ajax": {            
"url": "<?php echo site_url('infopasien/ajax_basket3')?>/" + kode,          
"type": "POST",
"success": function(data) {
 var res = eval(data);// OR var res = JSON.parse(data);
 $('[name="totprice"]').val(res.totprice);//OR $("#totprice").val(res.totprice);
 },}, });}

2 个答案:

答案 0 :(得分:1)

改变
$output = array(
            "draw" => $_POST['draw'],           
            "data" => $data,);   

$output = array(
            "draw" => $_POST['draw'],           
            "data" => $data,
            "totprice" => $totalprice
); 

答案 1 :(得分:1)

首先将您的javascript函数放在<script> <script>中。 在你的成功功能中尝试这个......

"success": function(data)
              {
                var res = eval(data);// OR var res = JSON.parse(data);
                $('[name="totprice"]').val(res.totprice);//OR $("#totprice").val(res.totprice);
              },