Как вызвать одну страницу просмотра с другой с помощью ajax - PullRequest
0 голосов
/ 06 ноября 2019

У меня есть просмотр страницы view1, где все данные из базы данных отображаются в виде данных.

Далее у меня есть другой просмотр страницы2, который является боковым меню с раскрывающимся списком для фильтрации данных, отображаемых в view1.

Ниже приведен код для отображения данных при загрузке.

    <script type="text/javascript">
    $(document).ready(function() {
     table = $('#table1').DataTable({

            "processing": true, //Feature control the processing indicator.
            "serverSide": true, //Feature control DataTables' server-side processing mode.
            "order": [], //Initial no order.


            "ajax": {
                "url": "<?php echo site_url('reportc/detailed_report_test')?>",
                "type": "POST",
                //data:{'year':year,'unit':unit,'month':month}
            },
            "columnDefs": [
            { 
                "targets": [ 0 ], //first column / numbering column
                "orderable": false, //set not orderable
            },
            ],

        });

        $('#btn-filter').click(function(){ //button filter event click
            table.ajax.reload();  //just reload table
        });
        $('#btn-reset').click(function(){ //button reset event click
            $('#form-filter')[0].reset();
            table.ajax.reload();  //just reload table
        });

    });

    </script>

Ниже приведена функция в контроллере:

function detailed_report_test()
{
    $user_id = $this->session->userdata('user_id');
    $is_admin = $this->session->userdata('is_admin');

    if($user_id == "" || $user_id == null || $is_admin == '0')
    {
        redirect(base_url('index.php/regc/index'));
    }
    else
    {
        $year = $this->input->post('year');
        if($year == NULL)
        {
            $year = (new DateTime())->format("Y");
        }
        $month = $this->input->post('month');
        if($month == NULL)
        {
            $month = (new DateTime())->format("F");
        }

        $unit_id = $this->input->post('unit_id');
        $no = $_POST['start'];
        if($_POST['length']!=-1)
        {
            $limit = "LIMIT ".$_POST['start'].",".$_POST['length'];

        }
        else
        {
            $limit="";
        }
        $getpatients_detailed_report = $this->reportm->get_patients_detailed_report_test($year,$month,$unit_id,$limit);

        $data = array();
        foreach($getpatients_detailed_report as $row)
        {

            $no++;
            $sub_array = array();
            $sub_array[] = $no;
            $sub_array[] = $row->index_no;
            $sub_array[] = $row->name;
            $sub_array[] = $row->age;
            $sub_array[] = $row->gender;


            $sub_array[] =  "<a onclick='viewdetails(\"".$row->index_no."\")' class='btn btn-primary btn-xs' style='border-radius:50%; margin-right:5px;' href='#responsive'><span class='glyphicon glyphicon-eye-open'></span></a>";

            $data[] = $sub_array;
        }
        $output = array(
            "draw"                    =>     intval($_POST["draw"]),
            "recordsTotal"          =>      $this->reportm->get_total_record_count($year,$month,$unit_id,$limit),
            "recordsFiltered"     =>     $this->reportm->get_total_filtered_count($year,$month,$unit_id,$limit),
            "data"                    =>     $data
        );
        echo json_encode($output);

    }
}

Далее идет view2:

<form name="form" id="form" method="post" action="<?php echo base_url(); ?><?php echo $action_url; ?>">
    <div class="form-group">
        <label for="form-field-select-3">
            Year
        </label>
        <select  class="form-control  search-select" type="text" id="year" name="year" value="<?php if(isset($year)) { echo $year; } ?>" title="Select year" style="width: 100%;"  >
            <option value="" >All </option>
            <?php for($k= date("Y");$k >= date("Y")-3;$k--)
            {
            ?>
            <option value="<?php echo $k; ?>" <?php if($k=="$year"){ echo 'selected="selected"';} ?> ><?php echo $k; ?></option>
            <?php
            }?>
        </select>
    </div>
    <div class="form-group">
        <label for="form-field-select-3">
            Month
        </label>
        <select class="form-control search-select" id="month" name="month" onchange="" title="Select month" value="<?php if(isset($month)) { echo $month; } ?>">
            <option value="" >All </option>
            <?php foreach($months as $month1)
            {
            ?>
            <option value="<?php echo $month1; ?>" <?php if($month1== $month){ echo 'selected="selected"';} ?> ><?php echo $month1; ?></option>
            <?php
            } ?>
        </select>
    </div>
    <div class="form-group">
        <label for="form-field-select-3">
            Unit
        </label>
        <select class="form-control search-select" id="unit_id" name="unit_id" onchange="get_districts(this.value)" title="Select unit">
            <option value="" >All </option>
            <?php foreach ($units->result() as $unit)
            {
            ?>
            <option value="<?php echo $unit->unit_id; ?>" <?php if($unit->unit_id=="$unit_id"){ echo 'selected="selected"';} ?> ><?php echo $unit->unit_name; ?></option>
            <?php
            }?>

        </select>
    </div>
</form>

как можноя вызываю detailed_report_test функцию из view2 со значениями выбранного выпадающего меню и отображаю данные в view1

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...