我正在为magento开发一个扩展程序,它显示自定义数据库表中的问题组,我需要将问题(从另一个自定义表中加载)分配给组。
到目前为止,我可以对网格进行编码以显示问题组和问题。当我去编辑一个问题组时,它会显示数据库中的所有问题。我需要的是选择其中一些并保存。如果我将所有问题加载到一个屏幕(到目前为止,我有130个问题,我可以通过更改"将每个问题加载到一个屏幕;每页查看20个"阅读"每页查看200个&# 34;在网格中)并选择我需要的东西,然后保存,它就像一个魅力。
但是,如果我从第1页选择一些问题并导航到第2页然后选择其他一些问题,依此类推,只保存最后一页中的问题。前几页中的所有其他选择都将丢失。
我发现每次浏览页面时都会覆盖网格序列化的隐藏值,因此选择会丢失。
任何建议都非常感谢。
这是我使用的代码 应用\代码\社区\ Muaw \处方\块\ Adminhtml \组\编辑\标签\ Question.php
class Muaw_Prescription_Block_Adminhtml_Groups_Edit_Tab_Question extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('questionsGrid');
$this->setUseAjax(true);
$this->setDefaultFilter(array('in_questions' => 1));
$this->setSaveParametersInSession(false);
}
protected function _addColumnFilterToCollection($column)
{
if ($column->getId() == 'in_questions') {
$questionIds = $this->_getSelectedQuestions();
if (empty($questionIds)) {
$questionIds = 0;
}
if ($column->getFilter()->getValue()) {
$this->getCollection()->addFieldToFilter('id', array('in' => $questionIds));
} else {
if ($questionIds) {
$this->getCollection()->addFieldToFilter('id', array('nin' => $questionIds));
}
}
} else {
parent::_addColumnFilterToCollection($column);
}
return $this;
}
protected function _prepareCollection()
{
$collection = Mage::getModel('muaw_prescription/question')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('in_questions', array(
'header_css_class' => 'a-center',
'type' => 'checkbox',
'name' => 'in_questions',
'field_name' => 'questions[]',
'values' => $this->_getSelectedQuestions(),
'align' => 'center',
'index' => 'id'
));
$this->addColumn('id', array(
'header' => Mage::helper('catalog')->__('ID'),
'sortable' => true,
'width' => '60',
'index' => 'id'
));
$this->addColumn('question', array(
'header' => $this->__('Question'),
'index' => 'question',
'align' => 'left',
));
return parent::_prepareColumns();
}
protected function _getSelectedQuestions()
{
$customers = array_keys($this->getSelectedQuestions());
return $customers;
}
public function getSelectedQuestions()
{
$tm_id = $this->getRequest()->getParam('id');
if (!isset($tm_id)) {
$tm_id = 0;
}
$questions = array();
$groupq = Mage::getModel('muaw_prescription/qgr')->getCollection()->addFieldToFilter('group_id', $tm_id);
foreach ($groupq as $group) {
$questions[] = $group->getQuestionId();
}
$custIds = array();
foreach ($questions as $cust) {
$custIds[$cust] = array('id' => $cust);
}
return $custIds;
}
public function getGridUrl()
{
return $this->_getData('grid_url') ? $this->_getData('grid_url') : $this->getUrl('*/*/questionsGrid', array('_current' => true));
}
}
应用\代码\社区\ Muaw \处方\块\ Adminhtml \组\编辑\ Tabs.php
class Muaw_Prescription_Block_Adminhtml_Groups_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{
public function __construct()
{
parent::__construct();
$this->setId('form_tabs');
$this->setDestElementId('edit_form');
$this->setTitle(Mage::helper('muaw_prescription')->__('Group Information'));
}
protected function _beforeToHtml()
{
$this->addTab('form_section', array(
'label' => Mage::helper('muaw_prescription')->__('Groups Information'),
'title' => Mage::helper('muaw_prescription')->__('Details'),
'content' => $this->getLayout()
->createBlock('muaw_prescription/adminhtml_groups_edit_tab_main')
->toHtml(),
));
$this->addTab('questions', array(
'label' => Mage::helper('muaw_prescription')->__('Associated Questions'),
'url' => $this->getUrl('*/*/questionstab', array('_current' => true)),
'content' => $this->getLayout(),
'class' => 'ajax',
));
return parent::_beforeToHtml();
}
}
应用\代码\社区\ Muaw \处方\控制器\ Adminhtml \ GroupsController.php
class Muaw_Prescription_Adminhtml_GroupsController extends Mage_Adminhtml_Controller_Action
{
protected function _initAction()
{
// load layout, set active menu and breadcrumbs
$this->loadLayout()
->_setActiveMenu('prescription/manage')
->_addBreadcrumb(
Mage::helper('muaw_prescription')->__('Group'),
Mage::helper('muaw_prescription')->__('Group')
)
->_addBreadcrumb(
Mage::helper('muaw_prescription')->__('Manage Group'),
Mage::helper('muaw_prescription')->__('Manage Group')
);
return $this;
}
public function indexAction()
{
$this->_title($this->__('Group'))
->_title($this->__('Manage Group'));
$this->_initAction();
$this->renderLayout();
}
public function QuestionAction()
{
$this->_title($this->__('Group Questions'))
->_title($this->__('Manage Group Questions'));
$this->loadLayout()
->_setActiveMenu('groups/manage')
->_addBreadcrumb(
Mage::helper('muaw_prescription')->__('Group Questions'),
Mage::helper('muaw_prescription')->__('Group Questions')
)
->_addBreadcrumb(
Mage::helper('muaw_prescription')->__('Manage Group Questions'),
Mage::helper('muaw_prescription')->__('Manage Group Questions')
);
$this->renderLayout();
}
public function newAction()
{
// the same form is used to create and edit
$this->_forward('edit');
}
public function editAction()
{
$this->_title($this->__('Group'))
->_title($this->__('Manage Group'));
$model = Mage::getModel('muaw_prescription/groups');
$newsId = $this->getRequest()->getParam('id');
if ($newsId) {
$model->load($newsId);
if (!$model->getId()) {
$this->_getSession()->addError(Mage::helper('muaw_prescription')->__('Group does not exist.'));
return $this->_redirect('*/*/');
}
$this->_title($model->getTitle());
$breadCrumb = Mage::helper('muaw_prescription')->__('Edit Item');
} else {
$this->_title(Mage::helper('muaw_prescription')->__('New Item'));
$breadCrumb = Mage::helper('muaw_prescription')->__('New Item');
}
$this->_initAction()->_addBreadcrumb($breadCrumb, $breadCrumb);
$data = Mage::getSingleton('adminhtml/session')->getFormData(true);
if (!empty($data)) {
$model->addData($data);
}
Mage::register('question_item', $model);
$this->renderLayout();
}
public function saveAction()
{
$redirectPath = '*/*';
$redirectParams = array();
// check if data sent
$data = $this->getRequest()->getPost();
if (isset($data['links'])) {
$customers = Mage::helper('adminhtml/js')->decodeGridSerializedInput($data['links']['questions']);
}
//print_r($customers);exit;
if ($data) {
$data = $this->_filterPostData($data);
$model = Mage::getModel('muaw_prescription/groups');
if (empty($data['pid'])) {
$arr = array();
$arr['form_key'] = $data['form_key'];
$arr['name'] = $data['name'];
$model->addData($arr);
$out = $model->save()->getId();
} else {
$arr = array();
$arr['form_key'] = $data['form_key'];
$arr['id'] = $data['pid'];
$arr['name'] = $data['name'];
$model->load($data['pid'])->addData($arr);
$model->setId($data['pid'])->save();
$out = $data['pid'];
}
try {
$hasError = false;
$this->_getSession()->addSuccess(Mage::helper('muaw_prescription')->__('The Group has been saved.'));
// check if 'Save and Continue'
if ($this->getRequest()->getParam('back')) {
$redirectPath = '*/*/edit';
$redirectParams = array('id' => $out);
}
$groupq = Mage::getModel('muaw_prescription/qgr')->getCollection()->addFieldToFilter('group_id', $out);
$sel_question = array();
foreach ($groupq as $group) {
if (!empty($group->getQuestionId())) {
$sel_question[$group->getId()] = $group->getQuestionId();
}
}
$del_arr = $new_arr = array();
$del_arr = array_diff($sel_question, $data['questions']);
$new_arr = array_diff($data['questions'], $sel_question);
if (!empty($data['questions'])) {
if (!empty($del_arr)) {
foreach ($del_arr as $del => $val) {
$id = $del;
$model_qgr = Mage::getModel('muaw_prescription/qgr');
try {
$model_qgr->setId($id)->delete();
} catch (Exception $e) {
//echo $e->getMessage();
}
}
}
} else {
$collection = Mage::getModel('muaw_prescription/qgr')->getCollection()->addFieldToFilter('group_id', $out);
foreach ($collection as $item) {
$id = $item->getId();
$model_qgr = Mage::getModel('muaw_prescription/qgr');
try {
$model_qgr->setId($id)->delete();
} catch (Exception $e) {
//echo $e->getMessage();
}
}
}
if (!empty($new_arr)) {
foreach ($new_arr as $new) {
if ($new != 'on') {
$new_data = array();
$new_data['question_id'] = $new;
$new_data['group_id'] = $out;
try {
$model_qgr = Mage::getModel('muaw_prescription/qgr')->setData($new_data);
$insertId = $model_qgr->save()->getId();
} catch (Exception $e) {
//echo $e->getMessage();
}
}
}
} else {
}
} catch (Mage_Core_Exception $e) {
$hasError = true;
$this->_getSession()->addError($e->getMessage());
} catch (Exception $e) {
$hasError = true;
$this->_getSession()->addException($e, Mage::helper('muaw_prescription')->__('An error occurred while saving the Group.'));
}
if ($hasError) {
$this->_getSession()->setFormData($data);
$redirectPath = '*/*/edit';
$redirectParams = array('id' => $data['pid']);
}
}
$this->_redirect($redirectPath, $redirectParams);
}
public function deleteAction()
{
// check if we know what should be deleted
$itemId = $this->getRequest()->getParam('id');
if ($itemId) {
try {
$model = Mage::getModel('muaw_prescription/groups');
$model->load($itemId);
if (!$model->getId()) {
Mage::throwException(Mage::helper('muaw_prescription')->__('Unable to find a group.'));
}
$model->delete();
$this->_getSession()->addSuccess(
Mage::helper('muaw_prescription')->__('The group has been deleted.')
);
} catch (Mage_Core_Exception $e) {
$this->_getSession()->addError($e->getMessage());
} catch (Exception $e) {
$this->_getSession()->addException($e,
Mage::helper('muaw_prescription')->__('An error occurred while deleting the group.')
);
}
}
$this->_redirect('*/*/');
}
protected function _initGroups()
{
$groupModel = Mage::getModel('muaw_prescription/groups');
$groupId = (int)$this->getRequest()->getParam('id', 0);
if ($groupId) {
try {
$groupModel->load($groupId);
if (!$groupModel->getId()) {
throw new Exception($this->__('This group no longer exists'));
}
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
return null;
}
}
Mage::register('current_group', $groupModel);
return $groupModel;
}
protected function _isAllowed()
{
switch ($this->getRequest()->getActionName()) {
case 'new':
case 'save':
return Mage::getSingleton('admin/session')->isAllowed('groups/manage/save');
break;
case 'delete':
return Mage::getSingleton('admin/session')->isAllowed('groups/manage/delete');
break;
default:
return Mage::getSingleton('admin/session')->isAllowed('groups/manage');
break;
}
}
protected function _filterPostData($data)
{
$data = $this->_filterDates($data, array('time_published'));
return $data;
}
public function gridAction()
{
$this->loadLayout();
$this->renderLayout();
}
protected function _isAjax()
{
if ($this->getRequest()->isXmlHttpRequest()) {
return true;
}
if ($this->getRequest()->getParam('ajax') || $this->getRequest()->getParam('isAjax')) {
return true;
}
return false;
}
public function questionsTabAction()
{
$itemId = $this->getRequest()->getParam('id');
$groupq = Mage::getModel('muaw_prescription/qgr')->getCollection()->addFieldToFilter('group_id', $itemId);
$sel_question = array();
foreach ($groupq as $group) {
if (!empty($group->getQuestionId())) {
$sel_question[] = $group->getQuestionId();
}
}
$saved_question_ids = $sel_question; // your load logic here
if (!empty($saved_question_ids)) {
$this->loadLayout()
->getLayout()
->getBlock('muaw.tab.questions')
->setQuestions($saved_question_ids);
} else {
$this->loadLayout()
->getLayout()
->getBlock('muaw.tab.questions')
->setQuestions($this->getRequest()->getPost('questions', null));
}
$this->renderLayout();
}
public function questionsGridAction()
{
$this->loadLayout()
->getLayout()
->getBlock('muaw.tab.questions')
->setQuestions($this->getRequest()->getPost('questions', null));
$this->renderLayout();
}
public function questionsAction()
{
$this->loadLayout();
$this->getLayout()->getBlock('muaw.tab.questions')
->setQuestions($this->getRequest()->getPost('questions', null));
$this->renderLayout();
}
}
应用\设计\ adminhtml \默认\默认\布局\ muaw_prescription.xml
<adminhtml_groups_edit>
<update handle="editor"/>
<reference name="left">
<block type="muaw_prescription/adminhtml_groups_edit_tabs" name="groups.edit.tabs" />
</reference>
<reference name="content">
<block type="muaw_prescription/adminhtml_groups_edit" name="groups.edit" />
</reference>
</adminhtml_groups_edit>
<adminhtml_groups_questionstab>
<block type="core/text_list" name="root" output="toHtml">
<block type="muaw_prescription/adminhtml_groups_edit_tab_question" name="muaw.tab.questions"/>
<block type="adminhtml/widget_grid_serializer" name="muaw.serializer.questions">
<reference name="muaw.serializer.questions">
<action method="initSerializerBlock">
<grid_block_name>muaw.tab.questions</grid_block_name>
<data_callback>getSelectedQuestions</data_callback>
<hidden_input_name>links[questions]</hidden_input_name>
<reload_param_name>questions</reload_param_name>
</action>
<action method="addColumnInputName">
<input_name>position</input_name>
</action>
</reference>
</block>
</block>
</adminhtml_groups_questionstab>
<adminhtml_groups_questionsgrid>
<block type="core/text_list" name="root">
<block type="muaw_prescription/adminhtml_groups_edit_tab_question" name="muaw.tab.questions"/>
</block>
</adminhtml_groups_questionsgrid>