ajax|web
1 首先对os3grid做一些小的修改:加入下面代码
function strlen(str)
{
var len;
var i;
len = 0;
for (i=0;i<str.length;i++)
{
if (str.charCodeAt(i)>255) len+=2; else len++;
}
return len;
}
然后在function grid_cell_txt_edit ( cell )里面修改以下一行,主要是应付中文
s += ' size="'+ strlen(val) + '" ';
另外,修改一些颜色
this.cols = { "hover" : "#d5f1ff",
2 工资表结构
考虑到每一个单位都有不同的帐套,所以按一对多建立帐套表
CREATE TABLE pq_unitgzxm (
unit_id char(8) NOT NULL DEFAULT '',
xmbh int NOT NULL DEFAULT 0,
xmbm varchar(3) NOT NULL PRIMARY KEY,
xmmc varchar(200) NOT NULL DEFAULT '',
lx varchar(200) NOT NULL DEFAULT '',
jsgs varchar(200) NOT NULL DEFAULT ''
)
INSERT INTO pq_unitgzxm values ('00000001',1,'F01','员工号','系统值','');
INSERT INTO pq_unitgzxm values ('00000001',2,'F02','姓名','系统值','');
INSERT INTO pq_unitgzxm values ('00000001',3,'F03','单位','系统值','');
INSERT INTO pq_unitgzxm values ('00000001',4,'F04','基本工资','录入值','');
INSERT INTO pq_unitgzxm values ('00000001',5,'F05','奖金','录入值','');
INSERT INTO pq_unitgzxm values ('00000001',6,'F06','应发工资','计算值','');
INSERT INTO pq_unitgzxm values ('00000001',7,'F07','个人所得税','计算值','');
INSERT INTO pq_unitgzxm values ('00000001',8,'F08','应扣工资','计算值','');
INSERT INTO pq_unitgzxm values ('00000001',9,'F09','实发工资','计算值','');
实际工资录入采用空间冗余的办法
CREATE TABLE pq_gzxm (
gzb_id char(8) NOT NULL DEFAULT '',
employee_id char(8) NOT NULL DEFAULT '',
xm varchar(400) NOT NULL DEFAULT '',
employeecontract_id char(8) NOT NULL DEFAULT '',
gzdata1 decimal NOT NULL DEFAULT '0',
gzdata2 decimal NOT NULL DEFAULT '0',
gzdata3 decimal NOT NULL DEFAULT '0',
gzdata4 decimal NOT NULL DEFAULT '0',
gzdata5 decimal NOT NULL DEFAULT '0',
gzdata6 decimal NOT NULL DEFAULT '0',
gzdata7 decimal NOT NULL DEFAULT '0',
gzdata8 decimal NOT NULL DEFAULT '0',
gzdata9 decimal NOT NULL DEFAULT '0',
gzdata10 decimal NOT NULL DEFAULT '0',
gzdata11 decimal NOT NULL DEFAULT '0',
gzdata12 decimal NOT NULL DEFAULT '0',
gzdata13 decimal NOT NULL DEFAULT '0',
gzdata14 decimal NOT NULL DEFAULT '0',
gzdata15 decimal NOT NULL DEFAULT '0',
gzdata16 decimal NOT NULL DEFAULT '0',
gzdata17 decimal NOT NULL DEFAULT '0',
gzdata18 decimal NOT NULL DEFAULT '0',
gzdata19 decimal NOT NULL DEFAULT '0',
gzdata20 decimal NOT NULL DEFAULT '0'
)
INSERT INTO pq_gzxm (gzb_id,employee_id,xm,employeecontract_id,gzdata1) values ('00000001','00000001','张三','00000001',0);
INSERT INTO pq_gzxm (gzb_id,employee_id,xm,employeecontract_id,gzdata1) values ('00000001','00000002','李四','00000002',0);
3 生成表单
function show_form($unit_id = '', $gzyear ='', $gzmonth='',$error = '') {
global $db, $me, $t,$_POST;
extract($_POST);
if ($unit_id && !$error) {
$rs = $db->Execute("select * from ".TBL_UNITGZXM." where unit_id = '$unit_id'");
$items = $rs->GetArray();
$t->assign('items', $items);
//$rs = $db->Execute("select e.employee_id,e.xm,ec.dwmc from ".TBL_EMPLOYEE." e,".TBL_EMPLOYEECONTRACT." ec,".TBL_UNITCONTRACT." uc where e.employee_id=
ec.employee_id and ec.unitcontract_id = uc.unitcontract_id and uc.unit_id ='$unit_id'");
//$t->assign('employees', $rs->GetArray());
$gzb_id = '';
$dwmc = '';
$gzb = $db->GetRow("select gzb_id,dwmc from ".TBL_GZB." where unit_id = '$unit_id'
and gzyear='2006' and gzmonth='04'");
$gzb_id = $gzb['gzb_id'];
$dwmc = $gzb['dwmc'];
$gz_array = array();
$rs = $db->Execute("select * from ".TBL_GZXM." where gzb_id = '$gzb_id'");
while (!$rs->EOF) {
$gzdata_array = array();
//gzdata1 从1开始
for ($i = 1, $count = count($items)-3; $i <= $count; $i++) {
array_push($gzdata_array, $rs->fields['gzdata'.$i]);
}
$gzline = array($rs->fields['employee_id'],$rs->fields['xm'],$dwmc,$gzdata_array);
array_push($gz_array, $gzline);
$rs->MoveNext();
}
} else {
$t->assign($_POST);
}
//print_r($gz_array);
$t->assign('error', $error);
$t->assign('gz_array', $gz_array);
$t->render('salary.html', PAGE_TITLE,'wrap.html',False);
}
表单模板
<script>
var request_id = 0;
// 这里将录入的数据生成一个xml文件,并使用ajax方法返回给服务器端。
function get_data ( g)
{
var objDom = new ActiveXObject("MSXML.DOMDocument"); //define a DOM object
objDom.async=false;
var objRoot = objDom.createElement("SALARY"); //create the root
objDom.appendChild(objRoot);
var l = g.length ();
var t;
var data, attrs;
var s = "";
var i, v;
for ( t = 0; t < l; t ++ )
{
var objField = objDom.createElement("Item");
objDom.documentElement.appendChild(objField);
curnode=objDom.documentElement.lastChild;
data = g.get_row ( t );
//alert(data[0]+data[1]+data[2]);
//attrs = g.get_row_attrs ( t );
for ( i = 3; i < data.length; i ++ ) {
var gzdata = objDom.createElement("gzdata"+(i-2));
gzdata.text=data [ i ];
curnode.appendChild(gzdata);
}
}
//alert(objDom.xml);
post_data = "op=savesalary&salarydata=" + escape(objDom.xml);
var returnstuff = http_fetch_sync('salary.php',post_data);
//alert(returnstuff['responseText']);
if(returnstuff['responseText'].indexOf("ok")!=-1)
{
alert("保存成功!");
}
}
function row_modified ( grid, cell_pos, row_num, new_val )
{
var attrs = grid.get_row_attrs ( row_num );
attrs [ 'changed' ] = 'YES';
}
// Create an OS3Grid instance
var g = new OS3Grid ();
// Set the callback for modifications
g.onchange = row_modified;
g.set_size ( "100%", "250px" );
// Grid Headers are the grid column names
g.set_headers ( <?php
for ($i = 0, $count = count($items); $i < $count; $i++) {
echo "'".$items[$i]['xmmc']."'";
if($i!=$count-1) echo ",";
}
?> );
// If contents is bigger than container, Grid will automatically show scrollbars
g.set_scrollbars ( true );
// The grid will have a solid border (these are CSS attributes)
g.set_border ( 1, "solid", "#cccccc" );
// Now, we add some rows
//g.add_row ( "fsoft", "纯月", "fsoft (@) sourceforge (dot) net" );
//g.set_row_attr ( -1, 'magic', 'magic-fsoft' );
//g.set_row_attr ( -1, 'changed', 'NO' );
<?php
for ($i = 0, $count = count($gz_array); $i < $count; $i++) {
$str = "";
$str .= "g.add_row('".$gz_array[$i][0]."','".$gz_array[$i][1]."','".$gz_array[$i][2]."',";
$gzdata_array = $gz_array[$i][3];
for ($j = 0, $count2 = count($gzdata_array); $j < $count2; $j++) {
$str .= "'";
$str .= $gzdata_array[$j][0];
$str .= "'";
if($j!=$count2-1) $str .= ",";
}
$str = $str.");";
echo $str;
// mygrid.addRow("2","00000002,text1,text2,1,1,1,1,1,1");
} ?>
// Enable sortable rows
//g.set_sortable ( true );
// Enable highlight of rows with the mouse
g.set_highlight ( true );
//g.set_col_editable ( 1, "txt" );
//g.set_col_editable ( 2, "txt" );
<?php
for ($i = 3, $count = count($items); $i <= $count; $i++) {
echo "g.set_col_editable ( ".$i.", \"txt\" );";
}
?>
g.sort_on_edit = true;
// Show the grid replacing the original HTML object with the "grid" ID.
g.render ( 'grid' );
</script>
4 http_fetch_sync 函数如下
var global_xmlhttp = getXMLHTTPinstance();
function http_fetch_sync(url,post_data)
{
global_xmlhttp = getXMLHTTPinstance();
var method = 'GET';
if ( typeof(post_data) != 'undefined' )
{
method = 'POST';
}
try
{
//global_xmlhttp.open(method, url,true);
global_xmlhttp.open(method, url,false);
}
catch(e)
{
alert('message:'+e.message+":url:"+url);
}
if ( method == 'POST')
{
global_xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
global_xmlhttp.send(post_data);
var args = {"responseText":global_xmlhttp.responseText,"responseXML":global_xmlhttp.responseXML,
"request_id":request_id};
return args;
}