在 yii2 中添加动态数据

append dynamic data in footable in yii2

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

这是我的全部post视图:

<?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>

这是我的post控制器功能:

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

这是post型号:

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

调用索引方法时出现 404 Not Found 错误

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

我认为你必须使用非大写但用 -:

分隔的控制器操作符号
 url : '<?php echo BASEURL.'post/get-foo-table-data'?>',  //try this /

嗯? “getFooTableData”不是有效的动作名称。在 yii2 中它应该是 actionGetFooTableData