如何拆分数组数据并将它们放入单独的表中

时间:2014-04-08 07:25:14

标签: jquery arrays split

如何拆分数组数据并将它们放入单独的表中。

$.ajax({
    type: "POST",
    url: "rapportage/rapport_detail?idKlant=" + klant,
    dataType: 'json',
    error: function () {
        alert("Onvoldoende gegevens beschikbaar om rapportage to genereren.");
    },
    success: function (data) { // add data on the right place in the table
        $(".project-id").replaceWith("<div class='bold'>" + "Id:" + "</div>" + "<div class='data'>" + data.projectid + "</div>");
        $(".project-titel").replaceWith("<div class='bold'>" + "Titel:" + "</div>" + "<div class='data'>" + data.titel + "</div>");
        $(".project-type").replaceWith("<div class='bold'>" + "Projecttype:" + "</div>" + "<div class='data'>" + data.projecttype + "</div>");
        $(".project-leider").replaceWith("<div class='bold'>" + "Projectleider:" + "</div>" + "<div class='data'>" + data.projectleider + "</div>");
        $(".project-status").replaceWith("<div class='bold'>" + "Status:" + "</div>" + "<div class='data'>" + data.statusproject + "</div>");
        $(".project-startdatum").replaceWith("<div class='bold'>" + "Startdatum:" + "</div>" + "<div class='data'>" + data.startproject + "</div>");
        $(".project-deadline").replaceWith("<div class='bold'>" + "Deadline:" + "</div>" + "<div class='data'>" + data.deadlineproject + "</div>");
        $(".project-omzetproject").replaceWith("<div class='bold'>" + "Omzet:" + "</div>" + "<div class='data'>&#8364;" + data.omzetproject + "</div>");
        $(".project-kostenproject").replaceWith("<div class='bold'>" + "Kosten:" + "</div>" + "<div class='data'>&#8364;" + data.kostenproject + "</div>");
        $(".project-margeproject").replaceWith("<div class='bold'>" + "Marge:" + "</div>" + "<div class='data'>&#8364;" + data.margeproject + "</div>");
        $(".project-urenproject").replaceWith("<div class='bold'>" + "Uren:" + "</div>" + "<div class='data'>" + data.totaalurenproject + ":" + data.totaalminutenproject + "</div>");
        $(".project-urenkostenproject").replaceWith("<div class='bold'>" + "Totale kosten in uren:" + "</div>" + "<div class='data'>&#8364;" + data.urenkostenproject + "</div>");
        alert(JSON.stringify(data));
    }
});

rapportage.php(控制器):

function rapport_detail(){
    $this->load->model('rapportage_model');
    $idKlant = $this->input->get('idKlant');
    $data['details'] = $this->rapportage_model->details_klanten($idKlant);
    $data['opbrengstenproject'] = $this->rapportage_model->rapport_detail_opbrengsten($idKlant);
    $data['kostenproject'] = $this->rapportage_model->rapport_detail_kosten($idKlant);
    $data['urenproject'] = $this->rapportage_model->rapport_detail_uren($idKlant);
    $compact = array();

    foreach($data['details'] as $row){
    $compact['projectid'][] = $row->idProject;
        $compact['titel'][] = $row->Titel;
        $compact['projecttype'][] = $row->Type;
        $compact['projectleider'][] = $row->Voornaam;
        $compact['statusproject'][] = $row->Soort;
        $compact['startproject'][] = $row->Begindatum;
        $compact['deadlineproject'][] = $row->Deadline;
        //add omzet detail
    foreach($data['opbrengstenproject'] as $row){
    $compact['omzetproject'] = $row->Prijs;
    }
        // add kosten detail toevoegen
    foreach($data['kostenproject'] as $row){
        $compact['kostenproject'] = $row->Prijs;
    }
        // calculate margeproject detail
    $compact['margeproject'][] = $compact['omzetproject'] - $compact['kostenproject'];

        // set initial values at 0
    $compact['totaalurenproject'] = 0;
    $compact['totaalminutenproject'] = 0;
    $compact['tariefproject'] = 0;
    $compact['minutenkostenproject'] = 0;
    $compact['kostenurenproject'] = 0;  

        // add Uren detail
    foreach($data['urenproject'] as $row){
        $compact['totaalurenproject'] = $compact['totaalurenproject'] + $row->Uren; // all uren added to each other
        $compact['totaalminutenproject'] = $compact['totaalminutenproject'] + $row->Minuten; // Alle minuten added to eachother

        $compact['minutenkostenproject'] = $compact['minutenkostenproject'] + $row->Tarief / 60 * $row->Minuten; // Tarief split through 60 and multiply by minutes
        $compact['kostenurenproject'] = $compact['kostenurenproject'] + $row->Tarief * $row->Uren; // Tarief multiply aantal uren
    }
        // change Minuten detail to uren
        $convertMintoHour = gmdate("H:i", ($compact['totaalminutenproject'] * 60));
        $vari = explode(':', $convertMintoHour);

        $compact['totaalurenproject'] = $compact['totaalurenproject'] + $vari[0]; // Uren
        $compact['totaalminutenproject'] = $vari[1]; // Minuten

        $compact['urenkostenproject'] = $compact['kostenurenproject'] + $compact['minutenkostenproject'];
        $compact['urenkostenproject'] = round($compact['urenkostenproject'], 2); // round to 2 decimals
    }

    echo json_encode($compact);
    }

它给出了这个输出:

Project    Resultaat in periode 01-04-2013 tot 01-04-2014
Id:    75,6,97
Titel:    Huisstijl,OostWestRegioBest.nl,test
Projecttype:    Vormgeving,web,Drukwerk
Projectleider:    Jan Douwe,Jan Douwe,Rene
Status:    Actief,Actief,Actief
Startdatum:    09-01-2013,02-06-2013,02-06-2014
Deadline:    13-09-2013,04-12-2013,06-02-2014
Omzet:    €null
Kosten:    €null
Marge:    €0,0,0
Uren:    16:10
Totale kosten in uren:    €665.52

我希望有这样的输出:

Project    Resultaat in periode 01-04-2013 tot 01-04-2014
Id:    75
Titel:    Huisstijl
Projecttype:    Vormgeving
Projectleider:    Jan Douwe
Status:    Actief
Startdatum:    09-01-2013
Deadline:    13-09-2013
Omzet:    €null
Kosten:    €null
Marge:    €0
Uren:    16:10
Totale kosten in uren:    €665.52

  Project    Resultaat in periode 01-04-2013 tot 01-04-2014
    Id:    6
    Titel:    OostWestRegioBest.nl
    Projecttype:    web
    Projectleider:   Jan Douwe
    Status:    Actief
    Startdatum:    02-06-2013
    Deadline:    04-12-2013
    Omzet:    €null
    Kosten:    €null
    Marge:    €0
    Uren:    16:10
    Totale kosten in uren:    €665.52



Project    Resultaat in periode 01-04-2013 tot 01-04-2014

Id:    97
Titel:    test
Projecttype:   Drukwerk
Projectleider:    Rene
Status:    Actief
Startdatum:    02-06-2014
Deadline:    06-02-2014
Omzet:    €null
Kosten:    €null
Marge:    €0
Uren:    16:10
Totale kosten in uren:    €665.52

1 个答案:

答案 0 :(得分:1)

您必须拆分所有,尝试此字段的字段

$(function(){
    $.ajax({
        type: "POST",
        url: "rapportage/rapport_detail?idKlant=" + klant,
        dataType: 'json',
        error: function () {
            alert("Onvoldoende gegevens beschikbaar om rapportage to genereren.");
        },
        success: function (data) {
            // you need to split your fields which can be splitted by a , like
            var id=data.projectid.split(','),
                titel=data.titel.split(','),
                projecttype =data.projecttype.split(','),
                projectleider =data.projectleider.split(','),
                statusproject =data.statusproject.split(','),
                startproject =data.startproject.split(',');
            var str='';
            for(var i=0,len=id.length;i<len;i++){
               str+="<div class='bold'>" + "Id:" + "</div>" + "<div class='data'>" + id[i] + "</div>";
               str+="<div class='bold'>" + "Titel:" + "</div>" + "<div class='data'>" + titel[i] + "</div>";
               str+="<div class='bold'>" + "Projectleider:" + "</div>" + "<div class='data'>" + projectleider[i] + "</div>";
               str+="<div class='bold'>" + "Status:" + "</div>" + "<div class='data'>" + statusproject[i] + "</div>";
               str+="<div class='bold'>" + "Startdatum:" + "</div>" + "<div class='data'>" + startproject[i] + "</div>";
               // same for all which can be splitted by a comma(,)
            }
            $('body').append(str); // you can use your main element to append new data
        }
    });
});

我没有提供完整的代码,我认为上面的代码可以帮助您解决问题。