How can I get the ID of selected order in the list of orders Prestashop? - prestashop

I am using Prestashop for an e-comercial web site. I want to export orders into an excel file. For that I added a button into the order by adding these lines
{block name=preTable}
<div><button type="button">Exporter Excel!</button>
<button type="button">Exporter PDF </button></div>
{/block}
into the file \admin\themes\default\template\controllers\orders\helpers\list. To execute the necessary query I need to have the id of the selected orders, but I really don't know how I can get it.

I guess you are using Prestashop 1.6 version.
You don't need any modifications. You can filter the orders you want to export (by date or other parameters) with Search and then press the Export button.

Checkboxes with the name "orderBox[]" contain ID value of the order. Every row that is selected will have row ID in array orderBox. So in your post method you access it through:
$orders = Tools::getValue('orderBox[]');
foreach ($orders as $order_id) {
// do something with ids
}

Related

Datatables: Create a footer on the fly

In my project, the number of columns in main table depends on the user settings and I get it from AJAX to dataTable directly. Several of these columns show different amounts (eg pre-price, final-price, etc.). Now, I have a task to calculate these pices to total sums in the footer.
I already know in which columns what amounts and the ordinal number of these columns for a every user. The simplest thing left — to put these sums in the footer. To do this, I first need to create the required number of footer columns. Simple task, but!
I use a drawCallback method *:
drawCallback {
string = innerHTML(needNode).repeat(x_times)
...
}
...and my footer is works.
Then I calculated the page total and I did
window.setTimeout(function(){
$( api.column( columnNumber ).footer() ).html(pageTotal);
},3000);
...but I didn't see any result in empty footer...
What am I doing wrong?
*I cannot use the footerCallback method, because it contain no required object for counting columns.
**If I create a footer manually in HTML, then the right result will be displayed in the correct column. But the footer will be displayed twice: my "tfoot" and automatically created table "dataTables_scrollFoot".
I used the setTimeout() for enough time to create the footer. It works, but does no need effect :(
Help me please. Any help would be appreciated.
...........

Vuetify Data Table jump to page with selected item

Using Vuetify Data Tables, I'm trying to figure out if there's a way to determine what page the current selected item is on, then jump to that page. My use case for this is, I'm pulling data out of the route to determine which item was selected in a Data Table so when a user follows that URL or refreshes the page that same item is automatically selected for them. This is working just fine, however, I can't figure out how to get the Data Table to display the correct page of the selection.
For example, user visits mysite.com/11
The Data Table shows 10 items per page.
When the user enters the site, item #11 is currently auto-selected, but it is on the 2nd page of items. How can I get this to show items 11-20 on page load?
I ended up using a solution similar to what #ExcessJudgement posted. Thank you for putting that code pen together, BTW! I created this function:
jumpToSelection: function(){
this.$nextTick(() => {
let selected = this.selected[0];
let page = Math.ceil((this.products.indexOf(selected) + 1) / this.pagination.rowsPerPage);
this.pagination.sortBy = "id";
this.$nextTick(() => {
this.pagination.page = page;
});
});
}
I'm not sure why I needed to put this into a $nextTick(), but it would not work otherwise. If anybody has any insight into this, it would be useful to know why this is the case.
The second $nextTick() was needed because updating the sortBy, then the page was causing the page to not update, and since I'm finding the page based on the ID, I need to make sure it's sorted properly before jumping pages. A bit convoluted, but it's working.

Yii2 Gridview get all selected row for all pagination

I wrapped my gridview with Pjax widget like this
\yii\widgets\Pjax::begin();
gridview
\yii\widgets\Pjax::end();
in order to make the gridview make ajax request when I click on each pagination.
I also use ['class' => 'yii\grid\CheckboxColumn'], in column as well.
and I find that when I'm on first pagination I checked some rows and then go to second page and check some rows but when I go back to first page what I've checked is gone.
My question is how can I keep all checkedrow for all pagination
With current conditions (Pjax, multiple pages, yii\grid\CheckboxColumn) it's impossible because of the way it works.
When you click on the pagination links all GridView html content is replaced by new one that comes from the AJAX response.
So obviously all selected checkboxes on the previous page are gone.
Few possible ways to solve that:
1) Write custom javascript and server side logic.
As one of the options, you can send AJAX request to server with parameter meaning that user has chosen to select all data for the bulk delete operation (or use separate controller action for bulk deletion). In this case actually we don't need to get the selected data from user, because we can simply get them from database (credits - Seng).
2) Increase number of displayed rows per page.
3) Use infinite scroll extension, for example this.
4) Break desired action in several iterations:
select needed rows on first page, do action (for example, delete).
repeat this again for other pages.
You can get selected rows like that:
$('#your-grid-view').yiiGridView('getSelectedRows');
[infinite scroll] : http://kop.github.io/yii2-scroll-pager/ will work good if you do not have any pjax filters. If you have filters also in play, do not use this plugin as it does not support pjax filters with it. For rest of the applications it is perfect to use.
Update1 : it seems to be straight forward than expected, here is the how I accomplished it
Add following lines to the checkbox column
'checkboxOptions' => function($data){
return ['id' => $data->id, 'onClick' => 'selectedRow(this)'];
}
Now add following JS to the common js file you will have in your project of the page where this datagrid resides
var selectedItems=[]; //global variable
/**
* Store the value of the selected row or delete it if it is unselected
*
* #param {checkbox} ele
*/
function selectedRow(ele){
if($(ele).is(':checked')) {
//push the element
if(!selectedItems.includes($(ele).attr('id'))) {
selectedItems.push($(ele).attr('id'));
}
} else {
//pop the element
if(selectedItems.includes($(ele).attr('id'))) {
selectedItems.pop($(ele).attr('id'));
}
}
}
Above function will store the selected row ids in the global variable array
Now add following lines to pjax:end event handler
$(document).on('pjax:end', function () {
//Select the already selected items on the grid view
if(!empty(selectedItems)){
$.each(selectedItems, function (index,value) {
$("#"+value).attr('checked',true);
});
}
});
Hope it helps.
I just solved this problem and it works properly with Pjax.
You may use my CheckboxColumn. I hope this can help. The checked items are recorded with cookies.
You can read the part with //add by hezll to understand how to fix it, because I didn't provide a complete general one.
Hope it works for you.
https://owncloud.xiwangkt.com/index.php/s/dGH3fezC5MGCx4H

MVC4: dynamically change route value from dropdown list selection

I have a dropdown list that serves as a record navigation control -- selecting a value from the dropdown is supposed to "jump to" that record. I feel like I've done stuff like this before that worked, but I can't get this one to work. The issue seems to be that I can't get the dropdown list to change the ID route value that the page was initially called with. So let's say my page is called from this URL:
/PatientProfile/Services/12
12 is the ID route value here--this is essentially the initial record displayed. This works. However, when I select something from my dropdown, it will redirect to something like this:
/PatientProfile/Services/12?ID=7
Notice how the 12 is still there in the route value ID. ID 7 was selected from the dropdown, but it's appended to the URL as a new parameter instead of the route value. What I want to happen is this:
/PatientProfile/Services/7
Here's what the razor looks like for my dropdown:
#using (Html.BeginForm("Services", "PatientProfile", FormMethod.Get))
{
#Html.Label("ID", "View Profile:")
#Html.DropDownListFor(model => model.CurrentProfile.ID, ViewBag.ProfileID as SelectList, new { onchange = "this.form.submit();" })
}
I tried both Html.DropDownList and Html.DropDownListFor, but saw no difference in behavior.
Any help greatly appreciated.
I would use jquery for this. Please confirm the generated id of the dropdownlist for this to work properly
$('#CurrentProfile_ID').change(function(){
window.location('#Url.Action("Services", "PatientProfile", new { id = "----" })'.replace("----", $('#CurrentProfile_ID :selected').val()));
});
Hopefully this helps.
PS. This sounds like a perfect situation for using a partial view that you update using an ajax call so you don't have to post back.

Yii: Receiving multiple records for a single model from view in POST method: To save Parent Child data

I am in a situation where I have more than 4 child tables associated with one Parent table. I need to create a user experience in which user presses Save button only once, meaning by, user enters all the data in parent model fields, then enters data in all four child model fields and then presses the save button. As far as I know, having relations in the model allows you to make associated rows inserted easily but the main problem is how to receive multiple rows from view in POST method for a single model (here I essentially mean the child models). I have tried it manually by repeating the attributes of child model in view but when I save the record, only the last rowset gets stored in the child table along with parent table, one row for the child table gets missed. Kindly note that I am using CActiveForm and other Bootstrap widgets in my View files.
Is it possible in Yii or I am too wishful....any suggestions or comments ????
Many thanks in advance.
Regards,
Faisal
I got the solution but with all the help from here and also from other forums. I followed the post by Kiran and tested it by generating additional HTML attributes using jQuery. On the submit, I got all the rows exactly how I wanted. In the controller, first I counted the total number of models submitted in the post request and then iterated over each one for desired processing. Following is the code snippet.
if(!empty($_POST))
{
$v=count($_POST['Address'])+1;
Yii::log(count($_POST['Address']));
for ($i=1; $i<$v; $i++){
$addressModel_1->attributes=$_POST['Address'][$i];
Yii::log('Dumping Data from '.$i.' model');
Yii::log($addressModel_1->city);
Yii::log($addressModel_1->street);
Yii::log($addressModel_1->state);}}
On the view side, I generated the HTML using jQuery function. All this function did was to add another set of html to allow the user to enter data. Important thing to note while generating the HTML is the name of model or else it wouldn't land where you want in controller.
Following is the code snippet of this function. Please note that I am hardcoding the "3" as id since I already had two sets of rows in DOM. I am going to further improve this code but rest assured, the logic works.
function createNewAddress(){
var newdiv = document.createElement('div');
var inner_html='<div class="row">';
inner_html+='<label for="Address_3_street">Street</label> <input name="Address[3][street]" id="Address_3_street" type="text" maxlength="200" /> </div>';
inner_html+='<div class="row">';
inner_html+='<label for="Address_3_city">City</label> <input name="Address[3][city]" id="Address_3_city" type="text" maxlength="200" /> </div> ';
inner_html+='<div class="row">';
inner_html+='<label for="Address_3_state">State</label> <input name="Address[3][state]" id="Address_3_state" type="text" maxlength="200" /> </div>';
newdiv.innerHTML=inner_html;
$('#user-form').append(newdiv);
}
This way, I can have n-number of child rows added on the fly from browser and user will save all data by pressing the Save or Submit button only once.
Thanks to everyone for their support.
Regards,
Faisal
You should be using tabular input, that way you can receive data for multipe instances of the same type, you can then save the parent and use its id to fill the child foreign keys.
You could make a new CFormModel to handle all the form fields validation, and then manually set the attributes after the $model->validate in the POST function. EG:
if ($model->validate){
$model_one = new ModelOne;
$model_one->name = $model->model_one_name;
$model_one->surname = $model->model_one_surname;
....
$model_two = new ModelTwo;
$model_two->name = $model->model_two_name;
$model_two->surname = $model->model_two_surname;
....
}