我有一个搜索结果列表。每个项目都有一个复选框。我想要做的是将所选项目发布到不同的表格。
以下是显示数据的代码:
<section class="grid_12">
<div class="block-border">
<form class="block-content form" id="table_form" method="post" action="">
<h1>Sortable table</h1>
<table id="mylist" data-campaignId="<?=$campaignid?>" class="table sortable no-margin" cellspacing="0" width="100%">
<thead>
<tr>
<th class="black-cell"><span class="loading"></span></th>
<th scope="col">
<span class="column-sort">
<a href="#" title="Sort up" class="sort-up"></a>
<a href="#" title="Sort down" class="sort-down"></a>
</span>
Name
</th>
<th scope="col">Client Of</th>
<th scope="col">
<span class="column-sort">
<a href="#" title="Sort up" class="sort-up"></a>
<a href="#" title="Sort down" class="sort-down"></a>
</span>
Home Phone
</th>
<th scope="col">
<span class="column-sort">
<a href="#" title="Sort up" class="sort-up"></a>
<a href="#" title="Sort down" class="sort-down"></a>
</span>
Email
</th>
<th scope="col">
<span class="column-sort">
<a href="#" title="Sort up" class="sort-up"></a>
<a href="#" title="Sort down" class="sort-down"></a>
</span>
Created On
</th>
<th scope="col" class="table-actions">Actions</th>
</tr>
</thead>
<tbody>
<?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>
<td><a href="/client/view/<?=$row->id?>"><?=$row->firstname?> <?=$row->lastname?></a></td>
<td><ul class="keywords">
<li><a href="#"><?=$row->clientof?></a></li>
<li class="orange-keyword"><a href="#"><?=$row->status?></a></li>
</ul></td>
<td><?=$row->homephone?></td>
<td><?=$row->email?></td>
<td><?=$row->created?></td>
<td class="table-actions">
<a href="/client/view/<?=$row->id?>" title="View / Edit" class="with-tip"><img src="/images/icons/fugue/pencil.png" width="16" height="16"></a>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<div class="clear"></div>
<div class="block-footer">
<img src="images/icons/fugue/arrow-curve-000-left.png" width="16" height="16" class="picto">
<a href="javascript:toggleChecked('checked')" class="button">Select All</a>
<a href="javascript:toggleChecked('')" class="button">Unselect All</a>
<span class="sep"></span>
<button id="post_diff_table">POST selected</button>
</div>
</form></div>
</section>
以上是上页中的所有当前javascript。
<? $campaignid = $this->uri->segment(4); ?>
<script type="text/javascript">
<!--
$(document).ready(function(){
toggleChecked = function(status)
{
alert('trigger');
$(".checkbox").each( function() {
$(this).attr("checked",status);
});
}
});
//-->
</script>
<script type="text/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");
});});
</script>
这是ajax控制器中的代码。
function campaign_post_to_other_table()
{
$campagnid=$this->input->post('campagnid');
/* @var Array */
$clients = $this->input->post('clients');
foreach ($clients as $client_id) {
$clientid = $this->input->post('clientid');
$submit['clientid'] = $clientid;
$submit['campaignid'] = $this->input->post('campaignid');
$submit['creationdate'] = date('Y-m-d H:i:s');
$submit['createdby'] = $this->session->userdata('uid');
//print_r($submit); exit();
$newid = $this->campaign_model->add_user_to_campaign($submit);
$cname = $this->client_model->get_client_name($clientid);
echo json_encode(array("response"=>"Success","name"=>$cname,"id"=>$newid,"clientid"=>$clientid));
}
}
这是add_user_to_campaign模型。
function add_user_to_campaign($data)
{
$this->db->insert('campaign_to_client',$data);
$newid = $this->db->insert_id();
return $newid;
}
答案 0 :(得分:0)
在表格定义中(我假设$ campaignid是campaignid,将mylist更改为您想要的任何内容):
<table id="mylist" data-campaignId="<?=$campaignid?>" class="table sortable no-margin" cellspacing="0" width="100%">
在客户端的循环集数据
中<?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");
});});
控制器中的:
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
*/
}
}
答案不是可复制的代码,因为我不知道你的项目内部,但你应该明白这个想法。
<小时/> <小时/> 关于其他代码的建议:不要在js中执行此操作:
$.post('/ajaxmodals/campaign_subscriber_add', {
clientid : cid,
campaignid : <?=$r->id?>
从我的示例
中获取表数据中的campaignid不要这样做:
$("#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>');
在js中组合元素,添加事件处理程序等等。您的方式使您可以使用许多全局函数并使代码不可读。