Set deleted records = 1 without actually deleting the records - yii

How can i set the records i want to delete to 1 instead of deleting the record itself? I am using Yii framework right now and i am new to it. I have the CGridview with update, view and delete. On the delete i want the user to set the records which they want to delete to 1, instead of deleting the entire record. Thank you in advance for any help you may provide!

Just update your actionDelete in the controller:
public function actionDelete($id)
{
$model = $this->loadModel($id);
$model->is_deleted = "1"; //assuming is_deleted is the column name
$model->update();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
and your model search function to remove those items which have is_deleted = 1, you can:
//.... rest of your code above
$criteria->addCondition("is_deleted != 1");
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));

Related

Yii2 ValidatePage to give empty output on wrong page number

I am trying to fetch a list of products from a table using my yii2 app and send it accross as a json to lazyload on scroll from the front end. I am using the searchmodel class. Now when data ends, last page data is being send again, i.e., if i have hundred records, the calls for page numbers above 5 would repeatedly send the same data as page number 4. How do i prevent it.
PS: Confused about the usage of the validatePage flag reading the documentation.
Here is my code of the controller.
public function actionAjaxIndex()
{
$searchModel = new productsS();
$response = (object) ['status' => 0];
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$response->status = 1;
$response->data = array();
foreach($dataProvider->models as $row){
foreach($row as $key=>$value){
$customerDetail[$key] = $value;
}
array_push($response->data, $customerDetail);
}
return json_encode($response);
}
Can somebody help with the best possible solution to go ahead.
You need to disable $validatePage for your data provider. This setting overwrites page if it is outside of range (so if you have 4 pages of records, but you request 5th page, pagination will automatically switch to 4th page - every page outside of range will display results for last page).
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->pagination->validatePage = false;

Dynamics CRM. Delete row from subgrid using JS

How can I delete a row from a subgrid using Javascript?
var grid = Xrm.Page.getControl("Produktrader").getGrid();
grid.getRows().forEach(function (row) {
var rowData = row.getData();
if (rowData != null) {
//delete row here
}
}
}
If you want to delete the record in JavaScript you will need to make a REST call to delete the record from the database, and then refresh the page. You can't just delete the record from the subgrid.
Sample: Create, retrieve, update, and delete using the OData endpoint with JavaScript and jQuery
I'll add to James' answer in that after you perform the Delete, you should trigger a subgrid refresh via javascript
Xrm.Page.getControl("Produktrader").refresh();

Typeahead with database as source

This snippet works and it correctly sets data from Table Product and column name as input for bootstrap typeahead extension for YII.
but, I have ended up writing a SELECT ALL from Table Product which is having large number of data.
Can we modify this so that a WHERE condition can be added to the DataProvider on user input event. Based on each alphabet entered, a new query could then be fired and only a subset of data retrieved?
<?php
$dataProvider = new CActiveDataProvider('Product');
$dataArray = $dataProvider->getData();
$myarray = array();
foreach ($dataArray as $data){
array_push($myarray, CHtml::encode($data->name));
}
$this->widget('bootstrap.widgets.TbTypeahead', array(
'name' => 'typeahead',
'options'=>array(
'name'=>'typeahead',
'source'=>$myarray,
'items'=>4,
'matcher'=>"js:function(item) {
return ~item.toLowerCase().indexOf(this.query.toLowerCase());
}",
),
'htmlOptions'=>array('class'=>'search-query span3', 'placeholder'=>"Search" ),
)); ?>
Once you start to supply a function to source, then you have the power to manipulate what happens, including how often you send requests.
minLength: 3, // <- custom option
source: function(query, process) {
var longEnough = query.length >= this.options.minLength;
// you can create custom variables (this.search) that a remembered across
// searches
if (longEnough && (! this.search || whateverRuleYouWantToLimitBy)) {
// remember the query so that you can compare it to the next one
this.search = query;
$.ajax({
url: '/ajaxsearch.php?value=' + query,
type: "GET",
success: process
});
}
}
I have some code that does something similar, and I cache the results returned by the Ajax code, and then I see if the new query string has the potential to change the results (e.g., if you limit by 4 results, but I only have 3 results, then a query that simply adds to the last query (search) has no need to hit the server).
Alternatively, you can kick off a timer that effectively waits for the user to stop typing to avoid the behavior of hitting the server for every key press. Technically, that results in slower feedback, but it's better for the server and mobile users. This is appropriate on sites that have a lot of traffic.

working with CCheckboxcolumn

im new in with yii.
I'm using CGridView with a CCheckBoxColumn to select records but i don't know how to execute an action for all the checked records only. any ideas? i tried before but when i click the submit button, all rows including the one not selected are processed.
this is my coding in admin page:
array(
'id'=>'check-boxes',
'name'=>'check-boxes',
'class'=>'CCheckBoxColumn',
'selectableRows' => '50',
'checked'=>"0",
),
i dont know how to post the checkbox value and everytime i post it, it give value of check-box[].
i submit it using this coding:
echo CHtml::ajaxLink('Submit', Yii::app()->createUrl('controller/action'),
array(
'type'=>'POST',
'data'=>'js:{ids : $.fn.yiiGridView.getChecked("grid-id","check-boxes")}'
));
but the nothing happen when i use this.
how we should pass the value in controller and how the controller works?
thank you.
public function actionActionName()
{
if(isset($_POST['check-boxes'])){
foreach ($_POST['check-boxes'] as $id){
$model = $this->loadModel($id);
CVarDumper::dump($id, 5, true);
}
}
else
CVarDumper::dump('Nothing is selected', 5, true);
}

CGridview filter on page load with pre define value in search field

I am working with the Yii framework.
I have set a value in one of my cgridview filter fields using:
Here is my jQuery to assign a value to the searchfield:
$('#gridviewid').find('input[type=text],textarea,select').filter(':visible:first').val('".$_GET['value']."');
And here my PHP for calling the cgridview:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'bills-grid',
'dataProvider'=>$dataProvider,
'filter'=>$model,
'cssFile'=>Yii::app()->baseUrl . '/css/gridview.css',
'pager'=>array(
'class'=>'AjaxList',
'maxButtonCount'=>25,
'header'=>''
),
'columns' => $dialog->columns(),
'template'=>"<div class=\"tools\">".$dialog->link()." ".CHtml::link($xcel.' Export to excel', array('ExcelAll'))."</div><br />{items}{summary}<div class=\"pager-fix\">{pager}</div>",));
The value appears in the search field and my cgridview works correctly without any issues, but I am unable to trigger the cgridview to refresh or filter. Does anyone know who to trigger the cgridview to filter after page load with a predefined value?
Any help would be greatly appreciated and please let me know if you need additional information.
Thank you.
You can solve the problem without any clientside code modification. In your controller action just set the default value for the attribute as shown below
public function actionAdmin()
{
$model = new Bills();
$model->unsetAttributes();
$model->attribute_name="default filter value";//where attribute_name is the attribute for which you want the default value in the filter search field
if(isset($_GET['Bills'])){
$model->attributes = $_GET['Bills'];
}
$this->render('admin',array('model'=>$model));
}
Have a look at 'default' index action that gii generates:
public function actionIndex()
{
$model = new Bills();
$model->unsetAttributes();
if(isset($_GET['Bills'])){
$model->attributes = $_GET['Bills'];
}
$this->render('index',array('model'=>$model));
}
So if you add one line like: $model->attribute = 'test';, you're done. 'attribute' is of course the attribute that has to have the default filter value (in this case value is 'test') :). So your code looks like:
public function actionIndex()
{
$model = new Bills();
$model->unsetAttributes();
if(isset($_GET['Bills'])){
$model->attributes = $_GET['Bills'];
}
if(!isset($_GET['Bills']['attribute']) {
$model->attribute = 'test';
}
$this->render('index',array('model'=>$model));
}
Of course youre attribute will have a test value (in filter) set up as long as you wont type anything in its filter field. I hope that that's what you're looking for. Your filter should work as always.
Sorry for my bad english :)
Regards
You can use Yii's update:
$.fn.yiiGridView.update('bills-grid', {
type: 'GET',
url: <?php echo Yii::app()->createUrl('controller/action') ?>"?Class[attribute]=<?php echo $_GET['value'] ?>
success: function() {
$.fn.yiiGridView.update('bills-grid');
}
});
This is how i do it, just change the URL, it should be the same controller action of the gridview and change URL parameters to the structure represented in there, should be like Bills[attribute]=value.