この例では、データベースからデータを取得し、データ グリッド (datagrid) に表示する方法を示します。次に、ユーザーが入力した検索キーワードに基づいて結果を検索して表示する方法を示します。
データ グリッド (DataGrid) を作成する
ページネーション付きのデータ グリッド (データ グリッド) を作成し、それにツールバーを追加します。
<table id="tt" class="easyui-datagrid" style="width:600px;height:250px" url="datagrid24_getdata.php" ツールバー="#tb" title="データの読み込み" iconCls="icon-save" rownumbers="true" pagination="true"> <スレッド> <tr> <th field="itemid" width="80">アイテムID</th> <th field="productid" width="80">製品 ID</th> <th field="listprice" width="80" align="right">定価</th> <th field="unitcost" width="80" align="right">単価</th> <th field="attr1" width="150">属性</th> <th field="status" width="60" align="center">ステータス</th> </tr> </スレッド> </表>
ツールバーは次のように定義されます。
<div id="tb" style="padding:3px"> <span>アイテム ID:</span> <input id="itemid" style="line-height:26px;border:1px solid #ccc"> <span>製品 ID:</span> <input id="productid" style="line-height:26px;border:1px solid #ccc"> <a href="#" class="easyui-linkbutton" plain="true" οclick="doSearch()">検索</a> </div>
ユーザーがクエリ値を入力してクエリ ボタンを押すと、「doSearch」関数が呼び出されます。
関数 doSearch(){ $('#tt').datagrid('load',{ itemid: $('#itemid').val(), 商品ID: $('#productid').val() }); }
上記のコードは、「load」メソッドを呼び出して、新しいデータ グリッド (datagrid) データをロードします。「itemid」および「productid」パラメーターをサーバーに渡す必要があります。
サーバー側コード
「conn.php」を含めます。 $page = isset($_POST['page']) ? intval($_POST['ページ']) : 1; $rows = isset($_POST['rows']) ? intval($_POST['行']): 10; $itemid = isset($_POST['itemid']) ? mysql_real_escape_string($_POST['itemid']) : ''; $productid = isset($_POST['productid']) ? mysql_real_escape_string($_POST['productid']) : ''; $offset = ($page-1)*$rows; $result = 配列(); $where = "itemid は '$itemid%' のように、productid は '$productid%' のように"; $rs = mysql_query("select count(*) from item where " . $where); $row = mysql_fetch_row($rs); $result["total"] = $row[0]; $rs = mysql_query("select * from item where " . $where . " limit $offset,$rows"); $items = 配列(); while($row = mysql_fetch_object($rs)){ array_push($items, $row); } $result["rows"] = $items; echo json_encode($result);