将所选项目发布到表格 [英] Post selected items to table

查看:89
本文介绍了将所选项目发布到表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索结果列表。每个项目都有一个复选框。



这里是显示数据的代码:

 < section class =grid_12> 
< div class =block-border>
< form class =block-content formid =table_formmethod =postaction =>
< h1>可排序表格< / h1>

< table id =mylistdata-campaignId =<?= $ campaignid?> class =table sortable no-margincellspacing =0width =100%>

< thead>
< tr>
< th class =black-cell>< span class =loading>< / span>< / th>
< th scope =col>
< span class =column-sort>
< a href =#title =排序class =sort-up>< / a>
< a href =#title =排序class =sort-down>< / a>
< / span>
名称
< / th>
< th scope =col>客户端< / th>
< th scope =col>
< span class =column-sort>
< a href =#title =排序class =sort-up>< / a>
< a href =#title =排序class =sort-down>< / a>
< / span>
家庭电话
< / th>
< th scope =col>
< span class =column-sort>
< a href =#title =排序class =sort-up>< / a>
< a href =#title =排序class =sort-down>< / a>
< / span>
电子邮件
< / th>
< th scope =col>
< span class =column-sort>
< a href =#title =排序class =sort-up>< / a>
< a href =#title =排序class =sort-down>< / a>
< / span>
Created On
< / th>
< th scope =colclass =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 =checkboxname =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->电子邮件?>< / td>
< td><?= $ row-> created?>< / td>
< td class =table-actions>
< a href =/ client / view /<?= $ row-> id?> title =View / Editclass =with-tip>< img src =/ images / icons / fugue / pencil.pngwidth =16height =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.pngwidth =16height =16class =picto>
< a href =javascript:toggleChecked('checked')class =button>全选< / a>
< a href =javascript:toggleChecked('')class =button>取消选择全部< / a>
< span class =sep>< / span>
< button id =post_diff_table> POST已选择< / 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);
} $ b});
// - >
< / script>

< script type =text / javascript>
$(function(){
$('#post_diff_table')。click(function(){
var table = $('table#mylist');
var campagnid = table.data('campaignId');
//检查的客户端ID数组
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;}
//调用控制器
$ .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);出口();

$ 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',$数据);

$ newid = $ this-> db-> insert_id();
return $ newid;
}


解决方案

$ id =mylistdata-campaignId =$ id=midist,则$ campaignid为campaignid,将mylist更改为任何所需的值):

  <?= $ campaignid?> class =table sortable no-margincellspacing =0width =100%> 

在您的循环中设置客户端数据

 <?php foreach($ r-> result()as $ row):?> 
< tr data-clientid =$ row-> id>
< td class =th table-check-cell>
< input type =checkboxname =selected []class =checkbox>
< / td>
<!--- other td here - >
< / tr>
<?php endforeach;?>
< / table>
<! - 按钮来发布所有选定的项目 - >
< button id =post_diff_table> POST已选择< / button>

javascript:

 code> $(function(){
$('#post_diff_table')。click(function(){
var table = $('table#mylist');
var campagnid = table.data('campaignId');
//检查的客户端数组数组
var checked_clients = $ .map(table.find('tr td.table-check-cell input: ),function(e){
return $(e).parents('tr')。data('clientid');
});
if(checked_clients.length === 0){return false;}
//调用控制器
$ .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插入代码此处
或调用$ this-> campaign_model-> add_user_to_campaign为每个客户端
* /
}
}

答案不是复制粘贴代码,因为我有不知道你的项目内部,但你应该得到的想法。




对其他代码的建议:不要在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& rightonclick =javascript:delete_subscriber(\''+ data.id +'\')> delete< / span>< / li>');



在js中组成元素,添加事件处理程序等等。你的方式使你使用更多的全局函数,使代码不可读。


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:

        <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>

Here is all the current javascript in the above page.

<? $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>

Here is the code in ajax controller.

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));
  }
  }

Here is the add_user_to_campaign model.

function add_user_to_campaign($data)
{
    $this->db->insert('campaign_to_client',$data);

    $newid = $this->db->insert_id();
    return $newid;
}  

解决方案

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.

这篇关于将所选项目发布到表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆