Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class AjaxController extends Controller {
- public $layout = "null";
- public function actionIndex() {
- $this -> render('index');
- }
- public function actionCreate() {
- if (Yii::app() -> request -> isAjaxRequest) {
- $modelProduk = new Produk;
- if ($_POST) {
- $modelProduk['nama_produk'] = $_POST['namaproduk'];
- $modelProduk['jumlah_produk'] = $_POST['jumlahproduk'];
- $modelProduk['harga_produk'] = $_POST['hargaproduk'];
- $modelProduk -> save();
- }
- $this -> render('create');
- }
- }
- public function actionIndexproduk() {
- $criteria = new CDbCriteria( array('order' => 'id_produk DESC'));
- $count = Produk::model() -> count($criteria);
- $pages = new CPagination($count);
- $pages -> pageSize = 2;
- $pages -> applyLimit($criteria);
- $dataProduk = Produk::model() -> findAll($criteria);
- $this -> render('indexproduk', array('dataProduk' => $dataProduk, 'pages' => $pages, ));
- }
- public function actionUpdate($id) {
- if (Yii::app() -> request -> isAjaxRequest) {
- $dataProduk = Produk::model() -> findByPk($id);
- if ($_POST) {
- $dataProduk -> nama_produk = $_POST['namaproduk'];
- $dataProduk -> jumlah_produk = $_POST['jumlahproduk'];
- $dataProduk -> harga_produk = $_POST['hargaproduk'];
- $dataProduk -> save();
- }
- $this -> render('update', array('dataProduk' => $dataProduk));
- }
- }
- public function actionDelete($id) {
- if (Yii::app() -> request -> isAjaxRequest) {
- Produk::model() -> deleteByPk($id);
- }
- }
- public function actionSearch($criteria = '') {
- $criteria = new CDbCriteria( array(
- 'select' => '*',
- 'condition' => "nama_produk LIKE '%$criteria%'",
- 'order' => 'id_produk DESC', ));
- $count = Produk::model() -> count($criteria);
- $pages = new CPagination($count);
- $pages -> pageSize = 2;
- $pages -> applyLimit($criteria);
- $model = Produk::model() -> findAll($criteria);
- $this -> render('indexProduk', array('dataProduk' => $model, 'pages' => $pages));
- }
- }
- ?>
- <div id="form"></div>
- <div id="data"></div>
- <?php
- Yii::app() -> clientScript -> registerCoreScript('jquery',
- CClientScript::POS_BEGIN);
- ?>
- <script>
- $(document).ready(function(){
- $("#form").load('<?php echo Yii::app()->request->baseUrl;?>/ajax/create');
- $('#data').load('<?php echo Yii::app()->request->baseUrl;?>/ajax/indexproduk');
- });
- </script>
- <form action="" method="post" id="formSearch">
- <input type="text" name="criteria" id="criteria" />
- <input type="button" value="Search" id="buttonSearch" />
- </form>
- <table border="1" cellpadding="5">
- <tr>
- <th>Nama Produk</th>
- <th>Jumlah Produk</th>
- <th>Harga Produk</th>
- <th>#</th>
- </tr>
- <?php foreach ($dataProduk as $produk){?>
- <tr>
- <td><?php echo $produk -> nama_produk;?></td>
- <td><?php echo $produk -> jumlah_produk;?></td>
- <td><?php echo $produk -> harga_produk;?></td>
- <td>
- <a href="#" class="update" id="<?php echo $produk->id_produk;?>">Edit</a> ||
- <a href="#" class="delete" id="<?php echo $produk->id_produk;?>">Delete</a>
- </td>
- </tr>
- <?php }?>
- </table>
- <br>
- <?php
- $this->widget('CLinkPager', array(
- 'header'=>'',
- 'maxButtonCount'=>5,
- 'pages' => $pages,
- ))
- ?>
- <script>
- $(document).ready(function(){
- //search
- $("#buttonSearch").click(function(){
- var criteria = $('#criteria').val();
- $("#data").load('<?php echo Yii::app()->request->baseUrl;?>/ajax/search/criteria/'+criteria);
- });
- $('li a').click(function(){
- /*ambil value dari attribut href
- dan load ke <div id=”data” */
- $('#data').load($(this).attr('href'));
- return false;
- });
- $('.update').click(function(){
- var id =$(this).attr('id');
- $('#form').load('<?php echo Yii::app()->request->baseUrl;?>/ajax/update/'+id);
- return false;
- });
- $('.delete').click(function(){
- var id =$(this).attr('id');
- $.ajax({
- type:'POST',
- url:'<?php echo Yii::app()->request->baseUrl;?>/ajax/delete/'+id,
- success:function(data){
- $('#data').load('<?php echo Yii::app()->request->baseUrl;?>/ajax/indexproduk');
- }
- });
- return false;
- });
- });
- </script>
- <h3>Create Data Produk</h3>
- <form action="" method="post" id="myForm">
- <table>
- <tr>
- <td>Nama Produk</td>
- <td>:</td>
- <td><input type="text" name="namaproduk" size="50" />
- </td>
- </tr>
- <tr>
- <td>Jumlah Produk</td>
- <td>:</td>
- <td><input type="text" name="jumlahproduk" size="5" /> </td>
- </tr>
- <tr>
- <td>Harga Produk</td>
- <td>:</td>
- <td><input type="text" name="hargaproduk" size="25" /></td>
- </tr>
- <tr>
- <td> </td>
- <td> </td>
- <td><input id="create" type="button" value="Create" /></td>
- </tr>
- </table>
- </form>
- <script>
- $(document).ready(function(){
- $('#create').click(function(){
- $('#create').attr({
- value : 'loading..',
- disabled : true,
- });
- var myFormData = $("#myForm").serialize();
- $.ajax({
- type:'POST',
- url : '<?php echo Yii::app()->request->baseUrl;?>/ajax/create',
- data:myFormData,
- success:function(data){
- $('#data').load('<?php echo Yii::app()->request->baseUrl;?>/ajax/indexproduk');
- $('#create').attr({
- value : 'Create',
- disabled : false,
- });
- $('#myForm')[0].reset();
- }
- });
- return false;
- });
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement