<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jqGrid Grouping Example</title>
<! -- Install jQuery -->
<script language="javascript" language ="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<! -- Install jQueryUI -->
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<! -- Install jqGrid -->
<link rel="stylesheet" type="text/css" media="screen" href="http://trirand.com/blog/jqgrid/themes/ui.jqgrid.css" />
<script src="http://trirand.com/blog/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="http://trirand.com/blog/jqgrid/js/jquery.jqGrid.js" type="text/javascript"></script>
<!--<script src="lib/js/grid.grouping.js" type="text/javascript"></script>-->
<script>
// Create some data
var mydata = [
{id:"1",invdate:"2010-05-24",name:"test",note:"note",tax:"10.00",total:"2111.00"} ,
{id:"2",invdate:"2010-05-25",name:"test2",note:"note2",tax:"20.00",total:"320.00"},
{id:"3",invdate:"2007-09-01",name:"test3",note:"note3",tax:"30.00",total:"430.00"},
{id:"4",invdate:"2007-10-04",name:"test",note:"note",tax:"10.00",total:"210.00"},
{id:"5",invdate:"2007-10-05",name:"test2",note:"note2",tax:"20.00",total:"320.00"},
{id:"6",invdate:"2007-09-06",name:"test3",note:"note3",tax:"30.00",total:"430.00"},
{id:"7",invdate:"2007-10-04",name:"test",note:"note",tax:"10.00",total:"210.00"},
{id:"8",invdate:"2007-10-03",name:"test2",note:"note2",amount:"300.00",tax:"21.00",total:"320.00"},
{id:"9",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
{id:"11",invdate:"2007-10-01",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
{id:"12",invdate:"2007-10-02",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
{id:"13",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
{id:"14",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
{id:"15",invdate:"2007-10-05",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
{id:"16",invdate:"2007-09-06",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
{id:"17",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
{id:"18",invdate:"2007-10-03",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
{id:"19",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
{id:"21",invdate:"2007-10-01",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
{id:"22",invdate:"2007-10-02",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
{id:"23",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
{id:"24",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
{id:"25",invdate:"2007-10-05",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
{id:"26",invdate:"2007-09-06",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
{id:"27",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
{id:"28",invdate:"2007-10-03",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
{id:"29",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"}
];
$(function() {
// and create the list
$("#list").jqGrid({
data: mydata,
datatype: "local",
height: 'auto',
rowNum: 30,
rowList: [10,20,30],
colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'],
colModel:[
{name:'id',index:'id', width:60, sorttype:"int"},
{name:'invdate',index:'invdate', width:90, sorttype:"date", formatter:"date"},
{name:'name',index:'name', width:100, editable:true},
{name:'amount',index:'amount', width:80, align:"right",sorttype:"float", formatter:"number", editable:true},
{name:'tax',index:'tax', width:80, align:"right",sorttype:"float", editable:true},
{name:'total',index:'total', width:80,align:"right",sorttype:"float"},
{name:'note',index:'note', width:150, sortable:false}
],
pager: "#pager",
viewrecords: true,
sortname: 'name',
grouping:true,
groupingView : {
groupField : ['name']
},
caption: "Grouping Array Data"
});
});
</script>
</head>
<body>
<table id="list"></table>
<div id="pager"></div>
</body>
</html>
<script src="http://trirand.com/blog/jqgrid/js/jquery.jqGrid.js" type="text/javascript"></script>를 가지고 테스트해보세요. github에서 받은 소스는 jquery.jqGrid.js 파일이 매우 작습니다. 내부적으로 js폴더 안에 있는 파일을 불러들이는데 그 경로가 제대로 맞지 않으면 불러와지지 않을 수 있습니다. 그런데 예제의 jquery.jqGrid.js는 통합된 것이기 때문에 따로 js파일을 내부적으로 불러들일 필요가 없습니다.
<?php
echo '{"page": 1, "total": 2, "records":18, "rows":[{"id":"1","cell":["1","KD0110103","\uad6d\uac04\uc7a5 (930ml) New"]},{"id":"2","cell":["2","KD0110101","\uad6d\uac04\uc7a5 (15L)"]},{"id":"3","cell":["3","KD0110306","\uc9c4\uac04\uc7a5 \uae08S (930ml) New"]},{"id":"4","cell":["4","KD0110307","\uc9c4\uac04\uc7a5 \uae08S (1.8L) New"]},{"id":"5","cell":["5","KD0110301","\uc9c4\uac04\uc7a5 \uae08S (5L)"]},{"id":"6","cell":["6","KD0110302","\uc9c4\uac04\uc7a5 \uae08S (15L)"]},{"id":"7","cell":["7","KD0110205","\uc9c4\uac04\uc7a5 S (930ml) New"]},{"id":"8","cell":["8","KD0110206","\uc9c4\uac04\uc7a5 S (1.8L) New"]},{"id":"9","cell":["9","KD0110201","\uc9c4\uac04\uc7a5 S (5L)"]},{"id":"10","cell":["10","KD0110204","\uc9c4\uac04\uc7a5 S (15L)"]},{"id":"11","cell":["11","KD0110401","\uc9c4\uac04\uc7a5 \uae08 F3 (500ml)"]},{"id":"12","cell":["12","KD0110406","\uc9c4\uac04\uc7a5 \uae08 F3 (15L)"]},{"id":"13","cell":["13","KD0110407","\uc9c4\uac04\uc7a5 \uae08 F3 (1.8L) New"]},{"id":"14","cell":["14","KD0110405","\uc9c4\uac04\uc7a5 \uae08 F3 (930ml) New"]},{"id":"15","cell":["15","KD0110402","\uc9c4\uac04\uc7a5 \uae08 F3 (5L)"]},{"id":"16","cell":["16","KD0110501","\uc870\ub9bc\uac04\uc7a5 (1.8L)"]},{"id":"17","cell":["17","KD0110503","\uc870\ub9bc \uac04\uc7a5 (930ml) New"]},{"id":"18","cell":["18","KD0110504","\uc870\ub9bc\uac04\uc7a5 (1.8L) New"]}]}';
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jqGrid Grouping Example</title>
<! -- Install jQuery -->
<script language="javascript" language ="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<! -- Install jQueryUI -->
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<! -- Install jqGrid -->
<link rel="stylesheet" type="text/css" media="screen" href="http://trirand.com/blog/jqgrid/themes/ui.jqgrid.css" />
<script src="http://trirand.com/blog/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="http://trirand.com/blog/jqgrid/js/jquery.jqGrid.js" type="text/javascript"></script>
<!--<script src="lib/js/grid.grouping.js" type="text/javascript"></script>-->
<script>
// Create some data
// and create the list
$(document).ready(function() {
$("#list").jqGrid({
url: 'json_data.php',
datatype: "json",
//height: 'auto',
rowNum: 10,
rowList: [10,20,30],
colNames:["No","Product Code","Product Name"],
colModel:
[
{name:"No",index:"No", width:50, align:"left"},
{name:"Itemcode",index:"Itemcode", width:150, align:"left"},
{name:"Itemname",index:"Itemname", width:150, align:"left"}
],
pager: "#pager",
viewrecords: true,
sortname: 'id',
//grouping:true,
//groupingView : {
//groupField : ['name']
//},
caption:"Product List"
});
});
</script>
</head>
<body>
<table id="list"></table>
<div id="pager"></div>
</body>
</html>

var mydata = [{"id":"1","cell":["1","KD0110103","\uad6d\uac04\uc7a5 (930ml) New"]},{"id":"2","cell":["2","KD0110101","\uad6d\uac04\uc7a5 (15L)"]},{"id":"3","cell":["3","KD0110306","\uc9c4\uac04\uc7a5 \uae08S (930ml) New"]},{"id":"4","cell":["4","KD0110307","\uc9c4\uac04\uc7a5 \uae08S (1.8L) New"]},{"id":"5","cell":["5","KD0110301","\uc9c4\uac04\uc7a5 \uae08S (5L)"]},{"id":"6","cell":["6","KD0110302","\uc9c4\uac04\uc7a5 \uae08S (15L)"]},{"id":"7","cell":["7","KD0110205","\uc9c4\uac04\uc7a5 S (930ml) New"]},{"id":"8","cell":["8","KD0110206","\uc9c4\uac04\uc7a5 S (1.8L) New"]},{"id":"9","cell":["9","KD0110201","\uc9c4\uac04\uc7a5 S (5L)"]},{"id":"10","cell":["10","KD0110204","\uc9c4\uac04\uc7a5 S (15L)"]},{"id":"11","cell":["11","KD0110401","\uc9c4\uac04\uc7a5 \uae08 F3 (500ml)"]},{"id":"12","cell":["12","KD0110406","\uc9c4\uac04\uc7a5 \uae08 F3 (15L)"]},{"id":"13","cell":["13","KD0110407","\uc9c4\uac04\uc7a5 \uae08 F3 (1.8L) New"]},{"id":"14","cell":["14","KD0110405","\uc9c4\uac04\uc7a5 \uae08 F3 (930ml) New"]},{"id":"15","cell":["15","KD0110402","\uc9c4\uac04\uc7a5 \uae08 F3 (5L)"]},{"id":"16","cell":["16","KD0110501","\uc870\ub9bc\uac04\uc7a5 (1.8L)"]},{"id":"17","cell":["17","KD0110503","\uc870\ub9bc \uac04\uc7a5 (930ml) New"]},{"id":"18","cell":["18","KD0110504","\uc870\ub9bc\uac04\uc7a5 (1.8L) New"]}];
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jqGrid Grouping Example</title>
<! -- Install jQuery -->
<script language="javascript" language ="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<! -- Install jQueryUI -->
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<! -- Install jqGrid -->
<link rel="stylesheet" type="text/css" media="screen" href="http://trirand.com/blog/jqgrid/themes/ui.jqgrid.css" />
<script src="http://trirand.com/blog/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="http://trirand.com/blog/jqgrid/js/jquery.jqGrid.js" type="text/javascript"></script>
<!--<script src="lib/js/grid.grouping.js" type="text/javascript"></script>-->
<script>
// Create some data
var mydata = [
{"id":"1","Itemcode":"KD0110103","Itemname":"\uad6d\uac04\uc7a5 (930ml) New"},
{"id":"2","Itemcode":"KD0110101","Itemname":"\uad6d\uac04\uc7a5 (15L)"},
{"id":"3","Itemcode":"KD0110306","Itemname":"\uc9c4\uac04\uc7a5 \uae08S (930ml) New"},
{"id":"4","Itemcode":"KD0110307","Itemname":"\uc9c4\uac04\uc7a5 \uae08S (1.8L) New"},
{"id":"5","Itemcode":"KD0110301","Itemname":"\uc9c4\uac04\uc7a5 \uae08S (5L)"},
{"id":"6","Itemcode":"KD0110302","Itemname":"\uc9c4\uac04\uc7a5 \uae08S (15L)"},
{"id":"7","Itemcode":"KD0110205","Itemname":"\uc9c4\uac04\uc7a5 S (930ml) New"},
{"id":"8","Itemcode":"KD0110206","Itemname":"\uc9c4\uac04\uc7a5 S (1.8L) New"},
{"id":"9","Itemcode":"KD0110201","Itemname":"\uc9c4\uac04\uc7a5 S (5L)"},
{"id":"10","Itemcode":"KD0110204","Itemname":"\uc9c4\uac04\uc7a5 S (15L)"},
{"id":"11","Itemcode":"KD0110401","Itemname":"\uc9c4\uac04\uc7a5 \uae08 F3 (500ml)"},
{"id":"12","Itemcode":"KD0110406","Itemname":"\uc9c4\uac04\uc7a5 \uae08 F3 (15L)"},
{"id":"13","Itemcode":"KD0110407","Itemname":"\uc9c4\uac04\uc7a5 \uae08 F3 (1.8L) New"},
{"id":"14","Itemcode":"KD0110405","Itemname":"\uc9c4\uac04\uc7a5 \uae08 F3 (930ml) New"},
{"id":"15","Itemcode":"KD0110402","Itemname":"\uc9c4\uac04\uc7a5 \uae08 F3 (5L)"},
{"id":"16","Itemcode":"KD0110501","Itemname":"\uc870\ub9bc\uac04\uc7a5 (1.8L)"},
{"id":"17","Itemcode":"KD0110503","Itemname":"\uc870\ub9bc \uac04\uc7a5 (930ml) New"},
{"id":"18","Itemcode":"KD0110504","Itemname":"\uc870\ub9bc\uac04\uc7a5 (1.8L) New"}
];
$(document).ready(function() {
$("#list").jqGrid({
// url: 'http://192.168.10.17:9999/application/libraries/jq_grid.php', //another controller function for generating data
data: mydata,
mtype : "POST", //Ajax request type. It also could be GET
datatype: "local", //supported formats XML, JSON or Arrray
colNames:["No","Product Code","Product Name"],
colModel:
[
{name:"id", width:50, align:"left"},
{name:"Itemcode", width:150, align:"left"},
{name:"Itemname", width:150, align:"left"}
],
rowNum:10,
width: 1200,
height: 250,
rowList:[10,20,30],
pager: "#pager",
sortname: "id",
viewrecords: true,
//rownumbers: true,
//gridview: true,
caption:"Product List"
});
});
</script>
</head>
<body>
<table id="list"></table>
<div id="pager"></div>
</body>
</html>
<?php
class JqGrid extends CI_Controller {
function __construct() {
parent::__construct();
}
function index() {
$data = array();
$this->load->view('jqgrid', $data);
}
function ajax_request() {
// 데이터베이스를 만들자... 이것도 일...
$data = array(
array("No"=>"1","Itemcode"=>"KD0110103","Itemname"=>"국간장 (930ml) New"),
array("No"=>"2","Itemcode"=>"KD0110101","Itemname"=>"국간장 (15L)"),
array("No"=>"3","Itemcode"=>"KD0110306","Itemname"=>"진간장 금S (930ml) New"),
array("No"=>"4","Itemcode"=>"KD0110307","Itemname"=>"진간장 금S (1.8L) New"),
array("No"=>"5","Itemcode"=>"KD0110301","Itemname"=>"진간장 금S (5L)"),
array("No"=>"6","Itemcode"=>"KD0110302","Itemname"=>"진간장 금S (15L)"),
array("No"=>"7","Itemcode"=>"KD0110205","Itemname"=>"진간장 S (930ml) New"),
array("No"=>"8","Itemcode"=>"KD0110206","Itemname"=>"진간장 S (1.8L) New"),
array("No"=>"9","Itemcode"=>"KD0110201","Itemname"=>"진간장 S (5L)"),
array("No"=>"10","Itemcode"=>"KD0110204","Itemname"=>"진간장 S (15L)"),
array("No"=>"11","Itemcode"=>"KD0110401","Itemname"=>"진간장 금 F3 (500ml)"),
array("No"=>"12","Itemcode"=>"KD0110406","Itemname"=>"진간장 금 F3 (15L)"),
array("No"=>"13","Itemcode"=>"KD0110407","Itemname"=>"진간장 금 F3 (1.8L) New"),
array("No"=>"14","Itemcode"=>"KD0110405","Itemname"=>"진간장 금 F3 (930ml) New"),
array("No"=>"15","Itemcode"=>"KD0110402","Itemname"=>"진간장 금 F3 (5L)"),
array("No"=>"16","Itemcode"=>"KD0110501","Itemname"=>"조림간장 (1.8L)"),
array("No"=>"17","Itemcode"=>"KD0110503","Itemname"=>"조림 간장 (930ml) New"),
array("No"=>"18","Itemcode"=>"KD0110504","Itemname"=>"조림간장 (1.8L) New")
);
// view에서 전달해야 할 것... 최소 두가지. 여기서는 get방식으로 가져왔다.
// post로 가져올 이유가 없어보여서...
// 보여줄 페이지
$page = $this->input->get('page');
// 한 페이지 당 나열될 데이터 갯수
$per_page = $this->input->get('rows');
// 전체 데이터 갯수
$records = count($data);
// 한 페이지당 나열될 갯수로 전체 데이터 갯수를 나누면 전체 페이지가 나옴.
$total_pages = ceil($records / $per_page);
if ($page > $total_pages) $page=$total_pages;
// 데이터 전송 준비
$responce = new stdClass;
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $records;
$i = 0;
// 시작 시점
// 데이터베이스에서는 limit 설정을 한다고 생각하면 됨...
// LIMIT $start, $per_page 이런 식으로 하면 됨.
$start = ($page -1) * $per_page;
for($j = $start; $j < $records; $j++) {
$row = $data[$j];
$responce->rows[$i]['id'] = $row['No'];
$responce->rows[$i]['cell'] = array
(
$row['No'],
$row['Itemcode'],
$row['Itemname']
);
$i++;
}
echo json_encode($responce);
}
}
viewer: jqgrid.php
<!DOCTYPE html>
<html>
<head>
<title>jqGrid test</title>
<!-- Install jQuery -->
<script language="javascript" language ="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<!-- Install jQueryUI -->
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<!-- Install jqGrid -->
<link rel="stylesheet" type="text/css" media="screen" href="http://trirand.com/blog/jqgrid/themes/ui.jqgrid.css" />
<script src="http://trirand.com/blog/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="http://trirand.com/blog/jqgrid/js/jquery.jqGrid.js" type="text/javascript"></script>
<!--<script src="lib/js/grid.grouping.js" type="text/javascript"></script>-->
<script>
$(document).ready(function() {
$("#list").jqGrid({
url: 'http://localhost/codeigniter-test/jqgrid/ajax_request',
mtype : "GET", //Ajax request type. It also could be GET
datatype: "json", //supported formats XML, JSON or Arrray
colNames:["No","Product Code","Product Name"],
colModel:
[
{name:"No", width:50, align:"left"},
{name:"Itemcode", width:150, align:"left"},
{name:"Itemname", width:150, align:"left"}
],
rowNum:10,
width: 1200,
height: 250,
rowList:[10,20,30],
pager: "#pager",
sortname: "No",
viewrecords: true,
//rownumbers: true,
//gridview: true,
caption:"Product List"
});
});
</script>
</head>
<body>
<table id="list"></table>
<div id="pager"></div>
</body>
</html>
주의할 것이 있다면 만일 encoding이 서로 일치하지 않으면 데이터값이 null이 될 수 있습니다. 되도록 둘 다 utf-8로 작업을 하세요. 마찬가지로 파일 인코딩도 그렇구요...
넘어왔는데 jqgrid에서 못 뿌리는 건지 등등 확인 필요합니다.
게시물 이동합니다.