2016-03-03 83 views
0

我有一個內聯可編輯表(我用Tabledit來表示這個),每一行都有一個ID,這些ID應該傳遞給控制器​​動作(Yii2)以便我保存編輯數據到數據庫。下面是我的js文件我TABLEDIT代碼:傳遞並從JavaScript獲取ID到PHP控制器

file.assetID = info.response; // the ID 

for (var i = 0; i < file.length; i++) { // the table 
    if (file[i].type == "image/jpeg") { 
     var type = "photo"; 
    } else if (file[i].type == "video/mp4") { 
     var type = "video"; 
    } 

    messageHtml += '<tr id="' + file[i].assetID + '">'; 
    messageHtml += '<td style="display:none;" id="' + file[i].assetID + '">' + file[i].assetID + '</td>'; 
    messageHtml += '<td>' + file[i].name + '</td>'; 
    messageHtml += '<td>' + type + '</td>'; 
    messageHtml += '<td>' + file[i].size + " KB" + '</td>'; 
    messageHtml += '<td><input type="text" class="form-control" placeholder="Tag"></td>'; 
    messageHtml += '<td><input type="text" class="form-control" placeholder="Description"></td>'; 
    messageHtml += '</tr>'; 
} 

var urlID = "../save-inline-edit/" + file[0].assetID; // url plus the ID of the row 
$('#uploader_table').Tabledit({ 
    url: urlID, 
    columns: { 
     identifier: [0, 'id'],      
     editable: [[1, file.name]/*, [3, file.tag], [4, file.description]*/] 
    }, 
    onSuccess: function(data, textStatus, jqXHR) { 
     console.log(data); 
     console.log(textStatus); 
     console.log(jqXHR); 
    }, 
    onFail: function(jqXHR, textStatus, errorThrown) { 
     console.log(file.assetID); 
     console.log(jqXHR); 
     console.log(textStatus); 
     console.log(errorThrown); 
    } 
}); 

我期待它會指向(urlID其中save-inline-edit是我controller-- public function actionSaveInlineEdit($id){...}動作功能)指定的URL保存聯編輯後,但作爲我檢查元素(保存後),它給了我這個錯誤: enter image description here

然後我把一個console.log查看錯誤的詳細信息,我得到這個: enter image description here

"Bad Request (#400): Missing required parameters: id"

這裏是我的控制器操作:

public function actionSaveInlineEdit($id) 
{ 
    header('Content-Type: application/json'); 
    $assetModel = $this->findModel($id); 

    $input = filter_input_array(INPUT_POST); 

    if ($input['action'] === 'edit') { 
     $assetModel->title = ""; 
     $assetModel->description = ""; 
     $assetModel->save(false); 
    } else if ($input['action'] === 'delete') { 
     $assetModel->status = "deleted"; 
     $assetModel->save(false); 
    } 

    echo json_encode($input); 
    return \yii\helpers\Json::encode([ 
     'message' => 'success', 
    ]); 
} 

我真的不知道怎麼算出來。我如何將ID傳遞給控制器​​?我希望你明白這一點。如果您有任何問題,請告訴我。如果您對實施有其他想法,請讓我知道。

+0

,如果你把它當作一個'parameter'那麼我想你'url'應該是'URLID VAR = 「../save-inline-edit?」 +文件[0] .assetID;'附加'?'。 –

+0

在yii2中,特別是在這個項目中,我正在工作,網址和身份驗證信息都是在我的案例''/ save-inline-edit/[ID]' – kaynewilder

+0

之後的斜槓後面傳遞的。但你的動作名稱似乎是'actionSaveInlineEdit'和'urlID'中的'save-inline-edit'? –

回答

1

當你把id作爲file.assetID中的代碼開始和使用文件獲取ID [0] .assetID

請使用file.assetID來獲取ID的URL。

感謝

+0

你好,謝謝。是的,但我如何將它傳遞給控制器​​? – kaynewilder

+1

var urlID =「../save-inline-edit/」; 使用前額外的參數一樣 網址:URLID, 數據:{ID:file.assetID}, 這樣 –

+0

我只是想你的建議。但問題是,我在將'id'傳遞給'url'中的控制器時遇到了問題。我仍然得到同樣的錯誤。 – kaynewilder

相關問題