【问题标题】:Is it possible to force the value of a new CRUD form field?是否可以强制新 CRUD 表单字段的值?
【发布时间】:2012-07-11 15:35:54
【问题描述】:

我正在使用敏捷工具包。我的 CRUD 中有一个下拉字段。

如何使“新建”按钮在此下拉菜单中显示与单击“编辑”按钮时不同的值?

这是我的代码:

 class page_things extends Page {
    function init(){
        parent::init();
        $p = $this;

        $f = $p->add('Form');

        $idCat = ($f->get('idCat')?$f->get('idCat'):$this->api->getConfig('idCat','MASP2U03'));


        $dpUE = $f->addField('dropdown', 'Category');
        $dpUE->setModel('Category');
        $dpUE->js('change',$f->js()->submit());
        $dpUE->set($idCat);

        $f->addSubmit('OK');

        $c = $f->add('CRUD');

        $c->setModel('things',array('name', 'field1', 'field2', 'field3'))->setMasterField('idCat',$idCat);


        if($f->isSubmitted()){

            $c->js(true)->show()->execute()->reload()->execute();

            }

    }
}

谢谢你的帮助!!

【问题讨论】:

    标签: php user-interface frameworks atk4


    【解决方案1】:

    使用

    $crud=$this->add('CRUD',array('allow_add'=>false));
    

    禁用默认添加按钮,然后添加您自己的按钮:

    if($crud->grid)$crud->grid->addButton('Add')->js('click')
        ->frameURL('Add',$this->api->url('./new'));
    

    在此之后,您需要创建一个新页面

    class page_things_new extends Page {
    

    并在此页面内以您希望它出现的方式定义表单,而不是“添加”单击。我不完全理解您的问题,但是通过这些说明,您可以在向 crud 添加新条目时显示不同的页面。

    【讨论】:

    • 非常感谢!使用您的代码和来源,我尝试了这个解决方案:if($crud->form) { $thefield = $c->form->getElement('idCat')->set($idCat)->js(true)->hide(); } 它有效!
    • 太棒了。我已经编辑了您最初的问题,以便对其他人也有用,请查看它。
    【解决方案2】:

    这是我尝试过的罗马书的替代品。它使用 $this->api->memorize 将在下拉列表中选择的 GET 变量存储在会话变量中。然后在Form中,您可以通过在模型中使用recall来设置默认选择的值。

    类似的东西

    在页面/事物中

     // load the javascript function (see later)
     $this->js()->_load('your_univ');
    
     /*****************************************************************/
     /* Code to populate drop down lists - amend where as required*/
                $catList=$this->api->db->dsql()->table('category c')
                         ->field('c.id')
                         ->field('c.name')
                         ->where('c.type',$cat_type)
                         ->order('c.id')
                         ->do_getAssoc();
    
        // Check if one is set on URL or default from config and memorize the value
        if ($_GET['cat']){
          $cat=$_GET['cat'];
        } else {
          $cat=$this->api-getConfig('idCat');
        }
        $this->api->memorize('category',$cat);
    
    
        $f=$p->add('Form',null,null,array('form_empty'))
             ->setFormClass('horizontal bottom-padded');
        $l1=$f->addField('dropdown','category')->setValueList($catList)->set($cat);
    
        // calls a bit of javascript described later to reload with the parameter
        $l1->js('change')->univ()->yourfunc($p->api->getDestinationURL(null), $l1);
    
        .. rest of your page code goes here ..
    

    然后在/lib/Model/Category.php中

    为该字段添加以下召回

      $this->addField('idCat')->system(true)->visible(false)
           ->defaultValue($this->api->recall('category'));
    

    注意 system(true) 和 visible(False) 表示它不会在 CRUD 上显示并且不可更改,但您可以使用这些选项,使其显示在 CRUD 网格中而不是表单中。

    最后,一点点 javascript 使重新加载工作(罗马人可能会建议一个更好的方法来做到这一点)。确保 yourfunc 在页面和 js 中匹配。

    在 /templates/js/your_univ.js 添加以下内容

        $.each({
            yourfunc: function(url, name){
                  document.location.href=url+'&cat='+$(name).val();
            },
        },$.univ._import);
    

    我在自己的页面中有类似的代码。如果您不希望猫在 URL 上显示,您可能可以将其作为 POST 以及下拉菜单作为表单。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      • 2020-11-02
      • 1970-01-01
      • 2015-12-28
      • 1970-01-01
      • 2011-12-25
      相关资源
      最近更新 更多