Checkbox column with Kendo grid - c#-4.0

I wanted to add a checkbox column as first column to below grid.
Can some one help me how to add it?
#(Html.Kendo().Grid(Model)
.Name("items")
.Columns(columns =>
{
columns.Bound(p => p.itemname).Title("Name");
columns.Bound(p => p.cost).Title("Cost");
columns.Bound(p => p.stockinhand).Title("Stock in hand");
columns.Command(command => command.Destroy()).Width(100);
})
.Pageable()
.DataSource(dataSource => dataSource
.Server()
.Model(model => model.Id(p=>p.Id))
.Destroy(update => update.Action("EditingInline_Destroy", "Grid"))
)
)

This is how I did it:
columns.Template(#<text></text>).ClientTemplate("<input type='checkbox' #= IsAdmin ? checked='checked':'' # class='chkbx' />")
and then on javascript:
$(function () {
$('#grid').on('click', '.chkbx', function () {
var checked = $(this).is(':checked');
var grid = $('#grid').data().kendoGrid;
var dataItem = grid.dataItem($(this).closest('tr'));
dataItem.set('IsAdmin', checked);
})
})

Hi you can add Checkbox in Header and Column like below :
columns.Bound(p => p.Status).HeaderTemplate("<input id='selectall' class='chkbx' type='checkbox' onclick='ToggleChkBox(this.checked);' />").ClientTemplate("<input id='checkbox' onclick='grdChkBoxClick(this); ' class='chkbxq' type='checkbox' />").Sortable(false).Filterable(false).Width(30);
And FInd Checkbox click like below :
//Cell click Checkbox select
$('#Grid').on("click", "td", function (e) {
var selectedTd = $(e.target).closest("td");
var grdChkBox = selectedTd.parents('tr').find("td:first").next("td").find('input:checkbox');
grdChkBox.prop('checked', !grdChkBox.prop('checked'));
});
And do Check all checkbox functionality like below :
function ToggleChkBox(flag) {
$('.chkbxq').each(function () {
$(this).attr('checked', flag);
});
}

I normally add a boolean column in the model; like following.
#(Html.Kendo().Grid(Model)
.Name("items")
.Columns(columns =>
{
columns.Bound(p => p.status).ClientTemplate("<input type='checkbox' disabled #= status == true ? checked='checked' : '' # />");
columns.Bound(p => p.itemname).Title("Name");
columns.Bound(p => p.cost).Title("Cost");
columns.Bound(p => p.stockinhand).Title("Stock in hand");
columns.Command(command => command.Destroy()).Width(100);
})
.Pageable()
.DataSource(dataSource => dataSource
.Server()
.Model(model => model.Id(p=>p.Id))
.Destroy(update => update.Action("EditingInline_Destroy", "Grid"))
)
)
And to disable it until you press "Edit" button just add "disabled" in the ClientTemplate. That should do it. Thanks.

you can add checkbox in each row with header using this,
#(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
.Name("Grid")
.Columns(columns => {
columns.Select();
columns.Bound(p => p.ProductName);
columns.Bound(p => p.UnitPrice);
columns.Bound(p => p.UnitsInStock);
columns.Bound(p => p.Discontinued);
})
.Pageable()
.Sortable()
.Events(ev=>ev.Change("onChange"))
.PersistSelection()
.DataSource(dataSource => dataSource
.Ajax()
.Model(model => model.Id(p => p.ProductID))
.Read(read => read.Action("Selection_Read", "Grid"))
))
Here we use PersistSelection() for persistting selected items across all pages.
If column.Select() gives error or not bind the grid then upgrade your kendo UI version. It will work.

Related

Laravel Excel Multiple table primary_id save to another table as a foreign_id

I can successfully import Excel Data in Laravel and Insert into Database multiple tables. I am using maatwebsite/excel version 3 composer package and laravel version 5.8
How can I insert mamber_id value in licence_application table (foreign id) which is member table primary id.
ImportClass file:
class UsersImport implements ToCollection
{
/**
* #param Collection $collection
*/
public function collection(Collection $rows)
{
foreach ($rows as $row){
Member::create([
'full_name' => $row[0],
'father_name' => $row[1],
'mother_name' => $row[2],
'cell_number' => $row[3],
]);
LicenceApplication::create([
'member_id' => ???
'licence_category_name' => $row[4],
]);
LicenceSuccess::create([
'application_type' => $row[5],
]);
}
}
}
Controller file: ImporExcelController.php
public function import(Request $request)
{
$this->validate($request, [
'select_file' => 'required|mimes:xls,xlsx'
]);
$path = $request->file('select_file')->getRealPath();
$data = Excel::import(new UsersImport, $path);
return back()->with('success', 'Import data successfully!');
}
Excel file screenshot: https://imgur.com/a/XhxBRpW
Please help me how to solve this issue?
I solve this issue here is the code.
foreach ($rows as $row){
$memberId = Member::create([
'full_name' => $row['full_name'],
'father_name' => $row['father_name'],
'mother_name' => $row['mother_name'],
'cell_number' => $row['cell_number'],
]);
$applicationId = LicenceApplication::create([
'member_id' => $memberId->member_id,
'licence_category_name' => $row['licence_category_name'],
]);
LicenceSuccess::create([
'member_id' => $memberId->member_id,
'application_id' => $applicationId->application_id,
'application_type' => $row['application_type'],
]);
}

Enable/Disable ASP.NET MVC Kendo grid custom template controls while Edit and Update

Enable/Disable Kendo grid custom template controls while Edit and Update.
Below code is helps you to Enable/Disable Kendo grid custom template controls
Kendo Grid.
#(Html.Kendo().Grid<Models.YourModelView>()
.Name("GridName")
.DataSource(datasource => datasource
.Ajax()
.Model(module =>
{
module.Id("ID");
})
.Read(read => read.Action("ReadAction", "YourController"))
.Create(create => create.Action("CreateAction", "YourController"))
.Update(update => update.Action("EditAction", "YourController"))
)
.Columns(columns =>
{
columns.Bound(p => p.ID).Hidden();
columns.Bound(p => p.Name).Title("Prefix");
columns.Command(command => { command.Edit(); });
})
.Editable(editable =>
{
editable.Mode(GridEditMode.PopUp).TemplateName("_AddNewCorporateGroup").Window(w => w.Title("Add/Update").Width(600));
})
.Events(e => e.Edit("onEdit"))
)
JavaScript Method.
function onEdit(e) {
var kendoWindow = e.container.data("GridName");
if (e.model.isNew()) {
$('#Custom template Controller Name').fadeIn();
} else {
$('#Custom template Controller Name').fadeOut();
}
}

Yii2 Modify find() Method in Model search()

I am trying to modify the find() method inside the model search and it throws an error "The data provider property must be set".
Here is my search model:
public function search($params)
{
$userID = Yii::$app->user->identity->id;
$groups = GroupAccess::find()
->where(['user_id' => $userID, 'item_name' => 'group_creator'])
->asArray()
->all();
foreach ($groups as $group) {
$accessGroups[] = $group['group_id'];
}
$query = Group::find($accessGroups);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'status_id' => $this->status_id,
//'created_user_id' => $this->created_user_id,
'created_date' => $this->created_date,
'profile_updated_user_id' => $this->profile_updated_user_id,
'profile_updated_date' => $this->profile_updated_date,
'last_accessed_user_id' => $this->last_accessed_user_id,
'last_accessed_date' => $this->last_accessed_date,
]);
$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'description', $this->description]);
return $dataProvider;
}
And here is my controller action:
$searchModel = new GroupSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
if (Yii::$app->request->isPjax) {
return $this->renderAjax('groups', [
'searchModel' => $searchModel,
'dataProviderMine' => $dataProvider,
]);
} else {
return $this->render('groups', [
'searchModel' => $searchModel,
'dataProviderMine' => $dataProvider,
]);
}
}
It is important to refine the query as the user should be able to see other groups.
How can i modify the find() method properly?
Thanks.
I see two bugs here:
Your find method
$query = Group::find($accessGroups)
will not work - just replace it with
$query = Group::find()->where(['id' => $accessGroups]);
I guess "The data provider property must be set" error is caused by your view code. E.g. if you are using GridView, you should set its 'dataProvider' widget option:
GridView::widget([
'dataProvider' => $dataProviderMine,
'searchModel' => $searchModel,
'columns' => [
'id', 'status_id', 'created_date' // your view columns here
]
])
Consider also using sub queries in your search method:
$idAccessQuery = GroupAccess::find()
->where(['user_id' => $userID, 'item_name' => 'group_creator'])
->select('group_id');
$query = Group::find()->where([
'id' => $idAccessQuery
]);

Alter a hardcoded form of options based on $user already saved

What I'm trying to accomplish
I'm building a favorites module and I need the ability to:
Select from a dropdown, hardcoded list of options
Have it save to the database
Upon refreshing the page, remove the already saved option from the list of options so it may not be added again
The third part is where I am unsure of how to proceed.
How my code is set up
This is my form:
/*
* Implentation of hook_form().
*/
function f25_favorites_form() {
$listOfPaths = f25_favorites_listOfPaths();
$form['path_options'] = array(
'#type' => 'value',
'#value' => array(
'default' => $listOfPaths['default']['#title'],
'concierge' => $listOfPaths['concierge']['#title'],
'concierge/add' => $listOfPaths['concierge/add']['#title'],
'survey-questions' => $listOfPaths['survey-questions']['#title'],
'survey-questions/add' => $listOfPaths['survey-questions/add']['#title'],
'profiles' => $listOfPaths['profiles']['#title'],
'profiles/add' => $listOfPaths['profiles/add']['#title'],
'statistics' => $listOfPaths['statistics']['#title'],
)
);
$form['path'] = array(
'#type' => 'select',
'#title' => t('Select Page'),
'#required' => TRUE,
'#weight' => '11',
'#options' => $form['path_options']['#value'],
);
$form[submit] = array(
'#type' => 'submit',
'#weight' => '1000000',
'#value' => t('Add')
);
return $form;
}
The name of the paths/options are called via a reference array:
/*
* List of Paths to add to favorites
*/
function f25_favorites_listOfPaths() {
$list = array();
$list = array(
'default' => array(
'#title' => t('Add to favorites'),
),
'concierge' => array(
'#title' => t('Concierge'),
'#image' => drupal_get_path('module', 'f25_favorites').'/img/concierge.png',
'#desc' => t('Concierge'),
),
'concierge/add' => array(
'#title' => t('New Concierge'),
'#image' => drupal_get_path('module', 'f25_favorites').'/img/concierge.png',
'#desc' => t('Concierge > Add'),
),
'survey-questions' => array(
'#title' => t('Survey Questions'),
'#image' => drupal_get_path('module', 'f25_favorites').'/img/survey-questions.png',
'#desc' => t('Current Survey Questions'),
),
'survey-questions/add' => array(
'#title' => t('New Survey Question'),
'#image' => drupal_get_path('module', 'f25_favorites').'/img/survey-questions.png',
'#desc' => t('Survery Question > Add'),
),
'profiles' => array(
'#title' => t('Profiles'),
'#image' => drupal_get_path('module', 'f25_favorites').'/img/profiles.png',
'#desc' => t('User Profiles'),
),
'profiles/add' => array(
'#title' => t('Add Profile'),
'#image' => drupal_get_path('module', 'f25_favorites').'/img/profiles.png',
'#desc' => t('Profiles > Add'),
),
'statistics' => array(
'#title' => t('Statistics'),
'#image' => drupal_get_path('module', 'f25_favorites').'/img/statistics.png',
'#desc' => t('Performance Stats'),
),
);
return $list;
}
And all this is what grabs the data on the databse:
/*
* Write Form data to database
*/
function f25_favorites_form_submit($form, &$form_state){
global $user;
$listOfPaths = f25_favorites_listOfPaths();
$selected = $form_state['values']['path'];
$data = array(
'uid' => $user->uid,
'path' => $selected,
'title' => $listOfPaths[$selected]['#title'],
'weight' => 10,
'timestamp' => time(),
);
drupal_write_record(f25_favorites, $data);
}
Possible Solutions
I've been told that I could used hook_form_alter() in order to modify my array but I am unsure as to when I should be comparing the db_query to my array and how to modify the differences accordingly.
I hope I've done a good job explaining what I'm try to do.
What would be the best way to accomplish this?
Instead of writing every response in f25_favorites_listOfPaths(), shouldn't you get them from the database?
You can then change whatever you want in the submit function to the database so that you don't fetch again the previously selected answer.
Example :
function f25_favorites_listOfPaths() {
return variable_get('f25_favorites_array_' . $user->uid, array(
// your $list array
));
}
function f25_favorites_submit_form($form, &$form_state) {
// your stuff already
drupal_write_record(f25_favorites, $data);
// Now what I propose you to do :)
variable_set('f25_favorites_array_' . $user->uid, array(
// new $list array without the favorite selected
));
}
The use of variable_get/set() should of course be replaced by your own table if you have too much datas.
P.S. : hook_form() does not exist :)

drupal_get_form is not passing along node array

I have not been able to get drupal_get_form to pass on the node data. Code snippets are below. The drupal_get_form documentation (api.drupal.org) states that it will pass on the extra parameters. I am basing the node data not being passed because (apparently) $node['language'] is not defined in hook_form which causes $form['qqq'] not to be created and thus the preview button shows up.
My goal here is that the preview button show up using path "node/add/author" but doesn't show up for "milan/author/add". Any alternative methods for achieving this goal would be helpful but the question I want answered is in the preceding paragraph. Everything I've read indicates that it should work.
This menu item
$items['milan/author/add'] = array(
'title' => 'Add Author',
'page callback' => 'get_author_form',
'access arguments' => array('access content'),
'file' => 'author.pages.inc',
);
calls this code
function get_author_form() {
//return node_form(NULL,NULL);
//return drupal_get_form('author_form');
return author_ajax_form('author');
}
function author_ajax_form($type) {
global $user;
module_load_include('inc', 'node', 'node.pages');
$types = node_get_types();
$type = isset($type) ? str_replace('-', '_', $type) : NULL;
// If a node type has been specified, validate its existence.
if (isset($types[$type]) && node_access('create', $type)) {
// Initialize settings:
$node = array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => $type, 'language' => 'bbb','bbb' => 'TRUE');
$output = drupal_get_form($type .'_node_form', $node);
}
return $output;
}
And here is the hook_form and hook_form_alter code
function author_form_author_node_form_alter(&$form, &$form_state) {
$form['author']=NULL;
$form['taxonomy']=NULL;
$form['options']=NULL;
$form['menu']=NULL;
$form['comment_settings']=NULL;
$form['files']=NULL;
$form['revision_information']=NULL;
$form['attachments']=NULL;
if($form["qqq"]) {
$form['buttons']['preview']=NULL;
}
}
function author_form(&$node) {
return make_author_form(&$node);
}
function make_author_form(&$node) {
global $user;
$type = node_get_types('type', $node);
$node = author_make_title($node);
drupal_set_breadcrumb(array(l(t('Home'), NULL), l(t($node->title), 'node/' . $node->nid)));
$form['authorset'] = array(
'#type' => 'fieldset',
'#title' => t('Author'),
'#weight' => -50,
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['author_id'] = array(
'#access' => user_access('create pd_recluse entries'),
'#type' => 'hidden',
'#default_value' => $node->author_id,
'#weight' => -20
);
$form['authorset']['last_name'] = array(
'#type' => 'textfield',
'#title' => t('Last Name'),
'#maxlength' => 60,
'#default_value' => $node->last_name
);
$form['authorset']['first_name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#maxlength' => 60,
'#default_value' => $node->first_name
);
$form['authorset']['middle_name'] = array(
'#type' => 'textfield',
'#title' => t('Middle Name'),
'#maxlength' => 60,
'#default_value' => $node->middle_name
);
$form['authorset']['suffix_name'] = array(
'#type' => 'textfield',
'#title' => t('Suffix Name'),
'#maxlength' => 14,
'#default_value' => $node->suffix_name
);
$form['authorset']['body_filter']['body'] = array(
'#access' => user_access('create pd_recluse entries'),
'#type' => 'textarea',
'#title' => 'Describe Author',
'#default_value' => $node->body,
'#required' => FALSE,
'#weight' => -19
);
$form['status'] = array(
'#type' => 'hidden',
'#default_value' => '1'
);
$form['promote'] = array(
'#type' => 'hidden',
'#default_value' => '1'
);
$form['name'] = array(
'#type' => 'hidden',
'#default_value' => $user->name
);
$form['format'] = array(
'#type' => 'hidden',
'#default_value' => '1'
);
// NOTE in node_example there is some addition code here not needed for this simple node-type
$thepath='milan/author';
if($_REQUEST["theletter"]) {
$thepath .= "/" . $_REQUEST["theletter"];
}
if($node['language']) {
$thepath='milan/authorajaxclose';
$form['qqq'] = array(
'#type' => 'hidden',
'#default_value' => '1'
);
}
$form['#redirect'] = $thepath;
return $form;
}
That menu path coincides with this theme (PHPTemplate)
This might not be it but I see that you use $node as an object at first (title) and then as an array (to get the language) in the make_author_form() method. If $node is an object, then that explains why you cant retrieve $node['language'].
Not sure if I completely understand what you are trying to do but it would be a good idea to use page arguments for it, I think.
function mymodule_form_alter($form_id, &$form) {
// If $form_id is {node->type}_node_form
// Then, check for the first argument in the URL and hide/show Preview accordingly
}
Turned out to be a basic programming error in line 4 of the make_author_form function. I was zeroing out the $node variable myself.

Resources