<ejs-grid id="Grid" dataSource="ViewBag.dataSource" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" })" allowPaging="true" height="300">
. . . . . . . . . . . . .
<e-grid-column field="ShipCountry"headerText="ShipCountry" edit="@(new {create = "create", read = "read", destroy = "destroy", write = "write"})" width="110"></e-grid-column>
<e-grid-column field="ShipName" width="110" edit="@(new {create = "createcity", read = "readcity", destroy = "destroycity", write = "writecity"})"> </e-grid-column>
</e-grid-columns>
</ejs-grid>
<script>
var countryElem;
var countryObj;
var stateElem;
var stateObj;
var country = [
{ countryName: 'United States', countryId: '1' },
{ countryName: 'Australia', countryId: '2' }
];
var state = [
{ stateName: 'New York', countryId: '1', stateId: '101' },
{ stateName: 'Virginia ', countryId: '1', stateId: '102' },
{ stateName: 'Washington', countryId: '1', stateId: '103' },
{ stateName: 'Queensland', countryId: '2', stateId: '104' },
{ stateName: 'Tasmania ', countryId: '2', stateId: '105' },
{ stateName: 'Victoria', countryId: '2', stateId: '106' }
];
function create() {
countryElem = document.createElement('input');
return countryElem;
};
function read() {
return countryObj.text;
};
function destroy() {
countryObj.destroy();
};
function write(args) {
countryObj = new ej.dropdowns.DropDownList({
dataSource: country,
fields: { value: countryId, text: 'countryName' },
value: args.rowData['ShipCountry'],
change: function () {
if (countryObj.text === 'Australia') {
stateObj.enabled = true; // ShipName is enabled only ShipCountry as Australia
}
else { stateObj.enabled = false; }
var tempQuery = new ej.data.Query().where('countryId', 'equal', countryObj.value);
stateObj.query = tempQuery;
stateObj.text = null;
stateObj.dataBind();
},
placeholder: 'Select a country',
floatLabelType: 'Never'
});
countryObj.appendTo(countryElem);
};
function createcity() {
stateElem = document.createElement('input');
return stateElem;
};
function readcity() {
return stateObj.text;
};
function destroycity() {
stateObj.destroy();
};
function writecity(args) {
stateObj = new ej.dropdowns.DropDownList({
dataSource: state,
fields: { value: 'stateId', text: 'stateName' },
enabled: false,
value: args.rowData['ShipName'],
placeholder: 'Select a state',
floatLabelType: 'Never'
});
stateObj.appendTo(stateElem);
}
</script> |
I need to conditionally validate the input of a drop-down
box inside the Add dialogue of a grid. I have a drop-down named Location. The
location is always required. The dropdown/field I want to conditionally
validate is named Reference. When the screen loads, the reference box is disabled
(greyed out). When "Truck" or "Substation" is selected in the location box, I then enable the Reference box. I also need to validate/require the reference
box to have input.
Here are some scenarios:
1) Location contains office. The
reference box is disabled and does not need to be validated.
2) Location contains Truck, the
reference box is enabled and needs to be validated.
3) Location contains Substation,
the reference box is enabled and needs to be validated.
4) Location contains Warehouse
A, the reference box is disabled and does not need to be validated.
Ex 1 : The user
selects Office. When the user clicks save the reference does not need to be
validated.
Ex 2: The User Selects Truck. When the user clicks save the Reference does need to be validated. It should look like this:
. . . . . . . . .
function write(args) {
countryObj = new ej.dropdowns.DropDownList({
dataSource: country,
fields: { value: 'countryId', text: 'countryName' },
value: args.rowData['ShipCountry'],
change: function (args) {
if (countryObj.text === 'Office' || countryObj.text === 'Warehouse A') {
stateObj.enabled = false;
document.getElementsByClassName('e-gridform')[0].ej2_instances[0].rules = {};
}
else {
stateObj.enabled = true;
document.getElementsByClassName('e-gridform')[0].ej2_instances[0].addRules('ShipName', { required: true });
}
var tempQuery = new ej.data.Query().where('countryId', 'equal', countryObj.value);
stateObj.query = tempQuery;
stateObj.text = null;
stateObj.dataBind();
},
placeholder: 'Select a country',
floatLabelType: 'Never'
});
countryObj.appendTo(countryElem);
};
. . . . . . . . .
function writecity(args) {
stateObj = new ej.dropdowns.DropDownList({
dataSource: state,
fields: { value: 'stateId', text: 'stateName' },
enabled: false,
value: args.rowData['ShipName'],
placeholder: 'Select a state',
floatLabelType: 'Never',
enabled: false
});
stateObj.appendTo(stateElem);
}
</script> |