1

我有一个名为statecolumns的表state_idstate_name. 目前我可以添加新状态并编辑它们,但我不能删除状态。我的代码可能有什么问题?

{title:"Actions",template:'<a class="left" onclick="javascript:openEditStatePopup(this);">Edit</a>' + 
    '<a class="right" onclick="javascript:deleteState(this);">Delete</a>'
    ,width:120,sortable:false}

该片段是视图代码,当我单击链接时,它会执行以下 JavaScript:

function deleteState(element)
{
    var countryDetail = {};
    var GriddataItem = $("#state_grid").data("kendoGrid").dataItem($(element).closest("tr"));
    countryDetail.state_id =GriddataItem.state_id;
    countryDetail.state_name = GriddataItem.state_name;
    // alert(countryDetail.state_id);
    $.ajax({
        url:"<?= $this->baseUrl('admin/state/delete')?>",
        data: {state_id :  countryDetail.state_id},
        dataType: "json",
        type: "POST",
        success: function(){
            alert('success');
        },
        failure:function(){
            alert('not working');
        }
    });
}

当我在通话alert(countryDetail.state_id)前回显$.ajax时,我可以获得正确的状态 ID。

我的删除控制器是:

public function deleteAction()
{

    $state = $this->_request->_getPost('state_id');
    $stateMapper = new Application_Model_Mapper_StateMapper();
    $stateMapper->delete($state);
}

删除的模型映射器是:

public function delete(Application_Model_State $state)
{
    $data = $state->toArray();
    $adapter = $this->getDbTable()->getAdapter()->delete(array('state_id=?'=>$data['state_id']));
}
4

2 回答 2

0

在您的控制器操作中deleteAction(),您将获得 POST 参数 'state_id'

$state = $this->_request->_getPost('state_id');
$stateMapper = new Application_Model_Mapper_StateMapper();
$stateMapper->delete($state);

$state你在$stateMapper->delete($state);函数中传递它

在您的模型类函数public function delete(Application_Model_State $state)定义中,您传递的不是状态模型对象和状态 ID,因此您应该将其更改为

public function delete($state_id)
{
    $adapter = $this->getDbTable()->getAdapter()->delete(array('state_id=?'=>$state_id));

}

然后它应该工作......

另一件事我没见过

failure:function(){
      alert('not working');
}

而是

error:function(){
    alert('not working');
}
于 2012-07-20T02:52:05.427 回答
0

你好,你需要写deleteAction如下

public function deleteAction()
{


    $state = $this->_getParam('state_id');
    $stateMapper = new Application_Model_Mapper_StateMapper();
    $stateId = $stateMapper->delete($state);
    $this->_helper->json(array('success'=>1));
} 
于 2012-07-25T04:18:36.040 回答