好久没来写博客了,并不是懈怠了,而是最近在写一个Post文章管理的大module,由于php和zend没怎么学,所以花费了大概一周的时候,才实现了增删改查功能,最近在改进模块功能,因为那里面的代码量实在太大,具体的要想一想才能写博客,这次写的是针对修改文章状态,使用ajax实现的,以前一直不知道ajax的好处,这次具体使用了,才算知道了一些皮毛,接下来,进入正题。
首先贴一下ajax代码:
<code><span>script</span>><span> $(<span><span>function</span><span>()</span> {</span> $(<span>".post-list-table .check"</span>).click(<span><span>function</span><span>()</span> {</span><span>var</span> post_id = $(<span>this</span>).parent().attr(<span>"alt"</span>); <span>var</span> status = $(<span>this</span>).attr(<span>"alt"</span>); $.get(<span>"/post/operate/"</span>+ post_id +<span>"/?status="</span>+ status, <span><span>function</span><span>(ret)</span> {</span> console.log(ret); <span>if</span>(ret[<span>1</span>] == <span>1</span>) { $(<span>"table tbody .status-"</span>+ post_id).html(<span>"Published"</span>); } <span>else</span><span>if</span>(ret[<span>1</span>] == -<span>1</span>) { $(<span>"table tbody .status-"</span>+ post_id).html(<span>"Rejected"</span>); } <span>else</span> { $(<span>"table tbody .status-"</span>+ post_id).html(<span>"Draft"</span>); } }); }); $(<span>".post-list-table .delete"</span>).click(<span><span>function</span><span>()</span> {</span><span>if</span>(confirm(<span>'确认删除?'</span>)) { <span>var</span> url = $(<span>this</span>).attr(<span>'url'</span>); $.getJSON(url, <span><span>function</span><span>(ret)</span> {</span>console.log(ret); <span>if</span>(ret[<span>0</span>] == <span>true</span>) { $(<span>'.delete[url="'</span>+url+<span>'"]'</span>).parents(<span>'tr'</span>).remove(); } }); } }); }) </span><span><span>script</span>></span></code>
从代码看出来,实现的是更改状态和删除两个功能,分别来自不同的action,接下来贴controller里面的action。
<code><span>public</span><span><span>function</span><span>operateAction</span><span>()</span> {</span><span>if</span>(!<span>$this</span>->userHasPermission(<span>'ADMIN'</span>, <span>'EDIT_REVIEW'</span>)) { <span>return</span><span>$this</span>->requirePermission(<span>'ADMIN'</span>, <span>'EDIT_REVIEW'</span>); } <span>$ret</span> = <span>false</span>; <span>$request</span> = <span>$this</span>->getRequest(); <span>$log_table</span> = <span>$this</span>->getPostLogTable(); <span>$user_service</span> = <span>$this</span>->getServiceLocator()->get(<span>'UserService'</span>); <span>$curr_user</span> = <span>$user_service</span>->getCurrentUser(); <span>$post_id</span> = <span>$this</span>->params()->fromRoute(<span>'id'</span>, <span>null</span>); <span>$post</span> = <span>$this</span>->getPostTable()->getPostById(<span>$post_id</span>); <span>$from_status</span> = <span>$post</span>[<span>'post_status'</span>]; <span>$status</span> = <span>$request</span>->getQuery(<span>'status'</span>, <span>null</span>); <span>$log_row</span> = <span>array</span>(); <span>if</span> (!is_null(<span>$status</span>)) { <span>if</span>(<span>$post</span>[<span>'post_status'</span>] != <span>$status</span>) { <span>$ret</span> = <span>$this</span>->getPostTable()->checkStatus(<span>$post</span>[<span>'id'</span>], (int)<span>$status</span>); <span>//var_dump($ret);exit();</span><span>if</span> (<span>$ret</span>) { <span>$log_row</span>[<span>'post_id'</span>] = <span>$post_id</span>; <span>$log_row</span>[<span>'user_id'</span>] = <span>$curr_user</span>->id; <span>$log_row</span>[<span>'user_name'</span>] = <span>$curr_user</span>->username; <span>$log_row</span>[<span>'date'</span>] = date(<span>'y-m-d'</span>,time()); <span>$log_row</span>[<span>'from_status'</span>] = <span>$from_status</span>; <span>$log_row</span>[<span>'to_status'</span>] = <span>$status</span>; <span>$log_table</span>->addRows(<span>$log_row</span>); } } <span>$ret</span> = <span>true</span>; } <span>$jsonModel</span> = <span>new</span> JsonModel(<span>array</span>(<span>$post_id</span>, <span>$ret</span> ? (int)<span>$status</span> : <span>$ret</span>)); <span>//var_dump($jsonModel);exit();</span><span>return</span><span>$jsonModel</span>; } <span>public</span><span><span>function</span><span>deletePostAction</span><span>()</span>{</span><span>if</span>(!<span>$this</span>->userHasPermission(<span>'ADMIN'</span>, <span>'VIEW_PRODUCT'</span>)) { <span>return</span><span>$this</span>->requirePermission(<span>'ADMIN'</span>, <span>'VIEW_PRODUCT'</span>); } <span>$post_id</span> = (int) <span>$this</span>->params()->fromRoute(<span>'post_id'</span>, <span>0</span>); <span>$ret</span> = <span>false</span>; <span>if</span> (<span>$post_id</span>) { <span>$table</span> = <span>$this</span>->getPostTable(); <span>$table</span>->deleteRowById(<span>$post_id</span>); <span>$this</span>->layout()->selectedTab = <span>'post-list'</span>; <span>$ret</span> = <span>true</span>; } <span>return</span><span>new</span> JsonModel(<span>array</span>(<span>$ret</span>)); }</code>
两个action的路由设置就不写了,也没有具体的phtml网页,只是实现功能而已。
ajax代码中,通过.get(url,data)函数,注意,此处的data是指action或者phtml返回的JsonModel的array数组,是返回的所有数据。实际上,在click之后,get的第一个参数,就执行了action,顺便获取了数据,然后根据参数,执行操作,这是非常方便的。
贴一下图吧,虽然看不出效果:
点击绿勾的话,status就是变成Published,点击红叉,就会变成Rejected,红垃圾箱,就是点击删除。
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了实习小结十二:Ajax的get函数使用实例,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。