jeudi 23 mars 2017

Looping and building a dynamic data Table

Im building a app the shows information of a survey results. In each survey i have questions, and each question have his answers.

But im having some issues on building the table, the header table and the body is build dynamacly, where the header is the questions, and the body are the answers.

Examle of my code table:

<table id="example1" class="table table-bordered table-striped">
    <thead>
    <tr>
       @foreach($survey->answers->groupBy('email')->first() as $answer)
           <th></th>

        @endforeach
    </tr>
    </thead>

    <tbody>

    @foreach($survey->answers->groupBy('email') as $answer)
        <tr>
            @foreach($answer as $a)
            <td></td>
                @endforeach
        </tr>
    @endforeach

    </tbody>

    <tfoot>

    @foreach($survey->answers->groupBy('email')->first() as $answer)
        <th></th>

    @endforeach

    </tfoot>
</table>

The only problem that im having is the syncronization between the header label questions and above that are the answers, sometimes the answer if a question is sort in a diferent order, for example is not in the correct position.

Cant figure out what im doing wrong, above i leave the tables, maybe someone can have a idea what im doing wrong.

Tables:

surveys:
- id;
- name;
questions:
- id;
- survey_id;
- label;
- internal_name;

answers:
- id;
- survey_id;
- question_id;
- answer
- email;

Question Model:

class Question extends Model
{

    public function survey()
    {
        return $this->belongsTo(Survey::class);
    }

    public function answers() {
        return $this->hasMany(Answer::class);

    }

    public function oneAnswer(){
        return $this->hasOne(Answer::class);
    }

}

Answer Model:

class Answer extends Model
{
    public function survey() {
        return $this->belongsTo(Survey::class);
    }

    public function question() {
        return $this->belongsTo(Question::class);
    }

}

Screenshot enter image description here



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2nc2EUH
via IFTTT

Aucun commentaire:

Enregistrer un commentaire