英文字符可用形如
{$vo.title|substr=0,5}
如果是中文字符thinkphp提供了msubstr如下
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)使用如下
{$vo.title|msubstr=0,21}
重點函數1:
$this->ajaxReturn($result,"型號增加成功!",1);
第一個是返回的數據變量,第二個是返回的信息,第三個是數據返回的狀態。
重點函數2:
ThinkAjax.send('__APP__/Index/delete/','ajax=1&partid='+partid,delComplete,'result');
第一個參數:在控制器里面的函數名稱
第二個參數:需要傳遞的參數ajax=1好像不可少
第三個參數:提交成功執行的函數名稱
第四個參數,就是顯示“數據處理中~”這些提示信息的Div的名稱。
重點函數3:
ThinkAjax.sendForm('frmpart','__APP__/Index/insert',addComplete,'result');
第一個參數代表提交名稱為frmpart的表單
第二是參數是提交的地址
第三個參數,如果提交成功,執行的函數名稱
第四個參數,就是顯示“數據處理中~”這些提示信息的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','__APP__/Index/insert',addComplete);
}
function addComplete(data,status){
if (status==1)
{
window.setTimeout(function (){window.location.href='__URL__',20000});
}
}
function delStock(partid){
ThinkAjax.send('__APP__/Index/delete/','ajax=1&partid='+partid,delComplete);
}
function delComplete(data,status){
if (status==1)
{
window.setTimeout(function (){window.location.href='__URL__',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="../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());
}
}
