user2775380
user2775380

Reputation: 77

multiple child rows in datatable

I have an exiting PHP/javascript application that generates a table with some rows and corresponding child rows(there can be many of them, all are retrived from DB at once), which are hidden by default. Child rows are shown after user clicks button placed in table row. It looks like this(one parent, two children): https://i.sstatic.net/Qwu5E.png

It is generated like this from php:

    for($i = 0; $i < count($shipArr); $i++)
    {           
        echo '
            <tr>
                <td><span>'.$shipArr[$i]["orderNo"].'</span></td>
            </tr>
            <tr>
                <td>
                    <table id="details">
        ;'
        for($j = 0; $j < count($shipDetailsArr[$i]); $j++)
        {
            echo '
                        <tr>
                            <td>
                                    <p>
                                        <span>Order Number: </span><span>'.$shipDetailsArr[$i][$j]["lineNo"].'</span>
                                    </p>
                            </td>
                        </tr>
            ';
        }
        echo '</table>;'
    }

Can I use somehow objects $shipArr and $shipDetailsArr populated from db to create the same effect using datatables plugin? How can I achieve this?

Thanks for any help.

Upvotes: 1

Views: 2302

Answers (2)

Mulan
Mulan

Reputation: 135197

I know this question is quite old, but I thought I'd chime in with some support considering another answer here linked to my project.

To solve this problem, I wrote htmlgen, mirrored on packagist. It makes HTML generation with PHP quite nice, if I do say so myself !

use function htmlgen\html as h;
use function htmlgen\map;

$beeData = [
  'pop' => 'yup',
  'candy' => 'sometimes',
  'flowers' => 'so much',
  'water' => 'not really',
  'sand' => 'indifferent',
  'donuts' => 'most definitely'
];

echo h('table',
  h('thead',
    h('tr',
      h('td', 'item'),
      h('td', 'do bees like it?')
    )
  ),
  h('tbody',
    map($beeData, function($value, $key) { return
      h('tr',
        h('td', $key),
        h('td', $value)
      );
    })
  )
);

Output (whitespace not included in actual output)

<table>
  <thead>
    <tr>
      <td>item</td>
      <td>do bees like it?</td>
    </tr>
  </thead>
  <tbody>
   <tr>
     <td>pop</td>
     <td>yup</td>
   </tr>
   <tr>
     <td>candy</td>
     <td>sometimes</td>
   </tr>
   <tr>
     <td>flowers</td>
     <td>so much</td>
   </tr>
   <tr>
     <td>water</td>
     <td>not really</td>
   </tr>
   <tr>
     <td>sand</td>
     <td>indifferent</td>
   </tr>
   <tr>
     <td>donuts</td>
     <td>most definitely</td>
   </tr>
 </tbody>
</table>

Upvotes: 1

S.Pols
S.Pols

Reputation: 3434

I dont't think there is a database plugin which can automatically generate a table. You should use something like a table generator. You can program it yourself in e.g. PHP.

If this is hard for you there are already classes which can do this. Two examples are:

Good luck!

Upvotes: 1

Related Questions