当前位置: 首页 > 图文教程 > 网络编程 > AJAX技术 > ajax实时任务提示功能的实现代码

AJAX技术
自己动手封装的 ajax
Ajax 对象 包含post和get两种异步传输方式
Ajax 超时检查脚本
AJAX 简介及入门实例
ajax 开发守则 10条说明
Ajax 返回字符串的过滤实现代码
Ajax 程序开发中常见问题
AJAX 验证框架13个
ajax 入门基础之 XMLHttpRequest对象总结
基于AJAX的分页类实现代码
如何在Asp.net中使用HtmlArea编辑器
使用 jQuery 简化 Ajax 开发
ASP.NET 与 Ajax 的实现方式
AJAX技术介绍
Ajax程序设计入门
学习Ajax教程,详细了解Get与Post
关于Ajax responseText 的一点阐述
ajax中文乱码解决方法
AJAX中文问题总结
AJAX无刷新更新数据

AJAX技术 中的 ajax实时任务提示功能的实现代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 102 ::
收藏到网摘: n/a

本项目运用了 FLEAPHP,MYSQL,SMARTY,FCKEDItor,JSON,PROTOTYPE的技术,在这里首先要感谢这些开源项目的开发者给我们带来的好东西,其次要感谢[生气猪--让我帮她做一个这样的小东西来提醒她按时完成事情].花了一个3个小时完成.希望给大家起到抛砖引玉的作用啊.... 项目代码结构见 我之前写的[EXT/FCKEditor 集成 -- AJAX UI -- 一种web开发的新的思维,要及时转换思想]一文.
中的
├─taskofpig
│ ├─Controller
│ ├─Dao
│ ├─js
│ ├─music
│ ├─tpl
│ ├─tpl_c
│ └─_log
项目代码如下:
db.sql
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for task
-- ----------------------------
CREATE TABLE `task` (
`id` int(11) NOT NULL,
`title` varchar(100) collate utf8_unicode_ci NOT NULL,
`desc` text collate utf8_unicode_ci,
`date` datetime NOT NULL,
`created` int(11) default NULL,
`updated` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- ----------------------------
-- Table structure for task_seq
-- ----------------------------
CREATE TABLE `task_seq` (
`id` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/ucren/taskofpig/index.php
<?php
//设置正确的时区
date_default_timezone_set("Asia/Shanghai");
define('TASKOFPIG_DIR',dirname(__FILE__)) ;
require('../phplibs/FLEA/FLEA.php');
// 对$GLOBALS[G_FLEA_VAR]['CLASS_PATH'] 进行配置
FLEA::import(TASKOFPIG_DIR); //将当前目录加入到环境变量中
FLEA::loadAppInf('appConfig.php') ; //将配置文件单独分出来,容易维护
FLEA::init();
// 由于 FLEA_Db_TableDataGateway 并不是自动载入的,因此需要明确载入
FLEA::loadClass('FLEA_Db_TableDataGateway');
FLEA::runMVC();
?>
/ucren/taskofpig/appConfig.php
<?php
// 对 $GLOBALS[G_FLEA_VAR]['APP_INF'] 进行配置
return array(
'dispatcher' => 'FLEA_Dispatcher_Simple' , //定制调度器 FLEA_Dispatcher_Auth
'controllerAccessor' => 'ctl' ,
'actionAccessor' => 'act' ,
'view' => 'FLEA_View_Smarty', //定制视图
'viewConfig' => array(
'smartyDir' => '../phplibs/Smarty',
'template_dir' => './tpl',
'compile_dir' => './tpl_c',
'left_delimiter' => '<%',
'right_delimiter' => '%>',
'debugging' => false
),
'dbDSN' => array( //定制数据库连接参数
'driver' => 'mysql',
'host' => 'localhost',
'login' => 'dbuser',
'password' => 'dbpass',
'database' => 'dbname' ,
'charset ' => 'utf8'
) ,
'logFileDir' => './log' , //定制日志
'logFilename' => 'task_admin.log'
);
?>
/ucren/taskofpig/Dao/Table.php
<?php
//生气猪的任务计划表
class Dao_TaskTable extends FLEA_Db_TableDataGateway
{
// 指定数据表名称
var $tableName = 'task';
// 指定主键字段名
var $primaryKey = 'id';
}
?>
/ucren/taskofpig/Controller/Default.php
<?php
FLEA::loadFile('Dao_Table.php',true) ;
FLEA::loadFile('FLEA_Ajax_JSON.php',true) ;
class Controller_Default extends FLEA_Controller_Action
{
var $smarty ;
function Controller_Default()
{
$this->smarty = $this->_getView();
$this->smarty->assign('sitename','任务计划表 -- 生气猪') ;
$this->smarty->assign('opname','任务列表') ;//缺省应该在子模块中更改值
}
function actionIndex()
{
$this->toModulePage(); //缺省显示任务列表页
}
//定义一个函数用于调用FCKeditor
function call_fck($input_name,$input_value,$w='800',$h='400')
{
include_once '../fckeditor/fckeditor.php';
$fcked = new FCKeditor($input_name) ;
$fcked->BasePath = '../fckeditor/';
$fcked->ToolbarSet = 'Default' ; //工具栏设置
$fcked->InstanceName = $input_name ;
$fcked->Width = $w;
$fcked->Height = $h;
$fcked->Value = $input_value;
$fck_area = $fcked->CreateHtml();
$this->smarty->assign('fck_area',$fck_area);
unset($fck_area) ;
unset($fcked) ;
}
function _showPage($tpl='taskofpig.main.html')
{
$this->smarty->display($tpl);
}
function actionAdd()
{
$this->addTask();
}
function actionUpdate()
{
$this->updateTask();
}
function deleteTask($id){
$row = array('id'=>$id);
$thisDao = & new Dao_TaskTable() ;
$status = $thisDao->remove($row); //返回boolean值
unset($thisDao);
return $status ;
}
function listTask()
{
$thisDao = & new Dao_TaskTable() ;
$rows = $thisDao->findAll(); //二维数组
foreach($rows as &$row) //注意这里要传引用
{
$row['desc'] = mb_substr($row['desc'],0,40,'UTF-8');
}
$this->smarty->assign('rowSet',$rows);
$this->_showPage();
}
function addTask()
{
$thisDao = & new Dao_TaskTable() ;
$row = array(
'title' => $_REQUEST['title'],
'desc' => $_REQUEST['desc'],
'date' => $_REQUEST['date']
);
$commitId = $thisDao->create($row);
unset($thisDao);
echo "成功添加新任务";
redirect( url("Default"),1) ;
}
function updateTask()
{
$thisDao = & new Dao_TaskTable() ;
$row = array(
'id' => $_REQUEST['id'],
'title' => $_REQUEST['title'],
'desc' => $_REQUEST['desc'],
'date' => $_REQUEST['date']
);
$commitId = $thisDao->update($row);
unset($thisDao);
echo "成功更新任务";
redirect( url("Default"),1) ;
}
function queryTask($id){
$thisDao = & new Dao_TaskTable() ;
$row = $thisDao->find(array('id'=>$id));
unset($thisDao);
return $row ;
}
function queryTaskForDate($date=null)
{
$thisDao = & new Dao_TaskTable() ; //'2008-08-17 07:42:29'
$row = $thisDao->find(array('date'=>date('Y-m-d H:i:s')));
unset($thisDao);
if (!empty($row))
{
$jsonobj = new Services_JSON();
echo $jsonobj->encode($row);
}
else
die(date('Y-m-d H:i:s'));
}
//任务流转控制方法
function toModulePage()
{
if ($_REQUEST['op'] == 'search') {
$this->queryTaskForDate();
}
else if ($_REQUEST['op'] == 'add') {
$this->smarty->assign('opname','添加新任务') ;
$this->smarty->assign('taskTime',date('Y-m-d H:i:s')) ;
$this->call_fck('desc','');
$this->_showPage('taskofpig.add.html');
}
else if ($_REQUEST['op'] == 'del') {
if ( isset($_REQUEST['id']) && is_numeric($_REQUEST['id']) )
$status = $this->deleteTask($_REQUEST['id']) ;
$this->listTask();
}
else if ($_REQUEST['op'] == 'edit') {
if ( isset($_REQUEST['id']) && is_numeric($_REQUEST['id']) ){
$row = $this->queryTask($_REQUEST['id']) ;
}
$this->call_fck('desc',$row['desc']);
unset($row['desc']) ;
$this->smarty->assign('rowSet',$row);
$this->smarty->assign('opname','修改任务') ;
$this->_showPage('taskofpig.edit.html');
}
else { //列表
$this->listTask();
}
}
}
?>
本项目运用了 FLEAPHP,MYSQL,SMARTY,FCKEDItor,JSON,PROTOTYPE的技术,在这里首先要感谢这些开源项目的开发者给我们带来的好东西,其次要感谢[生气猪--让我帮她做一个这样的小东西来提醒她按时完成事情].花了一个3个小时完成.希望给大家起到抛砖引玉的作用啊....
/ucren/taskofpig/tpl/taskofpig.main.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><%$sitename%> -- <%$opname%></title>
<script type="text/javascript" src="/ucren/prototype/prototype.js"></script>
<script type="text/javascript" src="/ucren/json/json_parse.js"></script>
<script type="text/javascript" src="js/tasktip.js"></script>
</head>
<body>
<bgsound>
<div>
<span id='task_attime'></span>
</div>
<hr>
<p>
<span onClick="document.location='index.php?op=add'">添加</span> |
<span onClick="document.location='index.php?op=search'">查看任务提示</span> |
</p>
<hr>
<hr>
<table width="90%" border="1" cellspacing="1" bgcolor="#cfdadc">
<tr bgcolor="#e8edec" align="center">
<td><b>ID</b></td>
<td><b>主题</b></td>
<td><b>任务内容</b></td>
<td><b>任务提示时间</b></td>
<td colspan="2"><b>管理</b></td>
</tr>
<%section name=rowIndex loop=$rowSet%>
<tr align="center">
<td><%$rowSet[rowIndex].id%></td>
<td><%$rowSet[rowIndex].title%></td>
<td><%$rowSet[rowIndex].desc%></td>
<td><%$rowSet[rowIndex].date%></td>
<td onClick="document.location='index.php?op=edit&id=<%$rowSet[rowIndex].id%>'"><b>编辑</b></td>
<td onClick="document.location='index.php?op=del&id=<%$rowSet[rowIndex].id%>'"><b>删除</b></td>
</tr>
<%/section%>
</table>
</body>
</html>
/ucren/taskofpig/tpl/taskofpig.edit.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><%$sitename%> -- <%$opname%></title>
</head>
<body bgcolor="e8edec">
<div align="center">
<form method="post" action="index.php?&act=update&id=<%$rowSet.id%>">
<table width="80%" border="1" cellspacing="1" bgcolor="#cfdadc">
<tr bgcolor="e8edec">
<td>任务标题: <input name="title" type="text" size="80" value="<%$rowSet.title%>"></td>
</tr>
<tr bgcolor="e8edec">
<td>提示时间: <input name="date" type="text" size="40" value="<%$rowSet.date%>"></td>
</tr>
<tr bgcolor="e8edec">
<td class="forumRow">任务内容<%$fck_area%></td>
</tr>
</table>
<input type="submit" value="提交">
<input type="button" value="返回" onclick="document.location='index.php'">
</form>
</div>
</body>
</html>
/ucren/taskofpig/tpl/taskofpig.add.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><%$sitename%> -- <%$opname%></title>
</head>
<body bgcolor="e8edec">
<div align="center">
<form method="post" action="index.php?&act=add">
<table width="80%" border="1" cellspacing="1" bgcolor="#cfdadc">
<tr bgcolor="e8edec">
<td>任务标题: <input name="title" type="text" size="80" ></td>
</tr>
<tr bgcolor="e8edec">
<td>提示时间: <input name="date" type="text" size="40" value="<%$taskTime%>"></td>
</tr>
<tr bgcolor="e8edec">
<td class="forumRow">任务内容<%$fck_area%></td>
</tr>
</table>
<input type="submit" name="Submit" value="提交">
<input type="reset" name="Submit3" value="重设">
</form>
</div>
</body>
</html>
/ucren/taskofpig/js/tasktip.js
//任务提示脚本实现,依赖于/ucren/prototype/prototype.js
var TaskTipAjax = function (){
this.desc = '按一定的时间间隔查询数据库中到期的事件信息,并给出提示[打开新窗口,播放一段音乐]' ;
}
//通过Ajax按时查询和提示
TaskTipAjax.prototype.doAction = function(obj)
{
var myAjax = new Ajax.Request(
'index.php?op=search' ,
{
method: 'get' ,asynchronous: true ,
onComplete: obj.showResponse
}
) ;
}
TaskTipAjax.prototype.showResponse = function (response)
{
if (response.responseText != '')
{
//此方法在/ucren/json/json_parse.js中定义
//task_obj是一个对象
//task_obj.id task_obj.title task_obj.desc task_obj.date
var task_obj = json_parse(response.responseText);
var newwin=window.open('','任务报时器','height=200, width=600,toolbar=0,menubar=0,location=0, status=0');
newwin.opener = null // 防止代码对论谈页面修改
//防止页面内容重复
if( typeof(newwin.document.body) != "undefined")
newwin.document.body.innerHTML = "";
newwin.document.write("<html><body><bgsound src='music/moonlight.mp3' autostart=true loop=infinite>");
newwin.document.write('<h1>任务报时器</h1><br/>');
newwin.document.write('<table border="1">');
newwin.document.write('<tr>');
newwin.document.write('<td width="33" bgcolor="#E8E8E8">ID</td>');
newwin.document.write('<td width="33" bgcolor="#E8E8E8">标题</td>');
newwin.document.write('<td width="33" bgcolor="#E8E8E8">描述</td>');
newwin.document.write('<td width="33" bgcolor="#E8E8E8">时间</td>');
newwin.document.write('</tr>');
newwin.document.write('<tr>');
newwin.document.write('<td>'+ task_obj.id +'</td>');
newwin.document.write('<td>'+ task_obj.title +'</td>');
newwin.document.write('<td>'+ task_obj.desc +'</td>');
newwin.document.write('<td>'+ task_obj.date +'</td>');
newwin.document.write('</tr>');
newwin.document.write('</table>');
newwin.document.write("</body></html>");
//置顶
newwin.focus();
}
}
var obj = new TaskTipAjax();
setInterval("obj.doAction(obj)",1000) ;
/ucren/taskofpig/music/moonlight.mp3
这个音乐 可以自己改
上面的代码熟悉JAVA的应该都可以看懂,这里只是要阐述一个简单的设计方案,希望大家予以增加扩展功能,谢谢,发我邮件,或者留言.
我觉得软件开发最重要的是开发思路,至于用什么语言实现倒是其次了,(*^__^*) 嘻嘻……