Welcome Guest, Not a member yet? Register   Sign In
Best way to set up AJAX/JQuery process?
#1

Hi,

I'm developing an app which must be AJAX enabled. It's not particularly difficult to set up but as I'm pretty new to AJAX in CI I've had a few concerns about duplication.

Currently, as part of the general CRUD processing on each table, the requirement is a view which lists all records in the table - whenever the user clicks an 'Edit' button for any table entry the app uses Jquery/AJAX to access the db for the appropriate record and display it in a modal form which is part of the same view but hidden until the button is clicked.

when the user has updated the record they clck on a 'Save' button which then uses Jquery/AJAX to access a controller/method to update the record in the db and redisplay the part of the view (the table body) showing all the records in the table, including the updated details. There's also a requirement to do a similarthing when adding records.

Currently, the part of the view which shows the details of each db record is below:

Now, I'm thinking that to redisplay just the table body then I'd need the controller to call a model function which gets all the table records and then processes those records by using an almost exact copy of the foreach code that already exists in the view, passes that back to the controller/method which then echoes it back to the JQuery function which can then target the table body and change the .html

That would me duplicating the view code in the model code which I'm not happy about ... as I say I'm pretty new to AJAX in CI so, there has to be a better way, doesn't there?

Code:
<tbody id="staff-table-body">
<?php
foreach ($data as $staff)
{
$detail_id = $staff['staffid'];
?>
<tr>
<td id="<?php echo $staff['staffid'];?>-s-fname"><?php echo $staff['fname'] ?></td>
<td id="s-lname-<?php echo $staff['staffid'];?>"><?php echo $staff['lname'] ?></td>
<td id="s-dept-<?php echo $staff['staffid'];?>"><?php echo $staff['dept'] ?></td>
<td><input id="<?php echo $staff['staffid'];?>-s-edit" name="btn_add" type="submit" class="s-edit btn btn-sm btn-primary" value="Edit" /></td>
<td>
<?php
echo anchor('staff/edit_staff/'.$staff['staffid'], 'Edit');
?>
</td>
<td>
</tr>
<?php
}
?>
</tbody>
Reply


Messages In This Thread
Best way to set up AJAX/JQuery process? - by blackbulldog - 02-12-2015, 05:07 AM



Theme © iAndrew 2016 - Forum software by © MyBB