5

データベースから値を取得できたので、渡したいものをさらに指定したいと思います。

以下のイベント関数に反応する選択ボックスから、値(レコードのuid)を読み取り、それをajaxActionに渡します。

    
    var uid;
    $('#mySelectBox').change(function() {
        arguments = $(this).attr('value');
        var uri = '<f:uri.action arguments="{uid: '+uid+'}" action="ajax" controller="Mycontroller1" pageType="89657201" />';

        jQuery.getJSON(uri, function(result) {
            // do something
        });
    });
    

私はそれを議論で試しましたが、それが正しい方法かどうかはわかりません。さらに、Marcus Biesioroffが提案したように、JSを別のファイルに保存する必要がありますが、Fluidの方法ではなく、自分でURIを作成する必要があります。

私のajaxActionは次のようになります。

    
        public function ajaxAction($uid) {
            $dataFromRepo = $this->myRepository->findByUid($uid);

            $resultArray = array(
                "field1" => $dataFromRepo->getField1(),
                "field2" => $dataFromRepo->getField2(),
                "field3" => $dataFromRepo->getField3(),
                "field4" => $dataFromRepo->getField4(),
            );
            return json_encode($resultArray);
        }
    

uidが正しく渡されていないことは確かですが、他のすべては機能します。

4

1 に答える 1

6

いくつかの間違いがあります:

  • ビューに配置されている場合でも、JSでvievhelperの構文を破ることはできません。アクションのフルパスをから渡す必要があります。<f:uri.action />
  • このJSには中かっこが含まれているため、このJSを表示することはできません。他にも問題の説明があります。
  • 外部ファイルからajax関数を呼び出し、アクションのリンクとuidを別々に渡してから、

ビューで:

<script type="text/javascript">
    var actionsPathFromViewHelperSetInTheView 
        = '<f:uri.action action="ajax" controller="Mycontroller1" pageType="89657201" />';
</script>
<script type="text/javascript" src="path/to/ext/Public/yourExternal.js"></script>


<!-- of course this field may/should be created with Fluid's viewhelper -->
<select id="mySelectBox" onchange="performAjaxCall(this)">
    <option value="1">Item 1</option>
    <option value="2">Item 2</option>
    <option value="3">Item 3</option>
</select>

(もちろん、プレフィックスを自分のものyourExternal.jsに変更する必要があります)tx_yourextkey_yourplugin

function performAjaxCall(selectFieldObj) {
    $.ajax({
        url: actionsPathFromViewHelperSetInTheView,
        data:{
            "tx_yourextkey_yourplugin[uid]":selectFieldObj.value
        },
        success:function (data) {
            // do something with your json
            alert('Load was performed.');
        }
    });
}

コントローラ内:

public function ajaxAction() {

    // try to always validate the incoming arguments
    if (!$this->request->hasArgument('uid') || intval($this->request->getArgument('uid')) == 0) {
        header('HTTP/1.1 400 Bad Request');
        return json_encode(array('error'=> 'Bad request'));
    }

    $uid = intval($this->request->getArgument('uid'));

    $dataFromRepo = $this->myRepository->findByUid($uid);
    if ($dataFromRepo == null) {
        header('HTTP/1.1 404 Not found');
        return json_encode(
           array('error'=> 'Not found or you have no access or something else... happens...')
        );
    }
    $resultArray = array(
        "field1" => $dataFromRepo->getField1(),
        // etc...
    );

    return json_encode($resultArray);
}
于 2012-05-24T14:26:57.953 に答える