简体   繁体   English

如何在 codeigniter 中获取选中的复选框表行值

[英]how to get checked checkbox table row value in codeigniter

VIEW PAGE查看页面

<table>
<thead>
    <tr>
        <th><input type="checkbox" checked="checked" class="checkAll"  name="checkAll" /></th>
        <th>#</th>
        <th>Beneficiary Name</th>
        <th>Stipendiary Type</th>
        <th class="text-right box">Bonus ₹</th>
        <th class="text-right">Stipendiary ₹</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td align="center">
            <input type="checkbox" checked="checked" class="chkclass " name="bene_id[]" value="1" /><input type="hidden" name="amount[]" value="500" tabindex ="-1" />
        </td>
        <td>1</td>
        <td>Jeinbai Nesamony</td>
        <td>Poor Pension</td>
        <td class="text-right box" id="hideshow">
            <input type="text" name="bonus[]" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
        </td>
        <td class="text-right wagein">500.00</td>
    </tr>
    <tr>
        <td align="center">
            <input type="checkbox" checked="checked" class="chkclass " name="bene_id[]" value="2" /><input type="hidden" name="amount[]" value="400" tabindex ="-1" />
        </td>
        <td>2</td>
        <td>Chellammal Kochimoni</td>
        <td>Poor Aid</td>
        <td class="text-right box" id="hideshow">
            <input type="text" name="bonus[]" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
        </td>
        <td class="text-right wagein">400.00</td>
    </tr>
    <tr>
        <td align="center">
            <input type="checkbox" checked="checked" class="chkclass " name="bene_id[]" value="3" /><input type="hidden" name="amount[]" value="400" tabindex ="-1" />
        </td>
        <td>3</td>
        <td>Thasammal Thangaiah</td>
        <td>Poor Aid</td>
        <td class="text-right box" id="hideshow">
            <input type="text" name="bonus[]" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
        </td>
        <td class="text-right wagein">400.00</td>
    </tr>
    <tr>
        <td align="center">
            <input type="checkbox" checked="checked" class="chkclass " name="bene_id[]" value="4" /><input type="hidden" name="amount[]" value="400" tabindex ="-1" />
        </td>
        <td>4</td>
        <td>Roselet</td>
        <td>Poor Aid</td>
        <td class="text-right box" id="hideshow">
            <input type="text" name="bonus[]" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
        </td>
        <td class="text-right wagein">400.00</td>
    </tr>
    <tr>
        <td align="center">
            <input type="checkbox" checked="checked" class="chkclass " name="bene_id[]" value="5" /><input type="hidden" name="amount[]" value="400" tabindex ="-1" />
        </td>
        <td>5</td>
        <td>Kamalam Chellam R.</td>
        <td>Poor Aid</td>
        <td class="text-right box" id="hideshow">
            <input type="text" name="bonus[]" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
        </td>
        <td class="text-right wagein">400.00</td>
    </tr>
</tbody>

在此处输入图像描述

MY REQUIREMENT我的要求

I want to save bellowed data's to table我想将以下数据保存到表中
1. bene_id 1. bene_id
2. Bonus ₹ 2. 奖金 ₹
3. Stipendiary ₹ 3. 津贴 ₹

I've fetch this table data form existing Beneficiary Table.我已经从现有的受益人表中获取了这个表数据。 So Bene_id and Stipendiary ₹ value get from that tabel.所以 Bene_id 和 Stipendiary ₹ 值来自该表。 Bonus ₹ will be an input.奖金₹将是一个输入。
Now i want to save table data to the payment table.现在我想将表数据保存到付款表。
I'm trying to post the value by array.我正在尝试按数组发布值。 it's working fine.它工作正常。
now i've an issue with the check box.现在我的复选框有问题。 i want to neglect the row value that unchecked.我想忽略未选中的行值。 That means i want row value which was checkbox: checked这意味着我想要复选框的行值:选中

i'm expecting jquery for passing checkbox: checked row value to hidden input array.我期待 jquery 用于传递复选框:检查行值到隐藏输入数组。

As i told you in the comments section, you can use normal HTML forms to submit to the action method on your controller, but you need to modify your form a little bit, this is the easiest solution.正如我在评论部分告诉你的那样,你可以使用普通的 HTML forms 提交到 controller 上的操作方法,但你需要稍微修改你的表单,这是最简单的解决方案。

Despite of option one simplicity i decided to give you another approach to solve this problem, so first look at the code of HTML and JavaScript:尽管选项一个简单,我还是决定给你另一种方法来解决这个问题,所以首先看看 HTML 和 JavaScript 的代码:

<table>
<thead>
   <tr>
     <th><input type="checkbox" checked="checked" class="checkAll"  name="checkAll" /></th>
     <th>#</th>
     <th>Beneficiary Name</th>
     <th>Stipendiary Type</th>
     <th class="text-right box">Bonus ₹</th>
     <th class="text-right">Stipendiary ₹</th>
   </tr>
</thead>
<tbody id="details">
  <tr>
    <td align="center">
        <input type="checkbox" checked="checked" class="chkclass " id="bene_id" value="1" />
    </td>
    <td>1</td>
    <td>Jeinbai Nesamony</td>
    <td>Poor Pension</td>
    <td class="text-right box" id="hideshow">
    <input type="text" name="bonus" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
    </td>
    <td class="text-right wagein" id="amount">500.00</td>
</tr>
<tr>
    <td align="center">
        <input type="checkbox" checked="checked" class="chkclass " id="bene_id" value="2" />
    </td>
    <td>2</td>
    <td>Chellammal Kochimoni</td>
    <td>Poor Aid</td>
    <td class="text-right box" id="hideshow">
        <input type="text" name="bonus" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
    </td>
    <td class="text-right wagein" id="amount" >400.00</td>
</tr>
<tr>
    <td align="center">
        <input type="checkbox" checked="checked" class="chkclass " id="bene_id" value="3" />
    </td>
    <td>3</td>
    <td>Thasammal Thangaiah</td>
    <td>Poor Aid</td>
    <td class="text-right box" id="hideshow">
        <input type="text" name="bonus" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
    </td>
    <td class="text-right wagein" id="amount" >400.00</td>
</tr>
<tr>
    <td align="center">
        <input type="checkbox" checked="checked" class="chkclass " id="bene_id" value="4" />
    </td>
    <td>4</td>
    <td>Roselet</td>
    <td>Poor Aid</td>
    <td class="text-right box" id="hideshow">
        <input type="text" name="bonus" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
    </td>
    <td class="text-right wagein" id="amount">400.00</td>
</tr>
<tr>
    <td align="center">
        <input type="checkbox" checked="checked" class="chkclass " id="bene_id" value="5" />
    </td>
    <td>5</td>
    <td>Kamalam Chellam R.</td>
    <td>Poor Aid</td>
    <td class="text-right box" id="hideshow">
        <input type="text" id="bonus" value="" class="tbl-input-cus bonus" tabindex ="1" />
    </td>
    <td class="text-right wagein" id="amount">400.00</td>
</tr>
</tbody>
</table>

<button id="submit">Submit</button>

<script type="text/javascript" src="<?= base_url(assets/js/jquery.min.js) ?>"></script>
<script type="text/javascript">
  function jsonify(){

    var rows = $('#details tr');
    var a = [];
    rows.each(function(){

        if($(this).find('#bene_id').is(':checked'))
        {
           var bene_id       = $(this).find('#bene_id').val();
           var stipendiary   = $(this).find('#amount').html();
           var bonus         = $(this).find('#bonus').val();

           var x = {
                bene_id:bene_id,
                stipendiary:stipendiary,
                bonus:bonus
            };
            a.push(x);
        }
    });
    var c = JSON.stringify(a);
    return c;
}

 $(function(){

   $('#submit').click(function(){
       $data = jsonify();

       $.ajax({
          type:'POST',
          url:'<?= base_url('controller/method_name') ?>',
          data:{details:data},
          success:function(response)
          {
            //if you data save successfuly, do sth here..
          }
       });
    });

 });

The following code is a PHP code of the action method on the specified controller:以下代码是指定controller上的action方法的PHP代码:

   public function method_name()
   {
         $details  = json_decode($this->input->post('details'));

         foreach($details as $det ){

            $bene_id     = $det->bene_id;
            $stipendiary = $det->stipendiary;
            $bonus       = $det->bonus;

            // your logic goes here
         }
   }

In this solution i didn't considered the validation and security issues, because i wanted to make it simple, so before you put it in your production server you must deal with these issues.在这个解决方案中,我没有考虑验证和安全问题,因为我想让它变得简单,所以在将它放入生产服务器之前,你必须处理这些问题。

I hope it helps.我希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM