KendoUI-Grid, CustomCommand - enable/disable - kendo-grid

I wants to enable/disable custom command in kendo-ui grid based on some other column.
How do i achieve that? Below is sample code
#(Html.Kendo().Grid<Model>()
.Name("Grid")
.Columns(columns =>
{
columns.Bound(a => a.Name).Width(90);
columns.Bound(a => a.Flag).Width(170);
columns.Command(commands => commands.Custom("ABC").Text("ABC").Click("ABC")).Title ("ABC").Width(130);
...
}
I want to enable/disable ABC command button based on flag value. Please help me.

You cannot do this through the command column.
I would suggest you to create template column which displays button based on your condition. How to execute conditional logic inside a template (or use external function to handle for complex logic) is covered here.

I solved the problem. Below is the sample code.
columns.Bound(a => a.Flag).ClientTemplate(
"# if(Flag == 'Value1') { #" + "<input type='button'
class='info k-button k-button- icontext' onclick='clickhere(#=parameter1#)'
value='ClickHere' />" +
"# } else { #" +
"NormalText" +
"# } #").Width(100).Title("ConditionalButton");
//JavaScript
<script type="text/javascript">
function clickhere(parameter1) {
window.location.href = '#Url.Action("Event", "Home")' +
'?parameter1=' + parameter1;
}
</script>

Related

Custom row/checkbox template for Kendo UI grid

I am trying to create a custom row template for a Kendo UI grid that includes the checkbox functionality. I have the grid created like this:
#(Html.Kendo().Grid(Model.Users)
.Name("usersGrid")
.Columns(columns =>
{
columns.Select().Width(50);
columns.Bound(c => c.UserName);
})
.DataSource(dataSource => dataSource
.Ajax()
.ServerOperation(false))
.ClientRowTemplate(UsersClientTemplate))
With a function to generate the row template like this:
Func<Grid<User>, string> UsersClientTemplate = (grid) =>
{
var id = Guid.NewGuid();
return #"<tr data-uid='#:uid#' role='row'>" +
"<td role='gridcell'>" +
$"<input class='k-checkbox' data-role='checkbox' type='checkbox' id={id}>" +
$"<label class='k-checkbox-label k-no-text' for={id}>​</label>" +
"</td>" +
"<td role='gridcell'>" +
"<a href='/users/details/#:data.Id#'>#:data.UserName#</a>" +
"</td>" +
"</tr>";
};
The problem is I can't figure out how to generate the unique ids for each row's template. If I don't include the id attribute on the checkbox input element and the for attribute on the checkbox label element, the checkbox/row selection doesn't work. But with the function I have above, the same id is generated for every row template and then the checkbox/row selection only works for the first row, obviously.
What is a better way to do this?
Templates are generated when the grid is built so as you have seen the id is always the same. What you want is to databind so kendo replaces the id with each record. I use my entity key with a prefix like "cb_" for checkbox:
$"<input class='k-checkbox' data-role='checkbox' type='checkbox' id='cb_#:uid#'>" +
$"<label class='k-checkbox-label k-no-text' for='cb_#:uid#'>​</label>" +

ClientTemplate expression to include javascript conditional

Have been using Kendo UI MVC for a while now and need to put some conditional logic on a checkbox that is in one field of the grid.
The fields in the grid that need to be evaluated are
Going off the example here
https://docs.telerik.com/aspnet-mvc/helpers/grid/faq#how-to-display-checkboxes-in-ajax-bound-grids
columns.Bound(p => p.ProductName).ClientTemplate(
"# if (HasIcon == true) { #" +
"<img src='" + Url.Content("~/Content/icons/") + "#= ProductID #.png' alt='#= ProductName # icon' />" +
"# } else { #" +
"#: ProductName #" +
"# } #"
);
How do I format the test within the conditional to use a grid field value ?
eg testing if ProductID is a specific value like this?
columns.Bound(p => p.ProductName).ClientTemplate(
"# if (#= ProductID # == 123) { #" +
"<img src='" + Url.Content("~/Content/icons/") + "#= ProductID #.png' alt='#= ProductName # icon' />" +
"# } else { #" +
"#: ProductName #" +
"# } #"
);
due to the sometimes complex nature of client templates, I personally tend to use the javascript function method
columns.Bound(p => p.ProductName).ClientTemplate("#= getProductIcon(data,
Url.Content("~/Content/icons/")#);
then in a js script block
function getProductIcon(data, imgUrl) {
if (data.ProductID == 123)
return "<img src='" + imgUrl + data.ProductID + ".png' alt='" + data.ProductName + " icon' />";
else
return data.ProductName;
}
just make sure your script is loaded before the grid is instantiated otherwise you'll run into getProductIcon undefined or some similar error. Also it's easier to throw a debugger on your javascript function and really see what's going on and verify the data in/out.
Excuse any typos in the above, I'm not at a location where I can validate the razor syntax at the moment.

Add class to bootstrap textbox - yii2

I have a form in yii2. In that I have a input field like below.
<input type="text" class="form-control" id="productsales-<?= $i ?>-value">
It's inside a dynamic form andno coming from any model.It's just to hold some calculated value. And the idea is to calculate the values fileed by users in this field to another textbox like below -
<?= $form->field($model, 'amount')->textInput(['maxlength' => true, 'class' => 'sum']) ?>
I already have the javascript code to calculate the total amount like below -
<?php
/* start getting the total amount */
$script = <<<EOD
var getSum = function() {
var items = $(".item");
var sum = 0;
items.each(function (index, elem) {
var priceValue = $(elem).find(".sumPart").val();
//Check if priceValue is numeric or something like that
sum = (parseFloat(sum) + parseFloat(priceValue)).toFixed(2);
});
//Assign the sum value to the field
$(".sum").val(sum);
};
//Bind new elements to support the function too
$(".container-items").on("change", ".sumPart", function() {
getSum();
});
EOD;
$this->registerJs($script);
/*end getting the total amount */
?>
Please note that I have a class sum in the second textbox which comes from a model. My question is that how can I add a class(sumPart) like this(sum) in the first textbox which is not coming from a model.
Update after discussion with Insane Skull
I have a dynamic form like below
I want to calculate sum of all the entries in Value and pass it to amount.
The code of Value is -
<input type="text" class="form-control sumPart" id="productsales-<?= $i ?>-value">
The code of Amount is -
<?= $form->field($model, 'amount')->textInput(['maxlength' => true, 'class' => 'sum']) ?>
The javascript I've tried is already given above.
The problem with this code is the calculated "value" is not passing to amount textbox.
Update: A little issue
Modify JS :
$this->registerJs('
function getSum() {
var sum = 0;
var items = $(".item");
items.each(function (index, elem) {
var priceValue = $(elem).find(".sumPart").val();
sum = parseFloat(sum) + parseFloat(priceValue);
});
if(isNaN(sum) || sum.length == 0) {
sum = 0;
}
$(".sum").val(sum);
}
$(".container-items").on("change", function() {
getSum();
});
jQuery(".dynamicform_wrapper").on("afterDelete", function(e) {
getSum();
});
');
I am little unsure about the problem you are facing. If you want to add a class to a input field, you can easily do so in the class attribute of the input tag. And the class attribute does support multiple values, so you can write something like this:
<input type="text" class="form-control sumPart" id="productsales-<?= $i ?>-value">
**UPDATE -- based on comment by OP **
#Tanmay, I understand that you may not be getting the desired output. However, it should not really matter if we are rendering the fields using $form->field() or plain HTML. If the fields have the proper classes assigned, then the JS code posted by Kostas in your referenced link should work - irrespective of how the class name was added to the input field.
Can you please confirm (by using Inspect Element) if the sumPart class is actually assigned to the relevant fields once the page loads? Maybe some JS is overwriting the class attribute again?
If the classes are assigned properly, please check if there are any other JS errors in console which may prevent the sum calculation JS from working properly.

Yii2 Dynagrid: How to implement Sortable to sort the rows

In Dynagrid it is possible to use Sortable to sort the columns. I need to enable sorting rows, which should work similarly:
Is there a way to implement such sorting directly in Dynagrid or do I have to programm the sorting separately?
To enable the sorting is simple:
$('tbody').sortable();
The only problem is to save the sorting to the database. I have decided to save it after a click on a button (and not with ajax) in views/sorted-table/index.php:
if (strpos(Url::current(), 'sort') === false) {
$form = ActiveForm::begin([
'id' => 'sort-form',
]);
echo '<input type="hidden" id="field-sort" name="sort" value="">';
if (isset($sortMessage) && $sortMessage != '') {
echo '<p>' . $sortMessage . '</p>';
}
echo Html::submitButton(Icon::show('floppy-o') . 'Sortierung speichern', ['class' => 'btn btn-primary']);
ActiveForm::end();
DynagridSortAsset::register($this);
}
The positions of rows are saved in the hidden input field by jQuery:
$('.btn-primary').mousedown(function() {
var sorting = "";
$('.sortable-row').each( function (index, value) {
sorting += $(this).attr('data-id') + ':' + $(this).attr('data-sortierung') +',';
});
$('#field-sortierung').val(sorting);
});
The values are then in controller processed and saved to the database.

Customizing Struts Displaytag table properties

I have knowledge on how to customize the display data using Struts2 displaytag:table and decorator.
But I need to customize the table columns (tr style)
public String getWorkFlow() {
WorkOrderDO currentWorkOrder = (WorkOrderDO) getCurrentRowObject();
String output = null;
if (currentWorkOrder.getChildId() == 0) {
if (!currentWorkOrder.isWorkFlowRetrieved()) {
output = "<input type = 'submit' value = 'Work Flow' id=" + currentWorkOrder.getMoveWorkOrderId() + " onclick = 'changeWorkOrder(this);return false;'/>";
} else {
output = "<input type = 'submit' value = 'Work Flow' id=" + currentWorkOrder.getMoveWorkOrderId() + " onclick = 'changeWorkOrder(this);return false;' disabled/>";
}
}
return output;
}
from above method in decorator class, we can customize the data look and feel.
But my question is, if currentWorkOrder.isWorkFlowRetrieved() == true, I want to highlight the entire column (tr) in the table in the display page with style property
Any idea?
you can do this by using style attribute in the display:column tag
for eg
I hope this works for you.