在yii2中将动态数据追加到footable中

时间:2015-06-30 07:49:24

标签: php ajax yii2 footable

我想将数据库中的动态数据追加到footable中。但是当我在ajax的url中给出控制器时,我得到404错误。

这是我的所有帖子

<?php

use yii\helpers\Html;
use yii\grid\GridView; 

$this->title = 'Posts';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="bg-light lter b-b wrapper-md">
    <div class="row">
        <div class="col-sm-6">
            <h1 class="m-n font-thin h3"><?= Html::encode($this->title) ?></h1>
        </div>
        <div class="col-sm-5">
            <?= Html::a('Create Post', ['create'], ['class' => 'btn btn-success pull-right']) ?>
        </div>
    </div>
</div>
<div class="panel-body"> 

    <div class="tab-pane active" id="demo">
        <div class="row">
            <div class="">
                <label class="col-sm-1 control-label">Search:</label>
                <div class="col-sm-3">
                    <input class="form-control" id="filter" type="text"/>
                </div>
                <div class="col-sm-3">
                    <a href="#clear" class="clear-filter" title="clear filter">[clear]</a>
                    <span class="row-count"></span>
                </div>
            </div> 
        </div>
        <table class="table demo" data-filter="#filter" data-page-size="5">
            <thead>
                <tr>
                    <th data-toggle="true">
                        Title
                    </th>
                    <th data-hide="phone">
                        Description
                    </th>
                    <th data-hide="phone,tablet">
                        Created Date and Time
                    </th> 
                    <th>
                        Status
                    </th>
                </tr>
            </thead>
            <tbody>
            </tbody>
            <tfoot class="hide-if-no-paging">
                <tr>
                    <td colspan="5">
                        <div class="pagination pagination-centered"></div>
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {

        function create_row (item) {
            var row = $('<tr><td>' + item.title + '</td><td>' + item.date + '</td></tr>');
            return row;
        }

        $.ajax({
            url : '<?php echo BASEURL.'post/getFooTableData'?>', **Here i am getting 404 error** 
            data: {  },
            success : function(data) {
                $.each(data, function(index, item){
                    var row = create_row(item);
                    $('table tbody').append(row);
                });

                $('table').trigger('footable_initialize');
            },
            error : function(xhr, statusText, error) {
                alert("Error! Could not retrieve the data.");
            }
        });

        $('table').footable({
            bookmarkable: { enabled: true }
        }).bind({
            'footable_filtering': function (e) {
                var selected = $('.filter-status').find(':selected').text();
                if (selected && selected.length > 0) {
                    e.filter += (e.filter && e.filter.length > 0) ? ' ' + selected : selected;
                    e.clear = !e.filter;
                }
            },
            'footable_filtered': function() {
                var count = $('table.demo tbody tr:not(.footable-filtered)').length;
                $('.row-count').html(count + ' rows found');
            }
        });

        $('.clear-filter').click(function (e) {
            e.preventDefault();
            $('.filter-status').val('');
            $('table.demo').trigger('footable_clear_filter');
            $('.row-count').html('');
        });

        $('.filter-status').change(function (e) {
            e.preventDefault();
            $('table.demo').data('footable-filter').filter( $('#filter').val() );
        });
    });
</script>

这是我的帖子控制器功能:

<?php 
public function getFooTableData()
    {
        $fooTableData = new Post();
        $fooTable = $fooTableData->getFooTableData();
        // print_r($fooTable);
        // exit;
        return $fooTable;
    }
?>

帖子模型

<?php
public function getFooTableData()
    {
        $query = new Query;
        $query->select('*')
        ->from('post');
        $command = $query->createCommand();
        $posts = $command->queryAll();
        return json_encode($posts) 
    }
?>

当调用我的索引方法时,它给了我404 Not Found错误

如何获取数据变量中的数据?

2 个答案:

答案 0 :(得分:1)

嗯? “getFooTableData”不是有效的操作名称..在yii2中,它应为actionGetFooTableData

答案 1 :(得分:0)

我认为你必须使用非大写的控制器动作的符号,但用-分隔:

 url : '<?php echo BASEURL.'post/get-foo-table-data'?>',  //try this /