我正在开发一个内部 CakePHP 公司应用程序,它有一个发货屏幕,有三个列表:
用户可以将项目从左侧列表拖动到右侧列表,反之亦然。单击“保存”按钮后,表单将与右侧列表中的项目一起提交。
作为保存的一部分,我需要将更新后的列表(使用 PHP )与数据库中保存的列表进行比较。
如果有任何添加,我需要使用发货ID更新商品记录。
对于任何删除,我需要更新商品记录以将发货ID卖给NULL。
这将允许用户在两个方向上拖放项目,然后单击“保存”并期望页面加载他们离开页面的状态。
我可以保存要添加到列表中的项目,并单独保存要删除的项目。但是,我不能让他们一起工作。当我尝试这个时,添加到列表中的一个项目将完全替换" Packed Items"上的所有其他项目。名单。
function edit() {
$this->Despatch->recursive = -1;
if (!empty($this->data)) {
if($this->Despatch->save($this->data)) {
/* NEED TO RETRIEVE CURRENT REPAIR IDs ASSOCIATED
WITH THE DESPATCH ID AND COMPARE WITH FORM SUBMISSION LIST TO
REMOVE THE DIFFERENCES */
# Retrieve current repair records where despatch_id = $this->id.
$this->Despatch->Repair->recursive = -1;
$current_associated_repairs = $this->Despatch->Repair->find('all', array(
'fields' => array('id', 'despatch_date', 'despatch_id'),
'conditions' => array('Repair.despatch_id =' => $this->data['Despatch']['id'])));
# Create array with repair IDs
$repairs = explode(",", $this->data['Repair']['ids']);
$i = 0;
foreach($repairs as $repair) {
$repairupdates[$i]['Repair']['id'] = $repair;
$i++;
}
# Delete array index to prevent it interfering with form saving later
unset($this->data['Repair']['ids']);
# 2. Find unmatched IDs between current records and form submission list of repairs
$length1 = sizeof($current_associated_repairs);
$length2 = sizeof($this->data['Repair']);
for ($i = 0; $i < $length1; $i++) {
for ($j = 0; $j < $length2; $j++) {
### LOGIC NOT CORRECT HERE !!! ###
### THIS WORKS FOR REMOVING PACKED INSTRUMENTS - FROM RIGHT TO LEFT ###
### BUT ACTUALLY BEING TRIGGERED WHEN ADDING FROM LEFT TO RIGHT TOO ###
if ($current_associated_repairs[$i]['Repair']['id'] == $this->data['Repair'][$j]['id']
&& !in_array($current_associated_repairs[$i]['Repair']['id'], $this->data['Repair'][$j])) {
# if it's in current repairs and not in form submission, set to null...
# otherwise, it must be an addition if there's no matches
# 3. Set the despatch_date and despatch_id fields of those records to NULL
$this->data['Repair'][$j+1]['id'] = $current_associated_repairs[$i]['Repair']['id'];
$this->data['Repair'][$j+1]['despatch_date'] = null;
$this->data['Repair'][$j+1]['despatch_id'] = null;
}
}
}
# 4. Save the new list of repairs to the current despatch
if($this->Despatch->save($this->data) && $this->Despatch->Repair->saveAll($this->data['Repair'])) {
$this->Session->setFlash(__('The despatch has been saved.', true), 'default', array('class' => 'success-scheme message'));
} else {
$this->Session->setFlash(__('The repair could not be updated with the despatch number. Please try again.', true), 'default', array('class' => 'error-scheme message'));
}
} else {
$this->Session->setFlash(__('The despatch could not be saved. Please try again.', true), 'default', array('class' => 'error-scheme message'));
}
$this->redirect($this->referer(array('action' => 'view', $this->data['Despatch']['id'])));
}
}
我认为我已经包含了所有人应该需要的所有细节,但只是询问您是否需要了解更多信息。我遇到的主要问题是理解实现此目的的代码逻辑。
非常感谢提前。
// click handler for saving despatch - required to save sortable lists properly
$('#save_despatch').click(function(e) {
e.preventDefault();
// gives comma-separated array of instrument IDs, e.g. 12345, 56789...
var result = $('#sortable2').sortable('toArray');
$('#RepairIds').val(result);
$('form').submit();
});
答案 0 :(得分:2)
我无法在你的逻辑中弄清楚你现在在做什么,但我会建议一种毫无疑问应该有用的方法。
为简单起见,您有两个列表:左侧和右侧。这段代码是一种伪代码,因为我不完全理解你需要什么,但是这样,列表应该保持更新,并且不能出错。
显示列表之前的逻辑(在PHP中):
//Retrieve records from mysql into two arrays->aLeft and aRight
//Create an MD5 hash to easily compare current en future lists.
$sCheckSum = md5(implode(",",$aLeft));
$_SESSION['checkSum'] = $sCheckSum;
//Generate your lists now
//Display output
JS:
$('#save_despatch').click(function(e) {
e.preventDefault();
// gives comma-separated array of instrument IDs, e.g. 12345, 56789...
var leftResult = $('#sortable2').sortable('toArray');
var rightResult = $('#sortableX').sortable('toArray'); //Make sure this matches your lists, as I have not seen your HTML
$('#Left').val(leftResult);
$('#Right').val(rightResult);
$('form').submit();
});
然后在接收端:
//Get the values of the #Left and #Right inputs in to $aLeft and $aRight
$sNewCheckSum = md5($aLeft);
if($sNewCheckSum != $_SESSION['checkSum'] ) //Left list is not the same, so the right is neither
{
foreach(explode(",",$aLeft) as $iRepairId){
//Not sure what should happen here, but thats up to you.
//Mysql update table SET ID = NULL WHERE repairdId = $iRepairId
}
foreach(explode(",",$aRight) as $iRepairId){
//Not sure what should happen here, but thats up to you.
//Mysql update table SET ID = xxx WHERE repairdId = $iRepairId
}
}