如何在插入数据时将多行选择的 id 传递到打印页面
Posted
技术标签:
【中文标题】如何在插入数据时将多行选择的 id 传递到打印页面【英文标题】:How can I pass id of multiple row selection to print page at the time of inserting data 【发布时间】:2019-12-07 10:15:51 【问题描述】:我通过选择多个行表数据成功插入数据,但同时我想在打印页面上打印数据。
在上图中,我只选择了 3 条记录,并在插入我希望在打印页面上显示的所选数据时插入到数据库中。
创建页面:
<form>
<table id="pending_collection_table"> </table>
<input type="button" id="allocate" value="allocate" name="allocate">
</form>
<script>
$('#allocate').click(function (event)
event.preventDefault();
var allVals = [];
$('input[name=selectedBilties]:checked').each(function()
allVals.push($(this).val());
);
var formData = new FormData();
var agent = $('#agent').val();
var rec_type = $('#rec_type').val();
formData.append("agent",agent);
formData.append("rec_type",rec_type);
for (var i = 0; i < allVals.length; i++)
formData.append('due_ids[]', allVals[i]);
alertify.confirm('Payment Recovery Allocation', 'Do you want to Allocate ?', function()
$.ajax(
url :"<?php echo base_url();?>crossing/payment_rec_allocation/PaymentRecAllocationController/createPaymentAllocation",
type:"POST",
dataType: "json",
data:formData,
contentType:false,
cache:false,
processData:false,
success: function(data)
if(data.PaymentRecAllocation.form_status=='false')
else if(data.PaymentRecAllocation.form_status=='true')
alertify.confirm('Payment Recovery Allocation', 'Do you want to print ? ', function()
window.location.href =" <?php echo base_url(); ?>crossing/payment_rec_allocation/PaymentRecAllocationController/printCollectionRecPage";
setTimeout(location.reload.bind(location), 2000);
,
function()
location.href="<?php echo base_url(); ?>", 'refresh';
);
);
, function()
);
);
</script>
控制器:
public function createPaymentAllocation()
$bilty_ids = $this->input->post('due_ids');
$biltyCount = count($bilty_ids);
$agent = $this->input->post('agent');
$due_to = $this->input->post('due_to');
for($i = 0; $i < $biltyCount; $i++)
$data = array(
'agent_id' =>$agent,
'pay_dueto' =>$due_to,
'mr_no' =>$bilty_ids[$i],
);
$modelResult = $this->PayRecAllModel->inserPaymentAllocation($data);
if($modelResult)
$data['PaymentRecAllocation'] = array(
'form_status' => 'true',
'form_message' => 'Payment Recovery has been successfully Allocate'
);
else
$data['PaymentRecAllocation'] = array(
'form_status' => 'false',
'form_message' => 'Something went wrong.'
);
echo json_encode($data);
型号:
public function inserPaymentAllocation($data)
if($this->db->insert('payment_rec_allocn', $data))
return true;
else
return false;
现在我的打印功能在controller
public function printCollectionRecPage()
$this->load->view('template/header');
$data= array();
$data['collnR'] = $this->PayRecAllModel->printCollectionRecPage();
$this->load->view('crossing/payment_rec_allocation/printCollectionRecovery',$data);
$this->load->view('template/footer');
打印页面型号:
public function printCollectionRecPage()
$this->db->select('*');
$this->db->from('payment_rec_allocn');
$this->db->join('crossing_cash_memo', 'payment_rec_allocn.mr_no = crossing_cash_memo.mr_no');
$this->db->where('total !=','0');
$query = $this->db->get();
return $query->result();
如何在打印页面中传递 id。
window.location.href =" <?php echo base_url(); ?>crossing/payment_rec_allocation/PaymentRecAllocationController/printCollectionRecPage";
如何在打印页面上传递选定的 ID。 我的打印页面有表格显示插入时间所选数据的数据。
【问题讨论】:
4 和 5 会发生什么? @Strawberry 在插入 3 条记录后下次我将插入 4 & 5 那么,4 和 5 是暂时存储在某个地方,还是每次都重新生成? @Strawberry 4 & 5 不是我可以选择所有记录的问题,但它显示了我数据库中存储的所有以前的数据。我只想在我的打印页面上显示选定的记录显示。 所以你插入了 5 条记录,但同时只想将其中 3 条传到下一页? 【参考方案1】:你可以使用insert_id()
函数
public function inserPaymentAllocation($data)
if($this->db->insert('payment_rec_allocn', $data))
$insert_id = $this->db->insert_id();
return $insert_id;
else
return false;
将返回的 id 存储到数组中
$modelResult[] = $this->PayRecAllModel->inserPaymentAllocation($data);
if(!empty($modelResult))
$data['PaymentRecAllocation'] = array(
'form_status' => 'true',
'form_message' => 'Payment Recovery has been successfully Allocate',
'form_ids' => $modelResult
);
将 id 传递给您的控制器进行打印
var ids = data.PaymentRecAllocation.form_ids.join(',');
window.location.href =" <?php echo base_url(); ?>crossing/payment_rec_allocation/PaymentRecAllocationController/printCollectionRecPage?ids="+ids;
但如果有多个插入,您最好使用
$this->db->trans_start();
//all your insertion code here
//if anything breaks the db will be rollback
$this->db->trans_complete();
【讨论】:
它给了我Uncaught TypeError: Cannot read property 'join' of undefined
这个错误
这个答案给出了基本的概述,让你有想法。我还没有测试过。你得到$modelResult
数组中的id了吗?
@sheryas 它只返回 1 个 id以上是关于如何在插入数据时将多行选择的 id 传递到打印页面的主要内容,如果未能解决你的问题,请参考以下文章
如何在codebehind的pageload事件被触发之前,在页面加载时将一个会话存储值传递到文本框中?
在页面加载时将道具传递给子组件 - 与 next.js 做出反应