Yii:将CGridview添加到表单中

时间:2013-08-29 03:25:19

标签: javascript php jquery yii model

我正在尝试在另一个模型中连接的 create.php 中创建一个可选表。如果您在表格中选择一行,它将自动放入我的下拉列表表单中。

我有5张桌子相互连接。

Shift: shft_id,start,end,name,status

Day: id_day,mon,tue,wed,thurs,fri,sat,sun,name

Sched: id_sched,sched_name,fk_id_day,status

EmpSched: id_emp_sched,fk_id_sched,from,to,default,fk_user

User_dummy: id_user, empno, leave_credits

在我的empsched表中,我希望在 create.php (empsched)中有一个表格,它将显示我日历表格的CGridView。当我在表格中双击它时。我的fk_id_sched得到了表格。我还希望在表格下方有一个模态弹出窗口,以便它可以添加我想要在fk_id_sched中获取的其他数据。

enter image description here

我只是想知道这是否可行? id_dayfk_id_sched相关。如果您双击表格,它将获取数据 - id_day 1将在我的fk_id_sched表单中以“hehe”的形式提取

在我的empSched _form.php

<div>
<?php echo $form->labelEx($model,'fk_id_sched'); ?>
<?php echo $form->dropDownList($model, 'fk_id_sched', CHtml::listData(
        Schedule::model()->findAll(), 'id_sched', 'sched_name'
    )
); ?>
<?php echo $form->error($model,'fk_id_sched'); ?>

在我的日子 admin.php

<?php $this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'day-grid',
'dataProvider'=>$model->search(),
'pager'=>array('header'=>''),
'emptyText'=>'Coming soon',
'filter'=>$model,
'columns'=>array(
    'id_day',
    array(
        'name'=>'monday',
        'value'=>'$data->monday==null ? "OFF" : $data->monday->name',
    ),
    array(
        'name'=>'tuesday',
        'value'=>'$data->tuesday==null ? "OFF" : $data->tuesday->name',
    ),
    array(
        'name'=>'wednesday',
        'value'=>'$data->wednesday==null ? "OFF" : $data->wednesday->name',
    ),
    array(
        'name'=>'thursday',
        'value'=>'$data->thursday==null ? "OFF" : $data->thursday->name',
    ),
    array(
        'name'=>'friday',
        'value'=>'$data->friday==null ? "OFF" : $data->friday->name',
    ),

empschedule模型中没有任何关系。

这是我的EmpSched create.php

的页面源代码
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="language" content="en" />

    <link rel="stylesheet" type="text/css" href="/hris/themes/bootstrap/css/styles.css" />

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="/hris/assets/ac3c9c93/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="/hris/assets/ac3c9c93/css/bootstrap-responsive.min.css" />
<link rel="stylesheet" type="text/css" href="/hris/assets/ac3c9c93/css/bootstrap-yii.css" />
<link rel="stylesheet" type="text/css" href="/hris/assets/ac3c9c93/css/jquery-ui-bootstrap.css" />
<link rel="stylesheet" type="text/css" href="/hris/assets/ac3c9c93/css/bootstrap-datepicker.css" />
<script type="text/javascript" src="/hris/assets/7c5d9cbe/jquery.min.js"></script>
<script type="text/javascript" src="/hris/assets/7c5d9cbe/jquery.ba-bbq.min.js"></script>
<script type="text/javascript" src="/hris/assets/ac3c9c93/js/bootstrap.bootbox.min.js"></script>
<script type="text/javascript" src="/hris/assets/ac3c9c93/js/bootstrap.min.js"></script>
<title>HRIS - Create EmpSched</title>

    </head>

<body>

<div class="navbar navbar-inverse navbar-fixed-top"><div class="navbar-inner"><div class="container"><a href="/hris/index.php" class="brand">HRIS</a><ul id="yw6" class="nav"><li><a href="/hris/index.php?r=site/index">Home</a></li><li><a href="/hris/index.php?r=site/page&amp;view=about">About</a></li><li><a href="/hris/index.php?r=site/contact">Contact</a></li><li><a href="/hris/index.php?r=site/logout">Logout (admin)</a></li></ul></div></div></div>
<div class="container" id="page">

            <ul class="breadcrumbs breadcrumb"><li><a href="/hris/index.php">Home</a></li><span class="divider">/</span><li><a href="/hris/index.php?r=scheduling/empSched/index">Emp Scheds</a></li><span class="divider">/</span><li class="active">Create</li></ul><!-- breadcrumbs -->

    <div class="row">
    <div class="span9">
        <div id="content">

<h1>Create EmpSched</h1>

<form class="form-vertical" id="emp-sched-form" action="/hris/index.php?r=scheduling/empSched/create" method="post">
<p class="help-block">Fields with <span class="required">*</span> are required.</p>


<div>
    <label for="EmpSched_fk_id_sched">Fk Id Sched</label>    <select name="EmpSched[fk_id_sched]" id="EmpSched_fk_id_sched">
<option value="1">hehe</option>
<option value="2">michael so</option>
<option value="3">John Mark</option>
<option value="4">Calvin</option>
<option value="5">Jeric</option>
<option value="7">anthony</option>
<option value="8">hehehehe</option>
<option value="9">Rhue ann</option>
</select>    </div>



<label for="EmpSched_from">From</label><div class="input-prepend"><span class="add-on"><i class="icon-calendar"></i></span><input class="span4" type="text" autocomplete="off" name="EmpSched[from]" id="EmpSched_from" /></div>


<label for="EmpSched_to">To</label><div class="input-prepend"><span class="add-on"><i class="icon-calendar"></i></span><input class="span4" type="text" autocomplete="off" name="EmpSched[to]" id="EmpSched_to" /></div>




<label class="checkbox" for="EmpSched_default"><input id="ytEmpSched_default" type="hidden" value="0" name="EmpSched[default]" /><input name="EmpSched[default]" id="EmpSched_default" value="1" type="checkbox" />
Default</label>


<div>
    <label for="EmpSched_fk_user" class="required">Fk User <span class="required">*</span></label>    <select name="EmpSched[fk_user]" id="EmpSched_fk_user">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
<option value="32">32</option>
<option value="33">33</option>
<option value="34">34</option>
<option value="35">35</option>
<option value="36">36</option>
<option value="37">37</option>
<option value="38">38</option>
<option value="39">39</option>
</select>    </div>

<div class="form-actions">
    <button class="btn btn-primary" id="yw2" type="submit" name="yt0">Create</button></div>

</form>
<div id="day-grid" class="grid-view">
<div class="summary">Displaying 1-10 of 12 results.</div>
<table class="items table">
<thead>
<tr>
<th id="day-grid_c0"><a class="sort-link" href="/hris/index.php?r=scheduling/empSched/create&amp;day_sort=name">Name<span class="caret"></span></a></th><th id="day-grid_c1"><a class="sort-link" href="/hris/index.php?r=scheduling/empSched/create&amp;day_sort=mon">Mon<span class="caret"></span></a></th><th id="day-grid_c2"><a class="sort-link" href="/hris/index.php?r=scheduling/empSched/create&amp;day_sort=tue">Tue<span class="caret"></span></a></th><th id="day-grid_c3"><a class="sort-link" href="/hris/index.php?r=scheduling/empSched/create&amp;day_sort=wed">Wed<span class="caret"></span></a></th><th id="day-grid_c4"><a class="sort-link" href="/hris/index.php?r=scheduling/empSched/create&amp;day_sort=thurs">Thurs<span class="caret"></span></a></th><th id="day-grid_c5"><a class="sort-link" href="/hris/index.php?r=scheduling/empSched/create&amp;day_sort=fri">Fri<span class="caret"></span></a></th><th id="day-grid_c6"><a class="sort-link" href="/hris/index.php?r=scheduling/empSched/create&amp;day_sort=sat">Sat<span class="caret"></span></a></th><th id="day-grid_c7"><a class="sort-link" href="/hris/index.php?r=scheduling/empSched/create&amp;day_sort=sun">Sun<span class="caret"></span></a></th><th class="button-column" id="day-grid_c8">&nbsp;</th></tr>
</thead>
<tbody>
<tr class="odd">
<td>hehe</td><td>8</td><td>6</td><td>6</td><td>6</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td class="button-column"><a class="view" title="View" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/view&amp;id=1"><i class="icon-eye-open"></i></a> <a class="update" title="Update" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/update&amp;id=1"><i class="icon-pencil"></i></a> <a class="delete" title="Delete" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/delete&amp;id=1"><i class="icon-trash"></i></a></td></tr>
<tr class="even">
<td>day2</td><td>&nbsp;</td><td>8</td><td>8</td><td>6</td><td>6</td><td>6</td><td>6</td><td class="button-column"><a class="view" title="View" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/view&amp;id=2"><i class="icon-eye-open"></i></a> <a class="update" title="Update" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/update&amp;id=2"><i class="icon-pencil"></i></a> <a class="delete" title="Delete" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/delete&amp;id=2"><i class="icon-trash"></i></a></td></tr>
<tr class="odd">
<td>day3</td><td>6</td><td>6</td><td>6</td><td>8</td><td>6</td><td>6</td><td>6</td><td class="button-column"><a class="view" title="View" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/view&amp;id=3"><i class="icon-eye-open"></i></a> <a class="update" title="Update" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/update&amp;id=3"><i class="icon-pencil"></i></a> <a class="delete" title="Delete" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/delete&amp;id=3"><i class="icon-trash"></i></a></td></tr>
<tr class="even">
<td>day4</td><td>6</td><td>6</td><td>6</td><td>6</td><td>8</td><td>6</td><td>6</td><td class="button-column"><a class="view" title="View" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/view&amp;id=4"><i class="icon-eye-open"></i></a> <a class="update" title="Update" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/update&amp;id=4"><i class="icon-pencil"></i></a> <a class="delete" title="Delete" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/delete&amp;id=4"><i class="icon-trash"></i></a></td></tr>
<tr class="odd">
<td>day5</td><td>9</td><td>9</td><td>9</td><td>9</td><td>9</td><td>9</td><td>9</td><td class="button-column"><a class="view" title="View" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/view&amp;id=5"><i class="icon-eye-open"></i></a> <a class="update" title="Update" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/update&amp;id=5"><i class="icon-pencil"></i></a> <a class="delete" title="Delete" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/delete&amp;id=5"><i class="icon-trash"></i></a></td></tr>
<tr class="even">
<td>day6</td><td>9</td><td>9</td><td>9</td><td>9</td><td>9</td><td>9</td><td>9</td><td class="button-column"><a class="view" title="View" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/view&amp;id=6"><i class="icon-eye-open"></i></a> <a class="update" title="Update" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/update&amp;id=6"><i class="icon-pencil"></i></a> <a class="delete" title="Delete" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/delete&amp;id=6"><i class="icon-trash"></i></a></td></tr>
<tr class="odd">
<td>day7</td><td>6</td><td>6</td><td>6</td><td>6</td><td>6</td><td>6</td><td>9</td><td class="button-column"><a class="view" title="View" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/view&amp;id=7"><i class="icon-eye-open"></i></a> <a class="update" title="Update" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/update&amp;id=7"><i class="icon-pencil"></i></a> <a class="delete" title="Delete" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/delete&amp;id=7"><i class="icon-trash"></i></a></td></tr>
<tr class="even">
<td>day11</td><td>8</td><td>6</td><td>6</td><td>6</td><td>6</td><td>6</td><td>8</td><td class="button-column"><a class="view" title="View" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/view&amp;id=11"><i class="icon-eye-open"></i></a> <a class="update" title="Update" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/update&amp;id=11"><i class="icon-pencil"></i></a> <a class="delete" title="Delete" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/delete&amp;id=11"><i class="icon-trash"></i></a></td></tr>
<tr class="odd">
<td>day12</td><td>6</td><td>6</td><td>6</td><td>6</td><td>6</td><td>9</td><td>9</td><td class="button-column"><a class="view" title="View" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/view&amp;id=12"><i class="icon-eye-open"></i></a> <a class="update" title="Update" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/update&amp;id=12"><i class="icon-pencil"></i></a> <a class="delete" title="Delete" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/delete&amp;id=12"><i class="icon-trash"></i></a></td></tr>
<tr class="even">
<td>day13</td><td>6</td><td>6</td><td>6</td><td>6</td><td>6</td><td>6</td><td>8</td><td class="button-column"><a class="view" title="View" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/view&amp;id=13"><i class="icon-eye-open"></i></a> <a class="update" title="Update" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/update&amp;id=13"><i class="icon-pencil"></i></a> <a class="delete" title="Delete" rel="tooltip" href="/hris/index.php?r=scheduling/empSched/delete&amp;id=13"><i class="icon-trash"></i></a></td></tr>
</tbody>
</table>
<div class="pagination"><ul id="yw3" class="yiiPager"><li class="previous disabled"><a href="/hris/index.php?r=scheduling/empSched/create">&larr;</a></li>
<li class=" active"><a href="/hris/index.php?r=scheduling/empSched/create">1</a></li>
<li class=""><a href="/hris/index.php?r=scheduling/empSched/create&amp;day_page=2">2</a></li>
<li class="next"><a href="/hris/index.php?r=scheduling/empSched/create&amp;day_page=2">&rarr;</a></li></ul></div><div class="keys" style="display:none" title="/hris/index.php?r=scheduling/empSched/create"><span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>11</span><span>12</span><span>13</span></div>
</div><script>
    $(function() {

        // when row is clicked
        $('#day-grid tbody:first').on('click', 'tr', function() {

            // get the ID
            var id = $(this).find('td:first').text();

            // select the same option in dropdown list with same value
            $('#EmpSched_fk_id_sched')
                .find("option[value="+ id +"]")
                .prop("selected", "selected");
        });
    });fb
</script>        </div><!-- content -->
    </div>
    <div class="span3">
        <div id="sidebar">
        <div class="portlet" id="yw4">
<div class="portlet-decoration">
<div class="portlet-title">Operations</div>
</div>
<div class="portlet-content">
<ul class="operations nav" id="yw5"><li><a href="/hris/index.php?r=scheduling/empSched/index">List EmpSched</a></li><li><a href="/hris/index.php?r=scheduling/empSched/admin">Manage EmpSched</a></li></ul></div>
</div>        </div><!-- sidebar -->
    </div>
</div>

    <div class="clear"></div>

    <div id="footer">
        Copyright &copy; 2013 by My Company.<br/>
        All Rights Reserved.<br/>
        Powered by <a href="http://www.yiiframework.com/" rel="external">Yii Framework</a>. </div><!-- footer -->

</div><!-- page -->

<script type="text/javascript" src="/hris/assets/ac3c9c93/js/bootstrap.datepicker.js"></script>
<script type="text/javascript" src="/hris/assets/ac3c9c93/js/locales/bootstrap-datepicker.en_us.js"></script>
<script type="text/javascript" src="/hris/assets/e689fd44/gridview/jquery.yiigridview.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
jQuery(function($) {
jQuery('a[rel="tooltip"]').tooltip();
jQuery('a[rel="popover"]').popover();
jQuery('#EmpSched_from').bdatepicker({'format':'yyyy-mm-dd','language':'en_us','weekStart':0});
jQuery('#EmpSched_to').bdatepicker({'format':'yyyy-mm-dd','language':'en_us','weekStart':0});
jQuery(document).on('click','#day-grid a.delete',function() {
    if(!confirm('Are you sure you want to delete this item?')) return false;
    var th = this,
        afterDelete = function(){};
    jQuery('#day-grid').yiiGridView('update', {
        type: 'POST',
        url: jQuery(this).attr('href'),
        success: function(data) {
            jQuery('#day-grid').yiiGridView('update');
            afterDelete(th, true, data);
        },
        error: function(XHR) {
            return afterDelete(th, false, XHR);
        }
    });
    return false;
});
jQuery('#day-grid').yiiGridView({'ajaxUpdate':['day-grid'],'ajaxVar':'ajax','pagerClass':'pagination','loadingClass':'grid-view-loading','filterClass':'filters','tableClass':'items table','selectableRows':1,'enableHistory':false,'updateSelector':'{page}, {sort}','filterSelector':'{filter}','pageVar':'day_page','afterAjaxUpdate':function() {
            jQuery('.popover').remove();
            jQuery('a[rel="popover"]').popover();
            jQuery('.tooltip').remove();
            jQuery('a[rel="tooltip"]').tooltip();
        }});
});
/*]]>*/
</script>
</body>
</html>

我希望day table位于EmpSched的create.php中,

3 个答案:

答案 0 :(得分:1)

好的,您必须将该网格放入创建表单中。为此,EmpSched控制器的actionCreate将需要以下附加代码:

$this->render('create', array(
    'model' => $model,
    'emp' => new CActiveDataProvider('day'), // add this line
));

在你的create.php中添加gridview:

<?php $this->widget('bootstrap.widgets.TbGridView',array(
    'id'=>'day-grid',
    'dataProvider'=>$emp,
...

答案 1 :(得分:0)

JavaScript(jQuery v1.9 +):

$(function() {

    // when row is clicked
    $('#day-grid tbody:first').on('click', 'tr', function() {

        // get the ID
        var id = $(this).find('td:first').text();

        // select the same option in dropdown list with same value
        $('#EmpSched_fk_id_sched')
            .find("option[value="+ id +"]")
            .prop("selected", "selected");
    });
});

答案 2 :(得分:0)

感谢Samuel Liew先生,我解决了我工作了几天的问题。

这就是我所做的。在我的EmpSched _form.php

<div>
<?php echo $form->labelEx($model,'fk_id_sched'); ?>
<?php echo $form->dropDownList($model, 'fk_id_sched', CHtml::listData(
    Schedule::model()->findAll(), 'id_sched', 'sched_name'
)
); ?>
<?php echo $form->error($model,'fk_id_sched'); ?>
</div>

和我的EmpSched create.php

<?php
$this->breadcrumbs=array(
'Emp Scheds'=>array('index'),
'Create',
);

$this->menu=array(
array('label'=>'List EmpSched','url'=>array('index')),
array('label'=>'Manage EmpSched','url'=>array('admin')),
);
?>

<h1>Create EmpSched</h1>

<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>

<?php $this->widget('bootstrap.widgets.TbGridView',array(
'id'=>'day-grid',
'dataProvider'=>$emp,
'columns'=>array(
    'id_day',
    'name',
    'mon',
    'tue',
    'wed',
    'thurs',
    'fri',
    'sat',
    'sun',

),
));
?>
<script>
$(function() {

    // when row is clicked
    $('#day-grid tbody:first').on('click', 'tr', function() {

        // get the ID
        var id = $(this).find('td:first').text();

        // select the same option in dropdown list with same value
        $('#EmpSched_fk_id_sched')
            .find("option[value="+ id +"]")
            .prop("selected", "selected");
    });
});
</script>

并将其添加到EmpSched actionCreate:

$this->render('create', array(
'model' => $model,
'emp' => new CActiveDataProvider('day'), // add this line
));

以下是输出: http://imageshack.us/photo/my-images/600/8pzx.png/