使用jqGrid制作出勤表

时间:2012-03-15 21:26:19

标签: php jquery mysql jqgrid

我想用jqGrid制作一张考勤表。我正在使用PHP和Mysql

我有两个表,一个名为MemberInfo,另一个名为Attendance。

从MemberInfo我想在网格中显示成员的名字和姓氏。然后我希望每周的每一天都有一个盒子。我希望当我向这些字段添加一些数据时,要将数据保存在考勤表中,以及如果我再次生成考勤网格,那么已填满的字段,以显示数据。

我的问题是: 如何添加更多列?如何将这些列与Attendance表连接?谢谢!

编辑:

我能够生成新列并使用cellEdit将数据添加到数据库中。使用来自2个表的数据生成网格仍然存在问题。谢谢!

我希望这很清楚!如果不是请告诉我!谢谢!

(如果有另一个PHP库可以让这更容易请告诉我) 编辑:

<?php
require_once 'jqgrid/jq-config.php';
// include the jqGrid Class
require_once "jqgrid/php/jqGrid.php";
// include the driver class
require_once "jqgrid/php/jqGridPdo.php";
// Connection to the server
$conn = new PDO("mysql:host=localhost;dbname=db;","root",NULL);
// Tell the db that we use utf-8
$conn->query("SET NAMES utf8");

// Create the jqGrid instance
$grid = new jqGridRender($conn);
// Write the SQL Query
$grid->SelectCommand = 'SELECT member_id,  first_name, last_name FROM members_info WHERE member_type !=5';
// set the ouput format to json
$grid->dataType = 'json';
$grid->table ="members_info";
$grid->setPrimaryKeyId("member_id");
// Let the grid create the model

$grid->setColModel();
// Set the url from where we obtain the data
$grid->setUrl('grid.php');
$grid->cacheCount = true;

// Set grid caption using the option caption
$today = date('Y-m-d');

                    if(isset($_POST['past_month'])){
                        $today = date('Y-m-d', strtotime($_POST['past_month']));
                    }

                    if(isset($_POST['next_month'])){
                        $today = date('Y-m-d', strtotime($_POST['next_month']));
                    }
$days = attendance_cal(date('F', strtotime($today)), date('Y', strtotime($today)));  // Gets the days for that month and that year

sort($days); //sort the days

foreach($days as $day){

    $grid->addCol(array(
"name"=>date('m-d', $day)
));

}

$grid->setGridOptions(array(
    "caption"=>"This is custom Caption",
    "rowNum"=>30000,
    "sortname"=>"member_id",
    "hoverrows"=>true,
    "width"=>1000,
    "height"=>1000,
    "cellEdit"=> true,
    "cellsubmit"=>"remote",
    "cellurl"=> "cell_dump.php",
    "rowList"=>array(10,20,50),
    "postData"=>array("grid_recs"=>776)
    ));


// Change some property of the field(s)
$grid->setColProperty("member_id", array("label"=>"ID", "width"=>60, "editable"=>false));
$grid->setColProperty("first_name", array("label"=>"First Name", "width"=>120, "editable"=>false));
$grid->setColProperty("last_name", array("label"=>"Last Name", "width"=>120, "editable"=>false));


// Enjoy
$grid->navigator = false;


// and finaly bind key navigation
// This is way if no events or parameter
//$grid->callGridMethod('#grid', 'bindKeys');
//
//in case of passing events is better this way

$bindkeys =<<<KEYS
$("#grid").jqGrid('bindKeys', {"onEnter":function( rowid ) { alert("You enter a row with id:"+rowid)} } );
KEYS;

$grid->setJSCode($bindkeys);



$grid->renderGrid('#grid','#pager',true, null, null, true,true);
$conn = null;

?>

让我更具体一点:

我的表“会员”的字段为“member_id”,“first_name”,“last_name”

“出勤”表格的字段为“attendance_id”,“member_id”,“attendance_date”,“attendance_value”

我的网格,我希望它看起来像:

|会员ID |名称| 03-15-2012 | 03-20-2012 | 03-22-2012 |

使用SelectCommand从“Members”表生成“Member Id”列和“Name”列,其他列我用addCol创建它们。我有点可以弄清楚如何通过cellEdit向数据库添加数据,但是当我加载工作表时,我不知道如何将来自数据库的数据放在网格中除了来自Members表的数据之外。我希望这更清楚!谢谢!

1 个答案:

答案 0 :(得分:1)

我假设你从未使用过jqGrid而你需要开始......

请查看此链接,它为您提供了演示代码,其中包含您需要了解的有关如何使用PHP创建网格的所有信息。

http://www.trirand.net/demophp.aspx