dsepcxw181184853 2013-08-28 18:45
浏览 29
已采纳

数据表和Codeigniter

I'm using the datatables plugin to format my tables with my data.

  • I make the call to my model from my controller to gather all the data.
  • I send the data array to the view file.
  • I start a table tag in my view with the datatable class which calls the plugin to turn the regular html table into a datatables table.
  • I check to verify that there is data in the array in the view and then if there is I do a foreach loop to create the table rows. If there is not data then I echo out a string of no data found.

My issue is when there is data it works fine however when there is not data I receive Requested Unknown Parameter 1 from the data source for row 0 error message.

I've seen others that have come across this same issue before however all the other posts I have seen have explained that they have used the datatables json data while specifically working with the datatables plugin.

Any ideas on how to prevent this error message from displaying.

<table class="dynamicTable table table-striped table-bordered table-condensed">
    <thead>
        <tr>
            <th style="width: 1%;" class="uniformjs" id="checkboxth"><input type="checkbox" /></th>
            <th class="center">ID</th>
            <th>Name</th>
            <th class="center">Date</th>
            <th class="center" id="created_updated"></th>
            <th class="right" id="actions">Actions</th>
        </tr>
    </thead>
    <tbody>
        <?php
        //vardump($themes);
        if (isset($themes) && is_array($themes))
        {
            foreach ($themes AS $theme)
            {
                echo '<tr class="selectable">';
                echo '<td class="center uniformjs"><input type="checkbox" /></td>';
                echo '<td class="center">' . $theme->id . '</td>';
                echo '<td>' . $theme->name . '</td>';
                if ($theme->updated_at != '0000-00-00 00:00:00')
                {
                    echo '<td class="center" style="width: 80px;">' . date('d M Y', strtotime($theme->created_at)) . '</td>';
                    echo '<td class="center" style="width: 80px;"><span class="label label-block label-inverse">updated</span></td>';
                }
                else
                {
                    echo '<td class="center" style="width: 80px;">' . date('d M Y', strtotime($theme->created_at)) . '</td>';
                    echo '<td class="center" style="width: 80px;"><span class="label label-block label-important">created</span></td>';
                }

                echo '<td class="center" style="width: 60px;">';
                echo '<a href="' . base_url() . 'themes/edit/' . $theme->id . '" class="btn-action glyphicons pencil btn-success"><i></i></a>';
                echo '<a href="' . base_url() . 'themes/delete/' . $theme->id . '" class="btn-action glyphicons remove_2 btn-danger"><i></i></a>';
                echo '</td>';
                echo '</tr>';
            } 
        }
        else
        {
            echo '<tr>';
            echo '<td class="center" colspan="7">There are no themes.  Select Add a New Theme.</td>';
            echo '</tr>';
        }
        ?>
    </tbody>
</table>

This is the code that came with the theme.

/* DataTables */
if ($('.dynamicTable').size() > 0)
{
    $('.dynamicTable').dataTable({
        "sPaginationType": "bootstrap",
        "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
        "oLanguage": {
            "sLengthMenu": "_MENU_ records per page"
        }
    });
}
  • 写回答

1条回答 默认 最新

  • dsafgdafgdf45345 2013-08-28 19:41
    关注

    Hi Please remove else condition from your code.Then your code will look like:

    <table class="dynamicTable table table-striped table-bordered table-condensed">
        <thead>
            <tr>
                <th style="width: 1%;" class="uniformjs" id="checkboxth"><input type="checkbox" /></th>
                <th class="center">ID</th>
                <th>Name</th>
                <th class="center">Date</th>
                <th class="center" id="created_updated"></th>
                <th class="right" id="actions">Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php
            //vardump($themes);
            if (isset($themes) && is_array($themes))
            {
                foreach ($themes AS $theme)
                {
                    echo '<tr class="selectable">';
                    echo '<td class="center uniformjs"><input type="checkbox" /></td>';
                    echo '<td class="center">' . $theme->id . '</td>';
                    echo '<td>' . $theme->name . '</td>';
                    if ($theme->updated_at != '0000-00-00 00:00:00')
                    {
                        echo '<td class="center" style="width: 80px;">' . date('d M Y', strtotime($theme->created_at)) . '</td>';
                        echo '<td class="center" style="width: 80px;"><span class="label label-block label-inverse">updated</span></td>';
                    }
                    else
                    {
                        echo '<td class="center" style="width: 80px;">' . date('d M Y', strtotime($theme->created_at)) . '</td>';
                        echo '<td class="center" style="width: 80px;"><span class="label label-block label-important">created</span></td>';
                    }
    
                    echo '<td class="center" style="width: 60px;">';
                    echo '<a href="' . base_url() . 'themes/edit/' . $theme->id . '" class="btn-action glyphicons pencil btn-success"><i></i></a>';
                    echo '<a href="' . base_url() . 'themes/delete/' . $theme->id . '" class="btn-action glyphicons remove_2 btn-danger"><i></i></a>';
                    echo '</td>';
                    echo '</tr>';
                } 
            }
           /* else
            {
                echo '<tr>';
                echo '<td class="center" colspan="7">There are no themes.  Select Add a New Theme.</td>';
                echo '</tr>';
            }*/
            ?>
        </tbody>
    </table>
    

    Hope it will works.Because datatable automatically handle no data in table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用