Ajax使用Typo3 Extbase Controller函数调用Datatables Serverside

时间:2015-04-21 20:27:25

标签: ajax datatable typo3 extbase

我正在尝试使用针对前端插件的Typo3 Extbase Controller对Datatable Serverside进行Ajax调用,但无法获取表中的数据,而只是以下输出:

{"sEcho":0,"iTotalRecords":"6","iTotalDisplayRecords":"6","aaData":[["1","Hans","Meier","51","2011-04-13","EUR200"],["2","Frank","Heinz","45","2004-02-17","EUR60"],["3","Katrin","Kohl","35","2011-08-17","EUR1000"],["4","Werner","Pertl","39","2013-11-19","USD499"],["5","Christina","Sinn","22","2015-03-09","GBP99"],["6","Klaus","Vienna","67","1991-01-15","EUR5000"]]}

我正在使用以下视图(list.html):

<f:layout name="Default" />
<f:section name="main">

<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Field 1</th>
            <th>Field 2</th>
            <th>Field 3</th>
            <th>Field 4</th>
            <th>Field 5</th>
            <th>Field 6</th>
        </tr>
    </thead>

    <tfoot>
        <tr>
            <th>Field 1</th>
            <th>Field 2</th>
            <th>Field 3</th>
            <th>Field 4</th>
            <th>Field 5</th>
            <th>Field 6</th>
        </tr>
    </tfoot>
</table>

<script type="text/javascript" src="typo3conf/ext/ffs_datatables/Resources/Public/ajaxFunctions.js"></script>

</f:section>

我使用的js函数(ajaxFunctions.js):

var MY_AJAX_ACTION_URL = '<f:uri.action action="list" controller="DatatableController" pageType="5000" />';

$(document).ready(function() {
  $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": {
        type: 'POST',
        url: MY_AJAX_ACTION_URL         
    }
  } );
} );

..在我的控制器中:

namespace myVendor\FfsDatatables\Controller;

/**
* DatatableController
*/
class DatatableController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {

/**
 * action list
 *
 * @return void
 */
 public function listAction() {

...我在这里包含以下语法:https://datatables.net/development/server-side/php_mysql并与return交换了echo ...

return json_encode( $output );
}
/**
 * action
 *
 * @return void
 */
public function Action() {
}
}

使用的Typoscript:

ajaxCall = PAGE
ajaxCall {
  typeNum = 5000
  config {
    disableAllHeaderCode = 1
    additionalHeaders = Content-type:application/json
    xhtml_cleaning = 0
    admPanel = 0
    debug = 0
    no_cache = 1
  }
}

我错过了什么或做错了什么?

1 个答案:

答案 0 :(得分:1)

我认为你必须在TypoScript中添加一行来获取页面的输出:

ajaxCall = PAGE
ajaxCall {
  typeNum = 5000
  config {
    disableAllHeaderCode = 1
    additionalHeaders = Content-type:application/json
    xhtml_cleaning = 0
    admPanel = 0
    debug = 0
    no_cache = 1
  }
  10 < tt_content.list.20.yourextensionname_yourpluginname
}

如果没有帮助,您应该考虑不要简单地在操作中返回数据,而是使用自Extbase 6.2以来可用的JsonView。也许这可以给你进一步的帮助: Typo3 extbase json output