简体   繁体   English

将jquery ajax响应中的json表示为html的表

[英]Represent json from jquery ajax response into a html's table

I create a search form that using ajax as the action. 我创建一个使用ajax作为动作的搜索表单。 In success ajax, how can I just load some specific div without load all refresh page. 在成功的ajax中,我如何只加载某些特定的div而不加载所有刷新页面。 This is my form 这是我的表格

<?php
        $properties = array('id' => 'form1');
        echo form_open("", $properties);
        ?>

        <fieldset>
            <div class="controls" id="chekboxes">
                <label class="checkbox "><input type="checkbox" name="criteria[]" id="nomorcb"/> Nomor Request </label>
                <input type="text" class="input-xlarge focused" id="no" name="no" placeholder="Masukkan No Request..."/>

                <label class="checkbox "><input type="checkbox" name="criteria[]" id="namacb"/> Nama User </label>
                <input type="text" class="input-xlarge focused" id="nama" name="nama" placeholder="Masukkan Nama User..."/>

                <label class="checkbox "><input type="checkbox" name="criteria[]" id="departementcb" /> Departement</label>
                <div class="control-group">
                    <div class="controls">
                        <select class="input-xlarge" id="selectError" name="dep">
                            <option value="">-- Pilih departement --</option>

                            <?php
                            foreach ($dep as $data) {
                                echo "<option value='" . $data['nama_departement'] . "'>" . $data['nama_departement'] . "</option>";
                            }
                            ?>
                        </select>
                    </div>
                </div>

                <label class="checkbox "><input type="checkbox" name="criteria[]" id="rentangcb"/> Rentang waktu</label>
                <div class="controls" id="tanggal-rentang">
                    <input type="text" class="input-small datepicker" id="tanggal" value="" name="tgl_awal"><span> &nbsp; &nbsp; &nbsp;s/d </span>
                    <input type="text" class="input-small datepicker" id="tanggal2" value="" name="tgl_akhir">
                </div>
            </div>

            <div class="form-actions">
                <button type="submit" class="btn btn-primary" id="submit">Cari</button>
                <button type="reset" class="btn" id="cancel">Cancel</button>
            </div>
        </fieldset>
        <?php echo form_close(); ?> 

this is the action's code for execute the form. 这是执行表单的动作代码。

  public function search() {
    $id_request = $this->input->post('nomor');
    $nama_user = $this->input->post('nama');
    $departement = $this->input->post('departement');
    $awal = $this->input->post('tgl_awal');
    if ($awal != "") {
        $awal = $this->input->post('tgl_awal');
    } else {
        $awal = "2014-01-01";
    }
    $timestamp_awal = strtotime($awal);
    $tgl_awal = date("Y-m-d H:i:s", $timestamp_awal);

    $akhir = $this->input->post('tgl_akhir');
    if ($akhir != "") {
        $akhir = $this->input->post('tgl_akhir');
    } else {
        $akhir = "2020-01-01";
    }

    $timestamp_akhir = strtotime($akhir);
    $tgl_akhir = date("Y-m-d H:i:s", $timestamp_akhir);

    $data = $this->model_request->search($id_request, $nama_user, $departement, $tgl_awal, $tgl_akhir);
     echo json_encode($data);
}

and this is the Ajax jquery for form above : 这是上面形式的Ajax jQuery:

$('form').on('submit', function() {
   var nomor = $('#no').val();
   var nama = $('#nama').val();
   var departement = $('#selectError').val();
   var tgl_awal = $('#tanggal').val();
   var tgl_akhir = $('#tanggal2').val();

   $.ajax({
        url: '<?php echo base_url() . 'it_team/control_it/search' ?>',
        type: 'POST',
        data: {nomor: nomor,
               nama: nama,
               departement: departement,
               tgl_awal: tgl_awal,
               tgl_akhir: tgl_akhir},
               dataType: 'json',
               success: function(obj) {
                  //Please give me an idea
               }
        });
     return false;

For testing, I try seearch and thank God, it gives me a success on json like this: 为了进行测试,我尝试使用seearch并感谢上帝,这使我在json上获得了成功,如下所示:

[{"id_request":"015","nama_user":"Dzil","departement":"IT","waktu_it_terima":"2015-06-19 02:51:05"},
{"id_request":"017","nama_user":"Dzil","departement":"IT","waktu_it_terima":"2015-06-19 13:32:46"}]

My problem is, the result of search form above will be displaying into a table in same page with the form above. 我的问题是,上面搜索表单的结果将显示在与上面表单相同页面的表格中。 You know, in tbody's table will be generate the object on based the return of json. 您知道,在tbody的表中将根据json的返回生成对象。 I am newbie using json. 我是使用json的新手。 The table looked like this 桌子看起来像这样

 <div class="box-content" id="things_table2"> 
        <table class="table table-bordered table-striped table-condensed" id="table1">
            <thead>
                <tr>
                    <th>No.  </th>
                    <th>No Request</th>
                    <th>Nama user</th>
                    <th>Departement</th>                                            
                    <th>Tanggal Request</th>
                    <th>Action</th>
                </tr>
            </thead>   
            <tbody id="hasil-pencarian">
                // Result will be showing here
            </tbody>
        </table>
    </div>

Any help it so appriciated. 它的任何帮助。

There is nothing special about json. json没有什么特别的。 Treat it like any js object, ie after parsing it. 像对待任何js对象一样对待它,即在解析它之后。

Since its just an array of objects, you can iterate over it and do whatever. 由于它只是一个对象数组,因此您可以对其进行迭代并执行任何操作。

var html = '';
$.each(data, function (index, element) {
  // build your html
  html += '<tr>;'
  html += '<td>'+ index + 1 + '</td>';
  html += '<td>'+ element.id_request + '</td>';
  // same for other elements use `element.<key-name>`
  // end tr after all the td's are done
  html += '</tr>;'
});

Once you have iterated over all the elements, inject it in the dom. 遍历所有元素后,将其注入dom。

$('.DOM-ELEMENT').html(html);

Try like this (beware there are less fields in your json than in your table): 尝试这样(请注意,json中的字段少于表中的字段):

var data = [
  {
    'id_request': '015',
    'nama_user': 'Dzil',
    'departement': 'IT',
    'waktu_it_terima': '2015-06-19 02:51:05'
  },
  {
    'id_request': '017',
    'nama_user': 'Dzil',
    'departement': 'IT',
    'waktu_it_terima': '2015-06-19 13:32:46'
  }
];

var html = '';
for (var i = 0; i < data.length; i++) {
  var td="";
  for(var key in data[i]){
    td+='<td>'+data[i][key]+'</td>';

  }
  html+="<tr>"+td+"</tr>";
}

$('#hasil-pencarian').html(html);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM