0

I have implemented the update and create functionality for my website.

I have a page for items. On the index page I show all the items and there is a create button to create a new item and an update button against each item to update a particular item.

The create and update forms are displayed below the list of items on the index page using ajax call, I call the actionUpdate and actionCreate and render the forms on the index page using the renderPartial.

If the update or create is success, the index page is reloaded.

I do not want to reload the entire page but just the list of items.

Here is the code snippet of my create action:

public function actionCreate() {

   $model = new Item();

   if( $model->load( Yii::$app->request->post() ) && $model->save() )
   {
      $items= Item::find()->all();
      return $this->redirect(['index', 'items' => $items]);
      exit;
   }  

   return $this->renderPartial('createUpdate', ['item' => $model, 'title' => "Create item" ]); 
}

Is there a way to achieve this?

-Nishi

Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
  • Then do ajax call and replace the div with the result – Sulthan Allaudeen Jun 03 '15 at 10:39
  • Thanks for your reply! Is there a way to get this done on the create submitButton click? The flow is like this: On the index page, I have a create item button which displays the create form below the list of items on the index page. On the create form also I have a Create submitButton which intern calls the actionCreate when the model is loaded. What I am trying to achieve is on the create submitbutton click, the create form should go away and only the list of items is refreshed without reloading or refreshing the page. I am not sure how to do this on the submit button click. Any ideas? – user3095514 Jun 03 '15 at 10:51
  • Shall i help you in logical way.. (As i don't know about the way you deal) – Sulthan Allaudeen Jun 03 '15 at 11:28
  • Please help.Let me know in the logical way. I would really appreciate that. Thanks – user3095514 Jun 03 '15 at 12:12
  • @user3095514 you can use widget for your items and reload content. Similar example here: http://stackoverflow.com/questions/30625173/yii-widget-reload-via-ajax/30642240#30642240 Just call ajax on click create and update content. – Danila Ganchar Jun 05 '15 at 12:09

1 Answers1

0

As the OP wants to give the logical solution i am giving this answer.

Step 1 :

While clicking the Create Button have the id as trigger and have the following script in your view.

<script>
$(document).ready(function(){
$("#trigger").click(function(){
$.ajax({url: "actionCreate", success: function(result){
$("#result").html(result);
}});
});
});
</script>

So whenever you click the create button it will call the actionCreate function.

Step 2 :

And according to your controller , it will create entries, and here you're going to return the updated list as the table itself.

I.e., You can fetch the table as a variable and do a foreach inside and build a table in the controller itself and return it to the view.

And according to this line in jquery $("#result").html(result); It will replace the div result with the one which you return.. I.e., It will give you the updated / new list in your view without reloading

Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
  • Thanks for reply. This approach does not suit my requirement. This will work if I click a button and just replace a some content. In my case if you see the code snippet above, I am calling the actioncreate in the first call when I am trying to display the form. So the control does not go inside if() and just displays the create form and then when I click "Create" on the form after filling data, the $model has data and it again calls the actionCreate and creates the new form and also reloads the index page. I do not want to reload this page but only a part of it. Any ideas on this? – user3095514 Jun 04 '15 at 10:57
  • Ah, Cool buddy, never mind about the one what i have. I am giving you the logic.. The thing is if the user clicked on this button, it will call an action which will call your function. And it will call the newly updated table and then the existing table content will be replaced with newly queried content.. – Sulthan Allaudeen Jun 04 '15 at 11:23