2012-09-09 6 views
2

몇 시간 동안 예제와 문서를보고 추가 매개 변수를 POST하는 방법을 알아 내려고 editurl에 아직까지 알지 못했습니다. Perl Catalyst를 사용하고 있습니다.editurl에 추가 매개 변수를 POST하는 방법은 무엇입니까?

컨트롤러에 모두 코드화되어 있지는 않지만 추가하고 편집하려면 POST가 필요하지만 레코드를 삭제하지는 않습니다. inv_id을 내 컨트롤러의 서버에 게시하여 레코드를 삭제해야합니다.

컨트롤러/Root.pm :

package MyFirstGrid::Controller::Root; 
use Moose; 
use namespace::autoclean; 

BEGIN {extends 'Catalyst::Controller'} 
with 'Catalyst::TraitFor::Controller::jQuery::jqGrid'; 

__PACKAGE__->config(namespace => ''); 

sub index :Path :Args(0) { 
    my ($self, $c) = @_; 

    $c->detach($c->view("TT")); 
} 

sub getdata :Local { 
    my ($self, $c) = @_; 

    my $inv_rs = $c->model('DB::Inventory')->search({}); 
    $inv_rs = $self->jqgrid_page($c, $inv_rs); 
    my @row_data; 
    while (my $inv = $inv_rs->next) { 
    my $single_row = { 
     cell => [ 
     $inv->inv_id, 
     $inv->client_id, 
     $inv->amount, 
     $inv->tax, 
     $inv->total, 
     $inv->note, 
     ], 
    }; 
    push @row_data, $single_row; 
    } 
    $c->stash->{json_data}{rows} = \@row_data; 
    $c->detach($c->view("JSON")); 
} 

sub postrow :Local { 
    my ($self, $c) = @_; 

    my $data = $c->req->params; 
    my $inv_rs = $c->model('DB::Inventory')->search({inv_id => $data->{inv_id}}); 
    $inv_rs->update({ 
    client_id => $data->{client_id}, 
    amount => $data->{amount}, 
    tax => $data->{tax}, 
    total => $data->{total}, 
    note => $data->{note}, 
    }); 
    $c->res->status(204); 
} 

sub default :Path { 
    my ($self, $c) = @_; 

    $c->response->body('Page not found'); 
    $c->response->status(404); 
} 

sub end : ActionClass('RenderView') {} 

__PACKAGE__->meta->make_immutable; 

1; 

index.tt :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>My First Grid</title> 

<link rel="stylesheet" type="text/css" media="screen" href="[% c.uri_for('/static/css/cupertino/jquery-ui-1.8.22.custom.css') %]" /> 
<link rel="stylesheet" type="text/css" media="screen" href="[% c.uri_for('/static/css/ui.jqgrid.css') %]" /> 

<style type="text/css"> 
html, body { 
    margin: 0; 
    padding: 0; 
    font-size: 75%; 
} 
</style> 

<script src="[% c.uri_for('/static/js/jquery-1.7.2.min.js') %]" type="text/javascript"></script> 
<script src="[% c.uri_for('/static/js/i18n/grid.locale-en.js') %]" type="text/javascript"></script> 
<script src="[% c.uri_for('/static/js/jquery.jqGrid.min.js') %]" type="text/javascript"></script> 

<script type="text/javascript"> 
$(function(){ 
    $("#list").jqGrid({ 
    url: "[% c.uri_for("getdata") %]", 
    datatype: 'json', 
    mtype: 'GET', 
    colNames:['Inv No', 'Client ID', 'Amount','Tax','Total','Notes'], 
    colModel :[ 
     //{name:'inv_id', index:'inv_id', editable:true, hidden:true, editrules:{edithidden:false}, hidedlg:true}, 
     {name:'inv_id', index:'inv_id', editable:true, hidden:true}, 
     {name:'client_id', index:'client_id', width:55, editable:true, editoptions:{size:10}}, 
     {name:'amount', index:'amount', width:80, align:'right', editable:true, editoptions:{size:10}}, 
     {name:'tax', index:'tax', width:80, align:'right', editable:true, editoptions:{size:10}}, 
     {name:'total', index:'total', width:80, align:'right', editable:true, editoptions:{size:10}}, 
     {name:'note', index:'note', width:150, sortable:false, editable: true, edittype:"textarea", editoptions:{rows:"2",cols:"20"}} 
    ], 
    pager: '#pager', 
    rowNum:10, 
    rowList:[10,20,30], 
    sortname: 'inv_id', 
    sortorder: 'desc', 
    viewrecords: true, 
    caption: 'My First Grid: Navigator', 
    editurl: "[% c.uri_for("postrow") %]", 
    height: 240 
    }); 

    jQuery("#list").jqGrid('navGrid','#pager', 
    {}, //options 
    {height:280,reloadAfterSubmit:false}, // edit options 
    {height:280,reloadAfterSubmit:false}, // add options 
    {reloadAfterSubmit:false}, // del options 
    {} // search options 
); 

}); 
</script> 
</head> 

<body> 
<table id="list"><tr><td/></tr></table> 
<div id="pager"></div> 
</body> 
</html> 
+0

YouTube에서 YAPC :: NA의 [Newbie로부터의 노트] (https://www.youtube.com/watch?v=PkbfJQ8NfKA "초보자 노트")를 방금 보았습니다. –

답변

2

나는 당신이 가지고있는 문제의 기원은 그리드의 작성에 있다고 가정합니다. 그리드를 채우는 동안 모든 그리드의 행 (<tr> 요소)은 id 속성을 갖습니다. 행을 편집하고 삭제하는 동안 해당 행의 id 속성 값이 항상 서버로 전송됩니다. id 속성의 값은 페이지에서 고유해야한다는 것을 아는 것이 중요합니다. inv_id의 값이 유일한 경우이 값을 id으로 직접 사용할 수 있습니다. jqGrid에 선택 사항을 알리려면 추가 격자 매개 변수로 jsonReader: {id: "inv_id"}을 추가하거나 key: true 속성을 inv_id 열의 정의에 추가하십시오.

는 펄 촉매 자신을 사용하지 않는,하지만 당신은 그리드 데이터를 입력하는 부분은 (my $single_row = { cell => [...]} 참조) cell 특성 ($single_row = {cell => [...], id => $inv->inv_id} 같은)에 추가로 id 속성을 포함해야합니다 날 것으로 보인다. inv_id이 고유하다면 을 inv_id 열의 정의에 추가하는 것이 더 좋을 것이므로 문제는 이미 해결 될 것입니다.

이미 id의 다른 값을 사용하고 당신이 정말로 두 값이 필요합니다 경우 : idinv_id는 다음 삭제 옵션의 예를 onclickSubmit 콜백 사용할 수 있습니다.

onclickSubmit: function (options, rowid) { 
    return {inv_id: $(this).jqGrid("getCell", rowid, "inv_id")}; 
} 

나는 결과로 추가 매개 변수 inv_id로 확장됩니다 삭제 작업 중에 서버에 게시 된 데이터를 다음

$("#list").jqGrid('navGrid', '#pager', {}, //options 
    {height: 280, reloadAfterSubmit: false}, // edit options 
    {height: 280, reloadAfterSubmit: false}, // add options 
    { // del options 
     reloadAfterSubmit: false, 
     onclickSubmit: function (options, rowid) { 
      return {inv_id: $(this).jqGrid("getCell", rowid, "inv_id")}; 
     } 
    } 
); 

를 사용하는 것을 의미한다.

+0

key : true 속성을 inv_id 정의에 추가하면 필요한만큼 정확하게 전달할 수 있습니다. 올렉, 너는 남자 야! – j0e

+0

@ j0e : 환영합니다! 문제가 이미 해결 된 경우 답변을 [수락] (http://meta.stackexchange.com/a/5235/147495) 할 수 있습니다. – Oleg

+0

다음 질문으로 튜토리얼을 만들었습니다. http://blogs.perl.org/users/j0e/2012/09/using-jqgrid-with-catalyst-tutorial-with-examples-for-newbies.html – j0e

관련 문제