Register here!

Forgot password?

ASK NEW QUESTION

Home Page » Zii » List View » ListView: Update ListView items after clicking on button in a different ListView
0 Votes
Vote Con!

ListView: Update ListView items after clicking on button in a different ListView

clickCListViewcruddifferenthtmlbuttonjquerymodelsubmitbuttonUpdate

Hey there,

I been trying to figure this out for a while now and going nowhere. Would really appreciate some pointers as to what am I missing.

Got 1 model, a “Course”, and 1 view “Timetable”. In the view, I’ve got:

  • 1st BootListView to display results of a course search form
  • 2nd BootListView to display courses that the student is registered for
  • 3rd BootListView to display courses that are just being previewed in the timetable
  • 4th BootListView to display courses for which the student is on waiting list to register in.
  • A BootGridView to display timetable
  • The link is http://www.318-new-rosi.site11.com/course/timetable2

    I would like to be able to update at least (for now) the 2nd BootListView when the student registers for a course via the “Add to courses” button in the modal that appears when the “More Info” button is clicked.

    Any ideas would really be very much appreciated.

    By blackrobe89 in List View · Asked 431 days 17 hours 35 mins ago

    Questions: 1 Accepted: 0 ( 0% ) | Reputation: 0

     

    Answers (9)

    1. 0 votes

      Hello. I think you have to use ajax’s update mechanism that would be fired when your modal is closed. In order to feed your updated data, you have to echo something as a result of your modal submit action.
      After seeing your resulting HTML code, it should go more or less like this:

      When you click on “More info” on the CSC318H1 course for instance, a modal is displayed with an “Add to Courses” submit button, which has the “add-button-5″ id
      I can see (source code) the following javascript (jQuery) code attached to that button

      1. $('body').on('click','#add-button-5',function(){
      2.      jQuery.ajax({
      3.           'success': function(data) {
      4.                         $("td.CSC318H1").text("CSC318H1");
      5.                         $("#modal-5 a.btn[data-dismiss]").click();
      6.           },
      7.           'type':'POST',
      8.           'url':'/course/add/5',
      9.           'cache':false,
      10.           'data':jQuery(this).parents("form").serialize()
      11.     });
      12.     return false;
      13. });
      $('body').on('click','#add-button-5',function(){
           jQuery.ajax({
                'success': function(data) { 
                              $("td.CSC318H1").text("CSC318H1"); 
                              $("#modal-5 a.btn[data-dismiss]").click();
                },
                'type':'POST',
                'url':'/course/add/5',
                'cache':false,
                'data':jQuery(this).parents("form").serialize()
          });
          return false;
      });

      You haven’t posted your PHP code, but I can tell you that you have to make it change it like the following, in order to take into account that data

      1. $('body').on('click','#add-button-5',function(){
      2.      jQuery.ajax({
      3.           'success': function(data) {
      4.                         $("td.CSC318H1").text("CSC318H1");
      5.                         //here, add the following line
      6.                         $('#enrolled-table .items').append('<b>Course Code:</b><a href="/course/5" rel="nofollow">CSC318H1</a><a href="/course/5" rel="nofollow">More Information</a>            ');
      7.                         //end of added line
      8.                         $("#modal-5 a.btn[data-dismiss]").click();
      9.           },
      10.           'type':'POST',
      11.           'url':'/course/add/5',
      12.           'cache':false,
      13.           'data':jQuery(this).parents("form").serialize()
      14.     });
      15.     return false;
      16. });
      $('body').on('click','#add-button-5',function(){
           jQuery.ajax({
                'success': function(data) { 
                              $("td.CSC318H1").text("CSC318H1"); 
                              //here, add the following line
                              $('#enrolled-table .items').append('<b>Course Code:</b><a href="/course/5" rel="nofollow">CSC318H1</a><a href="/course/5" rel="nofollow">More Information</a>            ');
                              //end of added line
                              $("#modal-5 a.btn[data-dismiss]").click();
                },
                'type':'POST',
                'url':'/course/add/5',
                'cache':false,
                'data':jQuery(this).parents("form").serialize()
          });
          return false;
      });

      By bennouna · 431 days 12 hours ago

      Questions: 0 Accepted: 0 ( 0% ) | Reputation: 0

    2. 0 votes

      The added line has been stripped from any div tag. Here’s correct code (in fact, I duplicated what you can see in the source after you refresh the page, I got it that’s the desired UI behavior — of course you can use fadeIn() to have a nicer effect)

      1. //here, add the following line
      2. $('#enrolled-table .items').append('<div class="row-fluid"><div class="well span8"><b>Course Code:</b><a href="/course/5">CSC318H1</a><a id="info-button-5" class="more-info btn" href="/course/5">More Information</a>        <input id="drop-button-5" class="drop btn" type="submit" name="yt12" value="Drop Course">    </div></div>');
      3. //end of added line
      //here, add the following line
      $('#enrolled-table .items').append('<div class="row-fluid"><div class="well span8"><b>Course Code:</b><a href="/course/5">CSC318H1</a><a id="info-button-5" class="more-info btn" href="/course/5">More Information</a>        <input id="drop-button-5" class="drop btn" type="submit" name="yt12" value="Drop Course">    </div></div>');
      //end of added line

      By bennouna · 431 days 11 hours 55 mins ago

      Questions: 0 Accepted: 0 ( 0% ) | Reputation: 0

      • Thank you for your help, Bennouna. I am however trying a different approach (Ajax), even though its quite tricky, I've managed to get most of the functionalities to work, but I can't get the courses to appear on the enrolled courses ListView without refreshing the page (manually, by F5). In fact, I'd really appreciate it if someone could point me to the right direction as to how to make it work even if it were to reload the page by itself. I've added a 1-bit status field into my Courses table, to classify the courses as Not-Enrolled, Enrolled, Waitlisted, or On-Preview. I update that field in the "Course" model class, whenever an "Add Course", "Drop Course", or "Preview Course" button is clicked. Then I get stuck at rendering (or refreshing) the view to show the changes.

        By blackrobe89 · 431 days 8 hours 54 mins ago

    3. 0 votes

      Well my first answer hasn’t made it (yet?) through the system, and I think you’ve seen only the second.

      Anyway here’s another try
      <<<
      Hello. I think you have to use ajax’s update mechanism that would be fired when your modal is closed. In order to feed your updated data, you have to echo something as a result of your modal submit action.
      After seeing your resulting HTML code, it should go more or less like this:

      When you click on “More info” on the CSC318H1 course for instance, a modal is displayed with an “Add to Courses” submit button, which has the “add-button-5″ id
      I can see (source code) the following javascript (jQuery) code attached to that button. You have to add the above line.

      1. $('body').on('click','#add-button-5',function(){
      2.      jQuery.ajax({
      3.           'success': function(data) {
      4.                         $("td.CSC318H1").text("CSC318H1");
      5.                         // add the line in the previous comment here
      6.                         $("#modal-5 a.btn[data-dismiss]").click();
      7.           },
      8.           'type':'POST',
      9.           'url':'/course/add/5',
      10.           'cache':false,
      11.           'data':jQuery(this).parents("form").serialize()
      12.     });
      13.     return false;
      14. });
      $('body').on('click','#add-button-5',function(){
           jQuery.ajax({
                'success': function(data) {
                              $("td.CSC318H1").text("CSC318H1");
                              // add the line in the previous comment here
                              $("#modal-5 a.btn[data-dismiss]").click();
                },
                'type':'POST',
                'url':'/course/add/5',
                'cache':false,
                'data':jQuery(this).parents("form").serialize()
          });
          return false;
      });

      You haven’t posted your PHP code, but I can tell you that you have to make it change it like above, in order to take into account that data.

      By bennouna · 431 days 3 hours 31 mins ago

      Questions: 0 Accepted: 0 ( 0% ) | Reputation: 0

      • This is great. That's actually exactly what I was trying to do initially, (appending the whole div to the ListView), however this seemed to me like a workaround from the proper Yii way. I think my mistake was stupidly forgetting to place a dot in the jQuery selector ".items". Anyway here are the Controller actions for adding/dropping/previewing: public function actionTimetable2() { $model = new Course('search'); $model->unsetAttributes(); if (isset($_GET['Course'])) $model->attributes = $_GET['Course']; $dataProvider = new CActiveDataProvider('Course'); $this->render('timetable2', array( 'model' => $model, 'dataProvider' => $dataProvider, )); } public function actionAdd($id) { $model = $this->loadModel($id); if ($model->prereqs === '1') { $model->status = '1'; } else { $model->status = '4'; } if ($model->save()) { if ($model->status === '1') { Yii::app()->user->setFlash('success', $model->course_code . ' was successfully added to your registered courses list.'); } else if ($model->status === '4') { Yii::app()->user->setFlash('warning', $model->course_code . ' was added to your courses list, however not all prerequisites are satisfied.'); } // Source of the problem perhaps: $this->render('timetable2', array('model' => $model, 'dataProvider' => $model->searchStatus(1))); } } public function actionDrop($id) { $model = $this->loadModel($id); $model->status = '0'; if ($model->save()) { Yii::app()->user->setFlash('success', $model->course_code . ' was successfully dropped from your registered courses list.'); } // Source of the problem perhaps: $this->render('timetable2', array('model' => $model, 'dataProvider' => $model->searchStatus(1))); } public function actionPreview($id) { // Still working on this one. $model = $this->loadModel($id); } In the Course model: public function searchStatus($status = 0) { $criteria = new CDbCriteria; $criteria->compare('status', $status); return new CActiveDataProvider($this, array('criteria' => $criteria)); } Preview and Add Buttons: $data->id)), // ajaxOptions array( 'success' => 'js: function(data) { $("td.' . $data->course_code . '").text("' . $data->course_code . '"); }', ), // htmlOptions array( 'id' => 'preview-button-' . $data->id, 'class' => 'preview btn', ) ); ?> $data->id)), // ajaxOptions array( 'success' => 'js: function(data) { $("td.' . $data->course_code . '").text("' . $data->course_code . '"); $("#modal-' . $data->id . ' a.btn[data-dismiss]").click(); }', ), // htmlOptions array( 'id' => 'add-button-' . $data->id, 'class' => 'add btn', ) ); ?> 'btn', 'data-dismiss' => 'modal' ) ); ?> The More Information and Drop buttons: $data->id), // htmlOptions array( 'id' => 'info-button-' . $data->id, 'class' => 'more-info btn', ) ); ?> $data->id)), // ajaxOptions array( 'success' => 'js: function(data) { $("td.' . $data->course_code . '").text(""); console.log(data); $(this).parent().remove(); document.reload(); }', ), // htmlOptions array( 'id' => 'drop-button-' . $data->id, 'class' => 'drop btn', ) ); ?> Finally, the three ListViews Currently Enrolled widget('BootListView', array( 'id' => 'enrolled-table', 'dataProvider' => $model->searchStatus(1), 'itemView' => '_item_list2', 'summaryText' => '', 'emptyText' => '', )); ?> Previewing in Timetable widget('BootListView', array( 'id' => 'preview-table', 'dataProvider' => $model->searchStatus(2), 'itemView' => '_item_list2', 'summaryText' => '', 'emptyText' => '', )); ?> Waitlisted Courses widget('BootListView', array( 'id' => 'waitlist-table', 'dataProvider' => $model->searchStatus(3), 'itemView' => '_item_list2', 'summaryText' => '', 'emptyText' => '' )); ?> The Flash messages are also not working, it might be because I'm not really using the User model really. Thanks again, really helpful.

        By blackrobe89 · 431 days 1 hour 49 mins ago

    4. 0 votes

      Controller Actions:

      1. public function actionTimetable2() {
      2.         $model = new Course('search');
      3.         $model->unsetAttributes();
      4.  
      5.         if (isset($_GET['Course']))
      6.             $model->attributes = $_GET['Course'];
      7.  
      8.         $dataProvider = new CActiveDataProvider('Course');
      9.         $this->render('timetable2', array(
      10.             'model' => $model,
      11.             'dataProvider' => $dataProvider,
      12.         ));
      13. }
      14.  
      15.  
      16.  
      17. public function actionAdd($id) {
      18.  
      19.         $model = $this->loadModel($id);
      20.  
      21.         if ($model->prereqs === '1') {
      22.             $model->status = '1';
      23.         } else {
      24.             $model->status = '4';
      25.         }
      26.         if ($model->save()) {
      27.             if ($model->status === '1') {
      28.                 Yii::app()->user->setFlash('success', $model->course_code . ' was successfully added to your registered courses list.');
      29.             } else if ($model->status === '4') {
      30.                 Yii::app()->user->setFlash('warning', $model->course_code . ' was added to your courses list, however not all prerequisites are satisfied.');
      31.             }
      32.             $this->render('timetable2', array('model' => $model, 'dataProvider' => $model->searchStatus(1)));
      33.         }
      34. }
      35.  
      36.  
      37.  
      38. public function actionDrop($id) {
      39.  
      40.         $model = $this->loadModel($id);
      41.  
      42.         $model->status = '0';
      43.         if ($model->save()) {
      44.             Yii::app()->user->setFlash('success', $model->course_code . ' was successfully dropped from your registered courses list.');
      45.         }
      46.         $this->render('timetable2', array('model' => $model, 'dataProvider' => $model->searchStatus(1)));
      47. }
      48.  
      49.  
      50.  
      51. public function actionPreview($id) {
      52.         $model = $this->loadModel($id);
      53. }
      public function actionTimetable2() {
              $model = new Course('search');
              $model->unsetAttributes();
      
              if (isset($_GET['Course']))
                  $model->attributes = $_GET['Course'];
      
              $dataProvider = new CActiveDataProvider('Course');
              $this->render('timetable2', array(
                  'model' => $model,
                  'dataProvider' => $dataProvider,
              ));
      }
      
      
      
      public function actionAdd($id) {
      
              $model = $this->loadModel($id);
      
              if ($model->prereqs === '1') {
                  $model->status = '1';
              } else {
                  $model->status = '4';
              }
              if ($model->save()) {
                  if ($model->status === '1') {
                      Yii::app()->user->setFlash('success', $model->course_code . ' was successfully added to your registered courses list.');
                  } else if ($model->status === '4') {
                      Yii::app()->user->setFlash('warning', $model->course_code . ' was added to your courses list, however not all prerequisites are satisfied.');
                  }
                  $this->render('timetable2', array('model' => $model, 'dataProvider' => $model->searchStatus(1)));
              }
      }
      
      
      
      public function actionDrop($id) {
      
              $model = $this->loadModel($id);
      
              $model->status = '0';
              if ($model->save()) {
                  Yii::app()->user->setFlash('success', $model->course_code . ' was successfully dropped from your registered courses list.');
              }
              $this->render('timetable2', array('model' => $model, 'dataProvider' => $model->searchStatus(1)));
      }
      
      
      
      public function actionPreview($id) {
              $model = $this->loadModel($id);
      }

      By blackrobe89 · 431 days 1 hour 38 mins ago

      Questions: 1 Accepted: 0 ( 0% ) | Reputation: 0

    5. 0 votes

      More Information and Add buttons

      1.         <?php
      2.         // More Information Link
      3.         echo CHtml::link('More Information',
      4.             // Url
      5.             array('view', 'id' => $data->id),
      6.             // htmlOptions
      7.             array(
      8.                 'id' => 'info-button-' . $data->id,
      9.                 'class' => 'more-info btn',
      10.             )
      11.         );
      12.         ?>
      13.         <?php
      14.         // Drop Course Ajax Submit Button
      15.         echo CHtml::ajaxSubmitButton('Drop Course',
      16.             // Url
      17.             CHtml::normalizeUrl(array('course/drop', 'id' => $data->id)),
      18.             // ajaxOptions
      19.             array(
      20.             'success' => 'js: function(data) {
      21.                            $("td.' . $data->course_code . '").text("");
      22.                            console.log(data);
      23.                            $(this).parent().remove();
      24.                            document.reload();
      25.                         }',
      26.             ),
      27.             // htmlOptions
      28.             array(
      29.                 'id' => 'drop-button-' . $data->id,
      30.                 'class' => 'drop btn',
      31.             )
      32.         );
      33.         ?>
              <?php
              // More Information Link
              echo CHtml::link('More Information', 
                  // Url
                  array('view', 'id' => $data->id), 
                  // htmlOptions
                  array(
                      'id' => 'info-button-' . $data->id,
                      'class' => 'more-info btn',
                  )
              );
              ?>
              <?php
              // Drop Course Ajax Submit Button
              echo CHtml::ajaxSubmitButton('Drop Course', 
                  // Url
                  CHtml::normalizeUrl(array('course/drop', 'id' => $data->id)), 
                  // ajaxOptions
                  array(
                  'success' => 'js: function(data) { 
                                  $("td.' . $data->course_code . '").text("");
                                  console.log(data);
                                  $(this).parent().remove();
                                  document.reload();
                               }',
                  ), 
                  // htmlOptions
                  array(
                      'id' => 'drop-button-' . $data->id,
                      'class' => 'drop btn',
                  )
              );
              ?>

      By blackrobe89 · 431 days 1 hour 35 mins ago

      Questions: 1 Accepted: 0 ( 0% ) | Reputation: 0

    6. 0 votes

      Preview and Add buttons

      1.     <?php
      2.     // Preview Course Ajax Submit Button
      3.     echo CHtml::ajaxSubmitButton('Preview Course in Calendar',
      4.         // Url
      5.         CHtml::normalizeUrl(array('course/preview', 'id' => $data->id)),
      6.         // ajaxOptions
      7.         array(            
      8.             'success' => 'js: function(data) {
      9.                                $("td.' . $data->course_code . '").text("' . $data->course_code . '");
      10.                          }',
      11.         ),
      12.         // htmlOptions
      13.         array(
      14.             'id' => 'preview-button-' . $data->id,
      15.             'class' => 'preview btn',
      16.         )
      17.     );
      18.     ?>
      19.  
      20.     <?php
      21.     // Add Course Ajax Submit Button
      22.     echo CHtml::ajaxSubmitButton('Add to Courses',
      23.         // Url
      24.         CHtml::normalizeUrl(array('course/add', 'id' => $data->id)),
      25.         // ajaxOptions
      26.         array(
      27.             'success' => 'js: function(data) {
      28.                                $("td.' . $data->course_code . '").text("' . $data->course_code . '");
      29.                                $("#modal-' . $data->id . ' a.btn[data-dismiss]").click();
      30.                          }',
      31.         ),
      32.         // htmlOptions
      33.         array(
      34.             'id' => 'add-button-' . $data->id,
      35.             'class' => 'add btn',
      36.         )
      37.     );
      38.     ?>
      39.  
      40.     <?php
      41.     echo CHtml::link('Close', '#', array(
      42.         'class' => 'btn',
      43.         'data-dismiss' => 'modal'
      44.         )
      45.     );
      46.     ?>
          <?php
          // Preview Course Ajax Submit Button
          echo CHtml::ajaxSubmitButton('Preview Course in Calendar',
              // Url
              CHtml::normalizeUrl(array('course/preview', 'id' => $data->id)),
              // ajaxOptions
              array(            
                  'success' => 'js: function(data) { 
                                      $("td.' . $data->course_code . '").text("' . $data->course_code . '"); 
                                }',
              ),
              // htmlOptions
              array(
                  'id' => 'preview-button-' . $data->id,
                  'class' => 'preview btn',
              )
          );
          ?>
      
          <?php
          // Add Course Ajax Submit Button
          echo CHtml::ajaxSubmitButton('Add to Courses',
              // Url
              CHtml::normalizeUrl(array('course/add', 'id' => $data->id)),
              // ajaxOptions
              array(
                  'success' => 'js: function(data) { 
                                      $("td.' . $data->course_code . '").text("' . $data->course_code . '"); 
                                      $("#modal-' . $data->id . ' a.btn[data-dismiss]").click();
                                }',
              ),
              // htmlOptions
              array(
                  'id' => 'add-button-' . $data->id,
                  'class' => 'add btn',
              )
          );
          ?>
      
          <?php
          echo CHtml::link('Close', '#', array(
              'class' => 'btn',
              'data-dismiss' => 'modal'
              )
          );
          ?>

      By blackrobe89 · 431 days 1 hour 34 mins ago

      Questions: 1 Accepted: 0 ( 0% ) | Reputation: 0

    7. 0 votes

      Well you should have guessed where to put it I guess?

      1.    <?php
      2.     // Add Course Ajax Submit Button
      3.     echo CHtml::ajaxSubmitButton('Add to Courses',
      4.         // Url
      5.         CHtml::normalizeUrl(array('course/add', 'id' => $data->id)),
      6.         // ajaxOptions
      7.         array(
      8.             'success' => 'js: function(data) {
      9.                               $("td.' . $data->course_code . '").text("' . $data->course_code . '");
      10.                               // HERE
      11.                               $("#enrolled-table .items").append("<div class=\"row-fluid\"><div class=\"well span8\"><b>Course Code:</b><a href=\"/course/' . $data->id . '\">' . $data->course_code . '</a><a id=\"info-button-' . $data->id . '\" class=\"more-info btn\" href=\"/course/' . $data->id . '\">More Information</a>        <input id=\"drop-button-' . $data->id . '\" class=\"drop btn\" type=\"submit\" name=\"yt12\" value=\"Drop Course\">    </div></div>");
      12.                               $("#modal-' . $data->id . ' a.btn[data-dismiss]").click();
      13.                         }',
      14.         ),
      15.         // htmlOptions
      16.         array(
      17.             'id' => 'add-button-' . $data->id,
      18.             'class' => 'add btn',
      19.         )
      20.     );
         <?php
          // Add Course Ajax Submit Button
          echo CHtml::ajaxSubmitButton('Add to Courses',
              // Url
              CHtml::normalizeUrl(array('course/add', 'id' => $data->id)),
              // ajaxOptions
              array(
                  'success' => 'js: function(data) {
                                     $("td.' . $data->course_code . '").text("' . $data->course_code . '");
                                     // HERE
                                     $("#enrolled-table .items").append("<div class=\"row-fluid\"><div class=\"well span8\"><b>Course Code:</b><a href=\"/course/' . $data->id . '\">' . $data->course_code . '</a><a id=\"info-button-' . $data->id . '\" class=\"more-info btn\" href=\"/course/' . $data->id . '\">More Information</a>        <input id=\"drop-button-' . $data->id . '\" class=\"drop btn\" type=\"submit\" name=\"yt12\" value=\"Drop Course\">    </div></div>");
                                     $("#modal-' . $data->id . ' a.btn[data-dismiss]").click();
                               }',
              ),
              // htmlOptions
              array(
                  'id' => 'add-button-' . $data->id,
                  'class' => 'add btn',
              )
          );

      By bennouna · 430 days 23 hours 41 mins ago

      Questions: 0 Accepted: 0 ( 0% ) | Reputation: 0

    © YiiAnswers.com 2011. All rights reserved.

    Switch to our mobile site