
重点函数1: |
$this->ajaxReturn($result,"型号增加成功!",1); |
第一个是返回的数据变量,第二个是返回的信息,第三个是数据返回的状态。 |
|
重点函数2: |
ThinkAjax.send('/Index/delete/','ajax=1&partid='+partid,delComplete,'result'); |
第一个参数:在控制器里面的函数名称 |
第二个参数:需要传递的参数ajax=1好像不可少 |
第三个参数:提交成功执行的函数名称 |
第四个参数,就是显示“数据处理中~”这些提示信息的Div的名称。 |
|
重点函数3: |
ThinkAjax.sendForm('frmpart','/Index/insert',addComplete,'result'); |
第一个参数代表提交名称为frmpart的表单 |
第二是参数是提交的地址 |
第三个参数,如果提交成功,执行的函数名称 |
第四个参数,就是显示“数据处理中~”这些提示信息的Div的名称。 |
<div id="result" class="none result" style="float:right;font-family:微软雅黑,Tahoma;width:150px;letter-spacing:2px"></div> |
|
|
function addComplete(data,status) |
其中的data参数,就是我们提交成功之后的返回值 |
$this->ajaxReturn($result,"型号增加成功!",1); |
那么,data就是变量$result的值,sataus就是最后的这个参数"1"或者"0" |
当然,别忘了在用ThinkAJAX的时候写上 |
XML/HTML代码 |
<load href="/Public/Js/Base.js" /> |
<load href="/Public/Js/prototype.js" /> |
<load href="/Public/Js/mootools.js" /> |
<load href="/Public/Js/Ajax/ThinkAjax.js" /> |
下面是代码 |
|
先看模板文件代码(着重看红色部分代码): |
<tagLib name="html" /> |
<include file="Public:header" /> |
<SCRIPT LANGUAGE="JavaScript"> |
<!-- |
function addStock(){ |
ThinkAjax.sendForm('frmpart','/Index/insert',addComplete); |
} |
function addComplete(data,status){ |
if (status==1) |
{ |
window.setTimeout(function (){window.location.href='/Mylib',20000}); |
} |
} |
function delStock(partid){ |
ThinkAjax.send('/Index/delete/','ajax=1&partid='+partid,delComplete); |
} |
function delComplete(data,status){ |
if (status==1) |
{ |
window.setTimeout(function (){window.location.href='/Mylib',20000}); |
} |
} |
//--> |
</SCRIPT> |
<table width="80%" border="1" cellspacing="0" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF"> |
<!--数据新增表单--> |
<form name=frmpart> |
<tr bgcolor='#E8E8E8'> |
<td width="10%"><input type="hidden" name="ajax" value="1"><input type="button" value="新增数据" onclick="addStock()"></td> |
<td width="38%">型号:<input type="text" id="partno" name="partno"> |
<div id="result" class="none result" style="float:right;font-family:微软雅黑,Tahoma;width:150px;letter-spacing:2px"></div> |
</td> |
<td width="25%">厂家:<input type="text" id="mfg" name="mfg"></td> |
<td width="12%">批号:<input type="text" id="datecode" name="datecode"></td> |
<td width="15%">数量:<input type="text" id="qty" name="qty"></td> |
</tr> |
</form> |
<tr bgcolor='#E8E8E8'> |
<td width="10%"><strong>ID</strong></td> |
<td width="38%"><strong>PartNo.</strong></td> |
<td width="25%"><strong>Mfg.</strong></td> |
<td width="12%"><strong>Datecode</strong></td> |
<td width="15%"><strong>Qty.</strong></td> |
</tr> |
<!--循环输出查询结果数据集--> |
<volist name='list' id='vo' > |
<tr> |
<td width="10%">{$vo.partid}</td> |
<td width="38%">{$vo.partno}</td> |
<td width="25%">{$vo.mfg}</td> |
<td width="12%">{$vo.datecode}</td> |
<td width="15%">{$vo.qty} <IMG SRC="/YunCode/Tpl/Public/images/del.gif" WIDTH="20" HEIGHT="20" BORDER="0" style="cursor:pointer" ALT="" onclick="delStock('{$vo.partid}')" align="absmiddle"></td> |
</tr> |
</volist> |
</table> |
<include file="Public:footer" /> |
再看控制器代码: |
// 数据写入操作 |
public function insert() { |
$Stk = D('Stock'); |
$Stk->create(); |
if($result=$Stk->add()) { |
$this->ajaxReturn($result,"型号增加成功!",1); |
}else { |
$this->error("型号增加失败!"); |
} |
} |
public function delete() { |
$Stk = M('Stock'); |
$condition['partid'] = $_REQUEST['partid']; |
if ($Stk->where($condition)->delete()) { |
$this->ajaxReturn($partid, $partid."型号删除成功!", 1); |
}else { |
$this->error($Stk->getError()); |
} |
} |
详解二: |
在动作模版页面添加代码: |
<load href="/Public/js/Base.js" /> |
<load href="/Public/js/prototype.js" /> |
<load href="/Public/js/mootools.js" /> |
<load href="/Public/js/ThinkAjax.js" /> |
<!--<load href="/Public/js/change.js" /> --> |
<script type="text/javascript"> |
function change(){ |
var ids = document.getElementById("name").value; |
ThinkAjax.send("/Code/fun","ajax=1&ids="+ids,'','result'); |
} |
</script> |
解释: |
第一个参数:在控制器里面的函数名称 |
第二个参数:需要传递的参数ajax=1好像不可少 |
第三个参数:提交成功执行的函数名称 |
第四个参数:显示返回值的id |
假设动作为: |
<input type="text" id="name" /> |
<input type="submit" onclick="change()" /> |
<div id="result">这里是返回信息</div> |
控制器action代码: |
public function fun(){ |
$ids = $_REQUEST['ids']; //接受参数 也可以用post方法接受 |
$majoy = new Model("majoy"); |
$data = $majoy->getBycollege_id($ids); |
if(!empty($data)){ |
$this->ajaxReturn("","已经存在",1); //此处可以用$this->error("已经存在"); |
} |
} |
解释:第一个是返回的数据变量, |
第二个是返回的信息, |
第三个是数据返回的状态。为0即使失败,显示字体为红色,为1则代表成功,显示字体为绿色 |



