Kendo UI Grid Toolbar - pass dataItem to DestroyButton/EditButton - razor

I have a Kendo UI grid with a toolbar (Razor syntax) and I cannot figure out what to pass as an argument into the DestroyButton/EditButton. I know the button expects "dataItem" - but how do I get that?
This is my grid:
#(Html.Kendo().Grid<AspNetPermission>().Name("grid")
.DataSource(ds => ds.Ajax().Read(read => read.Action("Permissions_Read", "AspNetPermissions"))
.Model(model => model.Id(p => p.Id))
.Create(create => create.Action("Create", "AspNetPermissions").Data("sendAntiForgery"))
.Update(update => update.Action("Edit", "AspNetPermissions").Data("sendAntiForgery"))
.Destroy(destroy => destroy.Action("Delete", "AspNetPermissions").Data("sendAntiForgery"))
)
.Columns(columns =>
{
columns.Bound(permission => permission.Name).Width(300);
})
.Selectable(selectable => selectable
.Mode(GridSelectionMode.Single)
.Type(GridSelectionType.Row))
.Sortable()
.Pageable()
.Editable(edit => edit.Enabled(true))
.ToolBar(tb => tb.Template(
#<text>
#*display buttons only if user is enabled*#
#if (ViewBag.CanUserManage == true)
{
#item.CreateButton("Add", new{#class="SPAdd"})
#item.EditButton(whatToPutHere) <!-- This is where I have a problem -->
#item.DestroyButton(whatToPutHere) <!-- This is where I have a problem -->
#item.SaveButton("Save","Cancel",new{#class="SPSave"})
}
</text>
))
.Events(events => events
.DataBinding("onGridDataBinding")
.Change("onGridSelectionChanged")
.DataBound("onGridDataBound")
)
)
}
Thx in Advance,
Frank

Something similar to this:
.ToolBar(b => b.Template(#"<div class='grid-column-right'>
<a class='k-button k-button-icontext k-grid-add' style='margin-right:40px; ' href='javascript: add(0)'>+ Add</a>
<a class='k-button k-button-icontext k-grid-refresh' href='javascript: refreshGridByName(""FilePattern#=uid#"")'>Refresh</a>
</div>"))

Related

'target' => '_blank' doesn't work. Rendering click in another tab

I have following code presenting picture from database(Blob) in GridView::widget. On click at picture, another controller method will be rendered, but in the same tab. I want to be rendered this method in another tab.
'target' => '_blank'
will not work with this code. How can I achieve this?
[
'attribute' => 'photo',
'label' => 'Photo',
'format' => 'raw', // hier nicht html,da das Bild sonst nicht angezeigt wird
'vAlign' => 'middle',
'value' => function($model) {
$url = 'data:image/jpeg;base64,' . base64_encode($model->photo);
return Html::a(
Html::img($url, ['alt' => 'Kein Photo gefunden', 'class' => 'img-circle', 'style' => 'width:50px;height:50px']), [Url::to('ds-substanz/vergroessern'), 'id' => $model->id], ['title' => 'Photo vergrößern', 'target' => '_blank']
);
}
],
Check if your gridview is not enclosed in Pjax. In this case you should use linkSelector in Pjax for other required links.
Otherwise, it is better to use JavaScript and jQuery or ...
The browser may ignore it. They have settings for such issues, of course your syntax is correct.
You can do the following:
Html::a('IMG.....', ['ds-substanz/vergroessern', 'id' => $model->id], ['onclick'=>"window.open(this.href,'_blank');return false;",'target' => '_blank']);
Good luck.

How to fix Kendo Grid MVC displaying footer total on first row?

I am not sure why this grid is showing footer totals on the first row of the grid. Has anyone run into this issue and able to fix it?
This is for the grid total not group totals:
Looks like the <tfoot> element is generated before the <tbody> element.
Grid razor code.
#(Html.Kendo().Grid(Model)
.Name("SalesSummaryGrid")
.Columns(c =>
{
c.Bound(p => p.GroupName).Title("Sales Type").Hidden();
c.Bound(p => p.Title).Title("CATEGORY")
.FooterTemplate(#<text>Total: </text>)
.Width(200);
c.Bound(p => p.Value).Title("GROSS")
.Width(100)
.FooterTemplate(#<text>#item.Sum</text>)
.Format("{0:C2}");
c.Bound(p => p.Comps).Title("COMP")
.Width(100)
.Format("{0:C2}");
c.Bound(p => p.Discounts).Title("DISCOUNTS")
.Width(100)
.Format("{0:C2}");
c.Bound(p => p.Voids).Title("VOIDS")
.Width(100)
.Format("{0:C2}");
c.Bound(p => p.NetSales).Title("NET")
.Width(100)
.Format("{0:C2}");
c.Bound(p => p.NetPercentage).Title("% OF NET")
.Width(100)
.Format("{0:P2}");
})
.Sortable()
.DataSource(dataSource => dataSource
.Server()
.Aggregates(aggregates =>
{
aggregates.Add(p => p.Value).Sum();
})
.Group(groups => groups.Add(p => p.GroupName))
)
)
I tried many different things, and stripped a lot of formatting from this grid. I believe this should work out of the box.

How to use custom array in yii2 dataprovider gridView?

I have the following array at the top of the view file:
$order_status = array(
'nocourier' => 'در حال جستجوی پیک',
'accepted' => 'پیک تعیین شد',
'picking' => 'در حال دریافت مرسوله',
'delivered' => 'تحویل داده شد'
);
And later in the page, I want to use it inside one of the columns of the dataprovider table as below:
[
'label' => 'Status',
'format' => 'raw',
'value' => function ($model, $order_status) {
return Html::a("<div class='col-sm-8 progress' style='padding: 0px; height: 10px;'>
<div class='progress-bar ".$model->status."'></div>
</div><label class='col-sm-4'>".$order_status[$model->status]."</label>", null);
},
'headerOptions' => ['style' => 'text-align: center;'],
'contentOptions' => ['style' => 'width: 300px;']
]
But I get empty label. What am I missing?
could be you need use for pass the content of the array to the anonymous function eg:
'value' => function ($model) use ($order_status){
return Html::a("<div class='col-sm-8 progress' style='padding: 0px; height: 10px;'>
<div class='progress-bar ".$model->status."'></div>
</div><label class='col-sm-4'>".$order_status[$model->status]."</label>", null);
},
i dont know this language but i can se one things that is wrong.
you are creating a function that containe $order_status. so when you call Value you have to pass on $order_status
Value($model , $order_status) for it to work.
i would call $order_status parameter for somthing else too.

Getting label with colon for model's field in a form

I need to have Body: (with colon at the end), not Body rendered as label for each field in my form. How can I achieve this the best way?
I tried modifying fieldConfig => template in ActiveForm::begin by adding div class=\"\">{label}:</div> into it:
<?php $form = ActiveForm::begin([
'id' => 'edit-form',
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'template' => "<div class=\"\">{label}:</div>\n<div class=\"\">{input}</div>\n<div class=\"\">{error}</div>",
'labelOptions' => ['class' => 'edit-label'],
]]); ?>
but it is wrong. Colon is rendered as separate DOM element, with incorrect styling and looks ugly.
I tried doing this awfully in CSS:
.edit-label::after {
content: ":";
}
but this is even worse.
I remember, that I made a lot of stupid things in Yii1 to get this. I don't want to repeat these stupid things, when implementing this in Yii2. What is the best way of achieving this?
When using Bootstrap 3 (yii\bootstrap\ActiveField) you can use additional placeholders in the $template and you need to replace {label} with {beginLabel}{labelTitle}:{endLabel}:
<?php $form = ActiveForm::begin([
'id' => 'edit-form',
'options' => [
'class' => 'form-horizontal',
'enctype'=>'multipart/form-data'
],
'fieldConfig' => [
'template' => "<div class=\"\">{beginLabel}{labelTitle}:{endLabel}</div>\n<div class=\"\">{input}</div>\n<div class=\"\">{error}</div>",
'labelOptions' => ['class' => 'edit-label'],
],
]); ?>
I don't know, how to solve this problem, if you're using basic yii\widgets\ActiveField instead.

Zend subform view script render

I would rather not deal with decorators as my form design is not exactly straight forward, but i would like to keep the functionality of validating the forms.
So i have it set up where sub forms are working correctly, but when i try to style it manually in my viewscript i get the name without the parent. I've seen other posts that are similar, but i haven't found a solution.
Example:
This is in my view script
<?php echo $this->form->username->renderViewHelper();?>
I then get
<input type="text" value="" id="username" name="username">
When rendered. It should be
<input type="text" value="" id="form1-username" name="form1[username]">
How do i get that form1 portion?
Thanks!
Edit
Ok, so i found one way.
By using belongTo, it works:
$form1->addElements(array(
new Zend_Form_Element_Text('username', array(
'belongsTo' => 'form1',
'required' => true,
'label' => 'Username:',
'filters' => array('StringTrim', 'StringToLower'),
'validators' => array(
'Alnum',
array('Regex',
false,
array('/^[a-z][a-z0-9]{2,}$/'))
)
))
));
Is there a better way to do this or is this the only way?
Edit2
public function prepareSubForm($spec){
if (is_string($spec)) {
$subForm = $this->{$spec};
} elseif ($spec instanceof Zend_Form_SubForm) {
$subForm = $spec;
} else {
throw new Exception('Invalid argument passed to ' .
__FUNCTION__ . '()');
}
$this->setSubFormDecorators($subForm)
->addSubmitButton($subForm)
->addSubFormActions($subForm);
return $subForm;
}
public function setSubFormDecorators(Zend_Form_SubForm $subForm){
$subForm->setDecorators(array(
'FormElements', \\<--- I tried to change this to PrepareElements before.
array('HtmlTag', array('tag' => 'dl',
'class' => 'zend_form')),
'Form',
));
return $this;
}
I believe you can get your desired output just by using:
<?php echo $this->form->username; ?>
I get the expected output when calling this without renderViewHelper. This is also without any special code for decorators or preparing sub forms. All I had to do was add belongsTo to the form element.
UPDATED:
If you set this to be your default decorator, you can eliminate the dd/dt tags from rendering, instead it will use a div. Then you may be closer to getting the custom output you want. You can change the tag in HtmlTag from div to whatever tag you would like to wrap your elements in. This is what I use mostly:
array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description')),
array('HtmlTag', array('tag' => 'div', 'class' => 'form-div')),
array('Label', array('class' => 'form-label', 'requiredSuffix' => '*'))
);
This is the default for Zend Framework:
array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description')),
array('HtmlTag', array('tag' => 'dd', 'id' => array('callback' => $getId)))
array('Label', array('tag' => 'dt'))
);
Note that file, and submit/button elements use different decorators.
Also see this answer