Advertisement
Guest User

Untitled

a guest
Sep 18th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. <?php
  2. class AjaxController extends Controller {
  3.  
  4. public $layout = "null";
  5.  
  6. public function actionIndex() {
  7. $this -> render('index');
  8. }
  9.  
  10. public function actionCreate() {
  11. if (Yii::app() -> request -> isAjaxRequest) {
  12. $modelProduk = new Produk;
  13. if ($_POST) {
  14. $modelProduk['nama_produk'] = $_POST['namaproduk'];
  15. $modelProduk['jumlah_produk'] = $_POST['jumlahproduk'];
  16. $modelProduk['harga_produk'] = $_POST['hargaproduk'];
  17. $modelProduk -> save();
  18. }
  19. $this -> render('create');
  20. }
  21. }
  22.  
  23. public function actionIndexproduk() {
  24.  
  25. $criteria = new CDbCriteria( array('order' => 'id_produk DESC'));
  26. $count = Produk::model() -> count($criteria);
  27. $pages = new CPagination($count);
  28. $pages -> pageSize = 2;
  29. $pages -> applyLimit($criteria);
  30. $dataProduk = Produk::model() -> findAll($criteria);
  31. $this -> render('indexproduk', array('dataProduk' => $dataProduk, 'pages' => $pages, ));
  32. }
  33.  
  34. public function actionUpdate($id) {
  35. if (Yii::app() -> request -> isAjaxRequest) {
  36. $dataProduk = Produk::model() -> findByPk($id);
  37. if ($_POST) {
  38. $dataProduk -> nama_produk = $_POST['namaproduk'];
  39. $dataProduk -> jumlah_produk = $_POST['jumlahproduk'];
  40. $dataProduk -> harga_produk = $_POST['hargaproduk'];
  41. $dataProduk -> save();
  42. }
  43. $this -> render('update', array('dataProduk' => $dataProduk));
  44. }
  45. }
  46.  
  47. public function actionDelete($id) {
  48. if (Yii::app() -> request -> isAjaxRequest) {
  49. Produk::model() -> deleteByPk($id);
  50. }
  51. }
  52.  
  53. public function actionSearch($criteria = '') {
  54. $criteria = new CDbCriteria( array(
  55. 'select' => '*',
  56. 'condition' => "nama_produk LIKE '%$criteria%'",
  57. 'order' => 'id_produk DESC', ));
  58.  
  59. $count = Produk::model() -> count($criteria);
  60. $pages = new CPagination($count);
  61. $pages -> pageSize = 2;
  62. $pages -> applyLimit($criteria);
  63. $model = Produk::model() -> findAll($criteria);
  64. $this -> render('indexProduk', array('dataProduk' => $model, 'pages' => $pages));
  65. }
  66. }
  67. ?>
  68.  
  69. <div id="form"></div>
  70. <div id="data"></div>
  71. <?php
  72. Yii::app() -> clientScript -> registerCoreScript('jquery',
  73. CClientScript::POS_BEGIN);
  74. ?>
  75. <script>
  76. $(document).ready(function(){
  77. $("#form").load('<?php echo Yii::app()->request->baseUrl;?>/ajax/create');
  78. $('#data').load('<?php echo Yii::app()->request->baseUrl;?>/ajax/indexproduk');
  79. });
  80. </script>
  81.  
  82. <form action="" method="post" id="formSearch">
  83. <input type="text" name="criteria" id="criteria" />
  84. <input type="button" value="Search" id="buttonSearch" />
  85. </form>
  86.  
  87. <table border="1" cellpadding="5">
  88. <tr>
  89. <th>Nama Produk</th>
  90. <th>Jumlah Produk</th>
  91. <th>Harga Produk</th>
  92. <th>#</th>
  93. </tr>
  94. <?php foreach ($dataProduk as $produk){?>
  95. <tr>
  96. <td><?php echo $produk -> nama_produk;?></td>
  97. <td><?php echo $produk -> jumlah_produk;?></td>
  98. <td><?php echo $produk -> harga_produk;?></td>
  99. <td>
  100. <a href="#" class="update" id="<?php echo $produk->id_produk;?>">Edit</a> ||
  101. <a href="#" class="delete" id="<?php echo $produk->id_produk;?>">Delete</a>
  102. </td>
  103. </tr>
  104. <?php }?>
  105. </table>
  106. <br>
  107. <?php
  108. $this->widget('CLinkPager', array(
  109. 'header'=>'',
  110. 'maxButtonCount'=>5,
  111. 'pages' => $pages,
  112. ))
  113. ?>
  114.  
  115. <script>
  116. $(document).ready(function(){
  117.  
  118. //search
  119. $("#buttonSearch").click(function(){
  120. var criteria = $('#criteria').val();
  121. $("#data").load('<?php echo Yii::app()->request->baseUrl;?>/ajax/search/criteria/'+criteria);
  122. });
  123.  
  124. $('li a').click(function(){
  125. /*ambil value dari attribut href
  126. dan load ke <div id=”data” */
  127. $('#data').load($(this).attr('href'));
  128. return false;
  129. });
  130.  
  131. $('.update').click(function(){
  132. var id =$(this).attr('id');
  133. $('#form').load('<?php echo Yii::app()->request->baseUrl;?>/ajax/update/'+id);
  134. return false;
  135. });
  136.  
  137. $('.delete').click(function(){
  138. var id =$(this).attr('id');
  139. $.ajax({
  140. type:'POST',
  141. url:'<?php echo Yii::app()->request->baseUrl;?>/ajax/delete/'+id,
  142. success:function(data){
  143. $('#data').load('<?php echo Yii::app()->request->baseUrl;?>/ajax/indexproduk');
  144. }
  145. });
  146. return false;
  147. });
  148. });
  149. </script>
  150.  
  151. <h3>Create Data Produk</h3>
  152. <form action="" method="post" id="myForm">
  153. <table>
  154. <tr>
  155. <td>Nama Produk</td>
  156. <td>:</td>
  157. <td><input type="text" name="namaproduk" size="50" />
  158. </td>
  159. </tr>
  160. <tr>
  161. <td>Jumlah Produk</td>
  162. <td>:</td>
  163. <td><input type="text" name="jumlahproduk" size="5" /> </td>
  164. </tr>
  165. <tr>
  166. <td>Harga Produk</td>
  167. <td>:</td>
  168. <td><input type="text" name="hargaproduk" size="25" /></td>
  169. </tr>
  170. <tr>
  171. <td>&nbsp;</td>
  172. <td>&nbsp;</td>
  173. <td><input id="create" type="button" value="Create" /></td>
  174. </tr>
  175. </table>
  176. </form>
  177. <script>
  178. $(document).ready(function(){
  179. $('#create').click(function(){
  180. $('#create').attr({
  181. value : 'loading..',
  182. disabled : true,
  183. });
  184. var myFormData = $("#myForm").serialize();
  185. $.ajax({
  186. type:'POST',
  187. url : '<?php echo Yii::app()->request->baseUrl;?>/ajax/create',
  188. data:myFormData,
  189. success:function(data){
  190. $('#data').load('<?php echo Yii::app()->request->baseUrl;?>/ajax/indexproduk');
  191. $('#create').attr({
  192. value : 'Create',
  193. disabled : false,
  194. });
  195. $('#myForm')[0].reset();
  196. }
  197. });
  198. return false;
  199. });
  200. });
  201. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement