2017-05-03 1 views
1

jQgrid를 사용 중입니다. PHP의 콘텐츠를 작성하여 JSON으로보기로 보냅니다. 아래는 제가 현재 사용하고있는 PHP 코드의 조각입니다 :코드가 백엔드에서 생성되어 jQgrid에서 JSON으로 뷰로 전송 될 때 사용자 정의 포매터를 추가하는 방법은 무엇입니까?

$colFormats[] = [ 
    'index'  => 'actions', 
    'name'  => 'actions', 
    'width'  => 70, 
    'editable' => false, 
    'formatter' => 'show_btn', 
    'sortable' => false, 
    'align'  => 'center' 
]; 

foreach ($classMedata->getFieldNames() as $key => $value) { 
    $colFormats[] = [ 
     'index' => $classMedata->getCollection().'.'.$value, 
     'name' => $value, 
     'width' => 100, 
    ]; 
} 

return $this->render('IntegrationBundle:api-logs:index.html.twig', [ 
     'colFormats' => json_encode($colFormats), 
     'colNames' => json_encode($colNames), 
]); 

는 이것은 내가보기에있는 자바 스크립트 코드 :

<script type="text/javascript"> 
    var show_btn = function (cellVal, options, rowObject) { 
     return '<input style="height:22px;" type="button" value="Show" onclick="" />'; 
    }; 

    $(function() { 
     $("#grid").jqGrid({ 
      url: '/sf/api-logs', 
      datatype: "json", 
      colNames: {{ colNames|raw }}, 
      colModel: {{ colFormats|raw }}, 
      width: 980, 
      height: 300, 
      pager: "#gridpager", 
      toppager: true, 
      hoverrows: true, 
      shrinkToFit: true, 
      autowidth: true, 
      rownumbers: true, 
      viewrecords: true, 
      rowList: [10, 20, 50, 100], 
      data: [], 
      rownumWidth: 50, 
      sortable: true, 
      jsonReader: { 
       root: 'rows', 
       page: 'page', 
       total: 'total', 
       records: 'records', 
       cell: '', 
       repeatitems: false 
      }, 
      loadComplete: function (data) { 
       if (data.records === 0) { 
        $("#load_grid").addClass("info_msg").html($("<span>", { 
         "class": "grid-empty", 
         "text": "No results were found." 
        })).delay(800).fadeIn(400); 
       } 
      } 
     }); 
    }); 
</script> 

PHP 뷰 내가 적절한 JS 코드를 볼 수 있습니다 렌더링 할 때 colNamescolModel을 위해 :

colNames: ["Actions", "ID", "Object", ...], 
colModel: [{ 
    "index": "actions", 
    "name": "actions", 
    "width": 70, 
    "editable": false, 
    "formatter": "show_btn", 
    "sortable": false, 
    "align": "center" 
}, 
{"index": "ApiLogs.id", "name": "id", "width": 100}, 
{"index": "ApiLogs.dataObject","name": "dataObject","width": 100}, ... 

그러나 대신 콜 럼 Actions에 렌더링 버튼이 표시의 나는 undefined 단어를 참조하십시오. 내 실수는 어디 있는지 모르겠습니다. 어떤 도움을 줄 수 있습니까?

나는 the docsthis post을 읽었으며 나는 올바르게하고 있다고 생각하지만 위에 설명한 문제로 인한 것이 아닙니다.

+0

을하십시오 확장있는 jqGrid에 대한 모든 질문에 ** 버전에 대한 정보를 포함하는이 작업을 수행하고 약 ** 포크 ** ([무료있는 jqGrid] (https://github.com/free-jqgrid/jqGrid), 상업용 [Guriddo jqGrid JS] (http://guriddo.net/?page_id=103334) 또는 이전 버전/이전 버전의 jqGrid (버전 4.7 이하). – Oleg

답변

1

가장 편안한 형식의 포매터는 formatter: "myFormatter"과 같은 문자열 형식입니다. 은 formatter: "integer", formatter: "date" 등입니다. 사용자 정의 포맷터를 "미리 정의 된 포맷터"로 등록하고 백엔드에 설정하십시오.

이있는 jqGrid의 ** 당신이 필요로하는 단지의

$.fn.fmatter.myFormatter = function (cellValue, options, rowData, addOrEdit) { 
    // the code of formatter (the same as the custom formatter) 
    return '<input style="height:22px;" type="button" value="Show" />'; 
}; 
$.fn.fmatter.myFormatter.unformat = function (cellValue, options, elem) { 
    // the code of unformatter, like 
    // return $(elem).text(); 
}; 
+0

감사합니다. – ReynierPM

+0

@ReynierPM : 환영합니다! – Oleg

+0

액션에 그리드의 매개 변수를 사용하려면 어떻게해야합니까? 응답에 표시되는 'ApiLogs.id'라고 말하니? 대답에 이것을 추가 할 수 있습니까? – ReynierPM

관련 문제