如何在javascript中应用动态json中的foreach循环?

时间:2017-11-13 05:59:18

标签: jquery json codeigniter-3 jtreetable

在几天之前,我从jsfiddle的代码中获得了一个静态json,并进行了一些更改以从中创建动态json。 jsfiddle from which i got code

现在我的代码正在运行。但我想要对它进行一些更改。下面是我当前的代码和图像,我使用json从mysql获取数据。

$(document).ready(function() {
    var data = [];
    $.ajax({
        url: "<?php echo base_url('responsible/get_pay_slips'); ?>",
        datatype: 'json',
        success: function(response) {
            response = $.parseJSON(response);
            x = 0;
            $.each(response, function(i, value) {

                console.log(value.PAY_YEAR, value.PAY_MONTH);

                var data1 = [];
                data[x++] = {
                    text: value.PAY_YEAR,
                    data: {},
                    children: [{
                        text: value.PAY_MONTH,
                        data: {
                            price: 0.1,
                            quantity: 20
                        }
                    }, ],
                    'state': {
                        'opened': false
                    }
                };
            });

            console.log(data);



            // load jstree
            $("div#jstree").jstree({
                plugins: ["table", "dnd", "contextmenu", "sort"],
                core: {
                    data: data
                },
                // configure tree table
                table: {
                    columns: [{
                            width: 500,
                            header: "YEAR"
                        },
                        {
                            width: 300,
                            value: "ACTION",
                            header: "ACTION",
                            format: function(v) {
                                if (v) {
                                    return 'Print | Save'
                                }
                            }
                        },

                    ],
                    resizable: true,
                    draggable: true,
                    contextmenu: true,

                }
            });
        }
    });

});

当前的数据流: Current flow of data 我真正想要的流程:

flow that i actully want

那么,我们如何在单个树中创建相同PAY_MONTH的{​​{1}}。 我只想要一个父PAY_YEAR,并且在其中只需要PAY_YEAR {。}}。

我正在使用codeigniter。

我的模特:

PAY_MONTH

我的控制器:

public function json_get_slips()

    { 
      $empid ='34';     
      $where = "where EMPLOYEE_ID='".$empid."' ";     
      $query = $this->db->query("select PAY_MONTH,PAY_YEAR from india_salary_slip_details ".$where." group by pay_month order by pay_year"); 
      return $query->result_array();
    }

我不太了解jquery,在这里我想用它创建动态数据。我没有希望。请帮助我。

1 个答案:

答案 0 :(得分:1)

使用此查询获取月份,您的查询将只显示一个月

                  SELECT CASE
                  WHEN salary_amt IS NULL THEN 0 ELSE salary_amt 
                  END as salary_amt
                  FROM
                  (
                      SELECT 1 AS MONTH
                       UNION SELECT 2 AS MONTH
                       UNION SELECT 3 AS MONTH
                       UNION SELECT 4 AS MONTH
                       UNION SELECT 5 AS MONTH
                       UNION SELECT 6 AS MONTH
                       UNION SELECT 7 AS MONTH
                       UNION SELECT 8 AS MONTH
                       UNION SELECT 9 AS MONTH
                       UNION SELECT 10 AS MONTH
                       UNION SELECT 11 AS MONTH
                       UNION SELECT 12 AS MONTH
                  ) as meses
                LEFT JOIN salaryData e ON meses.MONTH = MONTH(STR_TO_DATE(SALARY_DATE,'%d-%b-%y')) GROUP BY meses.MONTH