好久沒來寫部落格了,並不是懈怠了,而是最近在寫一個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教程有興趣的朋友有所幫助。