Post Selected Items To Table
I have a results list produced by a search. Each item has a check box. What I want to do is post the selected items to a different table. Here is the code that displays the data:
Solution 1:
In table definition (I assume that $campaignid is campaignid, change mylist to whatever you want):
<table id="mylist" data-campaignId="<?=$campaignid?>" class="table sortable no-margin" cellspacing="0" width="100%">
In your loop set data for client
<?php foreach($r->result() as $row) : ?>
<tr data-clientid="$row->id">
<td class="th table-check-cell">
<input type="checkbox" name="selected[]" class="checkbox">
</td>
<!---other td here -->
</tr>
<?php endforeach;?>
</table>
<!--button to post all selected items -->
<button id="post_diff_table">POST selected</button>
javascript:
$(function() {
$('#post_diff_table').click(function() {
var table = $('table#mylist');
var campagnid = table.data('campaignId');
//Array of checked client ids
var checked_clients = $.map(table.find('tr td.table-check-cell input:checked'), function(e) {
return $(e).parents('tr').data('clientid');
});
if (checked_clients.length === 0) {return false;}
//call the controller
$.post('/ajaxmodals/campaign_post_to_other_table', {
campagnid: campagnid,
clients: checked_clients
}, function(data) {
//process response
},"json");
});});
in controller:
function campaign_post_to_other_table()
{
$campagnid=$this->input->post('campagnid');
/* @var Array */
$clients = $this->input->post('clients');
foreach ($clients as $client_id) {
/* sql insertion code here
or call $this->campaign_model->add_user_to_campaign for each clientid
*/
}
}
The answer is not the copy-pastable code, as i have no idea of your project internals, but you should get the idea.
the suggestions on other code: do not do this in js:
$.post('/ajaxmodals/campaign_subscriber_add', {
clientid : cid,
campaignid : <?=$r->id?>
get the campaignid from table data as in my sample
do not do this:
$("#subscriberList").append('<li id="'+data.id+'"><a href="/client/view/'+data.clientid+'">'+data.name+'</a><span style="display: inline; float: right" onclick="javascript:delete_subscriber(\''+data.id+'\')">delete</span></li>');
compose element in js, add event handlers, etc. your way make you to use to much global functions and make the code unreadable.
Post a Comment for "Post Selected Items To Table"