具有多个mysql表的动态可扩展数据表?

时间:2018-02-14 14:34:31

标签: php codeigniter join dynamic datatables

我正在研究动态j查询数据表,我希望显示3个表中的数据。 它应该是可扩展的。我完成了1个表的实现。但是在加入另外两张桌子之后就无法正常工作了。 我附上我的表格格式和所有图片应该是我的数据外观。

我总共有4张桌子。 employee_master,uk_slip,germany_slip,poland_slip。 每个用户每个月都有自己的pay_slips。 例如。 XYZ是员工。 他将获得他的3个salary_slips 2018-FEBRUARY。我想显示一个数据表,第一个用户将看到他的名字然后在扩展数据表他将得到以下格式他的所有单据。 enter image description here
我当前的代码是仅显示来自uk_slip的数据。在UK之后,应显示另一个表数据。

我的模特:

public function get_all_payslips()
    {
        $empid = $this->session->userdata('EMPLOYEE_ID');
        $orgid = $this->session->userdata('CURRENT_ORG_ID');                    
        $where = "EMPLOYEE_ID ='".$empid."' ";
        $response = array();
        $queryString = "SELECT
                         em.EMPLOYEE_ID,
                         em.EMPLOYEE_NAME
                        FROM                                   
                        uk_slip assd,
                        employee_master em
                        WHERE
                        em.".$where."
                        GROUP BY em.EMPLOYEE_ID
                        order by em.EMPLOYEE_NAME asc";                            
        $query = $this->db->query($queryString);

        foreach ($query->result() as $data) 
        {         
            $result = array();
            $result['EMPLOYEE_NAME'] = $data-> EMPLOYEE_NAME;
            $queryString = "SELECT
                        mo.months,
                        MONTH((assd.pay_period)) as monthss,
                        YEAR((assd.pay_period)) as PAY_YEAR,
                        GROUP_CONCAT(DISTINCT(assd.id)) as action,
                        CONCAT(assd.ORG_ID,',',germany.ORG_ID,',',poland.ORG_ID) as org 
                        FROM                         
                        uk_new_salary_slip assd,
                        employee_master em,                               
                        months mo,
                        germany_slip germany,
                        poland_slip poland
                        WHERE
                        assd.emp_id = ". $data->EMPLOYEE_ID ."
                          AND mo.id =  MONTH((assd.pay_period))
                          GROUP BY monthss,PAY_YEAR
                          order by PAY_YEAR desc";

            $query1 = $this->db->query($queryString);                       
            $children = array();
            foreach ($query1->result() as $data1)
                {
                    $yearArray = array();                          
                    $yearArray['month'] = $data1->months;
                    $yearArray['year'] = $data1->PAY_YEAR;
                    $yearArray['org'] = $data1->org;                            
                    $yearArray['action'] = $data1->action;
                    array_push($children, $yearArray);
                }
                $result['children'] = $children;
                array_push($response, $result);

        }                   
        return $response;                   
    }

使用jquery数据表的我的视图:

$(document).ready(function() 
        { 
        var table = null;
        $.ajax({
          url:"<?php echo base_url('responsible/get_all_payslips'); ?>",
          datatype:'json',            
              success: function(response)
                {
                  var response1 = $.parseJSON(response);
                  console.log(JSON.stringify(response1['pay_slips']));
                  var data = response1['pay_slips'];
                  table = $('.datatables').DataTable({                 
                  columns : [
                      {
                        className      : 'details-control',
                        defaultContent : '',
                        data           : null,
                        orderable      : false
                      },
                      {data : 'EMPLOYEE_NAME'},

                    ],                  
                    data : data,                  
                    pagingType : 'full_numbers',
                  });
                }
      });

    function format(data) 
        {
            return '<div class="details-container">'+
              '<table  class="details-table table-bordered">'+
                '<thead>'+
                      '<tr>'+                  
                        '<th>Year</th>'+
                        '<th>Month</th>'+
                        '<th>PDF</th>'+               
                      '</tr>'+
                    '</thead>'+
                    '<tbody>'+
                      '<tr>'+
                      '<td>'+data.PAY_YEAR+'</td>'+
                      '<td>'+data.monthss+'</td>'+
                      '<td>'+'<a class="btn btn-success" href="data.action">'+data.action+'</a>'+'</td>'+
                      '</tr>'+
                      '<tr>'+
                      '<td>'+data.PAY_YEAR+'</td>'+
                      '<td>'+data.monthss+'</td>'+
                      '<td>'+'<a class="btn btn-success" href="data.action">'+data.action+'</a>'+'</td>'+
                      '</tr>'+
                    +'</tbody>'+
                  '</table>'+
                  '</div>';    
        };
        $('.datatables tbody').on('click', 'td.details-control', function () 
            {
                var tr = $(this).closest('tr');
                var row = table.row( tr );
                console.log(row.data()['children']);
                var childrenArray = row.data()['children'];
                var check = '<div class="details-container">'+
                '<table  class="details-table table-bordered">'+
                  '<thead>'+
                        '<tr>'+                  
                          '<th>Year</th>'+
                          '<th>Month</th>'+
                          '<th>PDF</th>'+               
                        '</tr>'+
                      '</thead>'+
                      '<tbody>';
                      for(var i=0;i<childrenArray.length;i++)
                        {
                            check +='<tr>'+
                            '<td>'+childrenArray[i].year+'</td>'+
                            '<td>'+childrenArray[i].month+'</td>';
                            var orgid = childrenArray[i].org.split(",");
                            var action = childrenArray[i].action.split(",");
                            var obj = {};                    
                            for (var k = 0; k < orgid.length; k++) 
                            {
                                obj[orgid[k]] = action[k];
                            }
                            var arrayData = sortObject(action);
                            check +='<td>';     

                            for(var j=0;j<arrayData.length;j++)
                                {
                                    var country = "";
                                    var color = "";
                                    if(arrayData[j].key=="40")
                                        {
                                           country = "1";
                                           color = "INDIANRED";
                                        }
                                    else if(arrayData[j].key=="41")
                                        {
                                           country = "2";
                                           color = "LIGHTPINK";
                                        }
                                    else  if(arrayData[j].key=="47")
                                        {
                                           country = "3";
                                           color = "LIGHTSALMON";
                                        }
                                    else
                                        {
                                           country = "UK";
                                           color = "DARKKHAKI";
                                        }
                                       check+='<a class="btn btn-success"  style="background-color : ' + color  + ';" href="<?php echo base_url()?>responsible/uk_pdf/'+arrayData[j].value+'">'+country+
                                                        '</a>|';
                                }
                              check +='</td></tr>';

                        }


                    check += '</tbody>'+
                            '</table>'+
                            '</div>';



                    if (row.child.isShown()) 
                        {
                           tr.next('tr').removeClass('details-row');
                           row.child.hide();
                           tr.removeClass('shown');
                        }
                    else 
                        {
                           row.child(check).show();
                           tr.next('tr').addClass('details-row');
                           tr.addClass('shown');
                        }
            });

    });

    function sortObject(obj) 
        {
            var arr = [];
            console.log(arr);
            for (var prop in obj) 
                {
                    if (obj.hasOwnProperty(prop)) 
                        {
                            arr.push({
                                'key': prop,
                                'value': obj[prop]
                            });
                        }
                }
            arr.sort(function(a, b) { return a.value - b.value; });

            return arr;
        }

在所有3个slip_tables emp_id中很常见。然后在每个具有ORG_ID列的表中。对于具有ORG_ID = 40的英国,对于德国= 57,对于POLAND = 47.在uk_slip中具有列pay_period以获得具有mysql函数MONTH and YEARMONTH的{​​{1}}。在德国和波兰,YEAR得到同样的东西。

0 个答案:

没有答案