I'm trying to do this:
Open a bootstrap dialog with a button, through a URL. Action of the Url in controller is "myController/updateDialog".
By open dialog, this loads a page (myController/updateDialog) with a form to fill data (form has a button to submit).
If I submit the form by clicking the button, in my controller I tried close the dialog and refresh some things in the original page, this I do with JS. But it doesn't work. I always get the window navigator in the blank with the new URL; I expect to keep the original page where the dialog was invoked, to see the changes, same way that does CJUIDIALOG in Yii 1.x.
The controller's code is:
public function actionUpdatedialog($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post() && $model->save() )) {
$this->view->registerJs("window.close(); window.opener.fn.yiiGridView.update('books-heads'); return false;",
$this->view::POS_HEAD);//try close and finally
die(); //finally
}
return $this->render('update', [
'model' => $model,
]);
}
I tried, with :
"window.close();" and
"$(#Mymodal).modal('hide');" and others, but could not sort it out.
You should submit your form via ajax, accept and determine the return value. If it is successful, call $("#your modal box").modal('hide') to close your dialog. e.g:
public function actionUpdatedialog($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post() && $model->save() )) {
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return ['code' => 200, 'message' => 'success'];
}
return $this->render('update', [
'model' => $model,
]);
}
In the page:
$("#formName").on('submit', function () {
// or use ajax
fetch(...).then(response => {
response.json().then(json => {
if (200 === json.code) {
$("yourModalBox").modal('hide');
} else {
alert(json.message);
}
});
});
});
Answer translation from Google Translate, hope this helps you.
Related
the controllers actions of my yii2 application render and validate/save http input data. The first time (when the route was requested) it renders the form but with errors as follows:
I need to render form in the first time without error label. Is there way to solve this using the same action to render and save ?
This is my code:
public function actionCreate()
{
$model = new CourseForm;
$model->attributes = Yii::$app->request->post('CourseForm');
if ($model->validate()) {
// save...
} else {
return $this->render( 'create', [ 'model' => $model, 'errors' => $model->errors ]);
}
}
Because you are loading the attribute and calling the validate() every time the action is called and when the validation fails the view loads with the errors highlighted. So that pretty much makes sense why is it doing like that.
Secondly you can just use $model->load() and supply the post() array to it it will load the attributes automatically.
Your code can be reduced to the following and it will not show the error labels when the page loads untill unless you submit the form
public function actionCreate()
{
$model = new CourseForm();
if($model->load(Yii::$app->request->post()) && $model->validate()){
// save...
}
return $this->render('create', ['model' => $model, 'errors' => $model->errors]);
}
i have a problem, after passing the data model in another view with form and displayed, I would like to save them in db in the new view with button submitt
My controller
public function actionOffri()
{
$model = new Viaggi;
if($model->load(Yii::$app->request->post()) && $model->validate()){
// $model->save();
$request = Yii::$app->request;
$params = $request->bodyParams;
Yii::$app->session->setFlash('success', 'succes data');
return $this->render('preview', ['params' => $params]);
}else {
Yii::$app->getSession()->setFlash('error', 'error data');
return $this->render('offri', ['model' => $model]);
}
}
I haven't created an action PREVIEW but read data with pass params,
and I would save the data in the database with another button in view PREVIEW
An easy approach would be to create a form in the Preview view with all the data of $params inside of hidden fields, and after that add another button to submit the hidden form. Another way would be to save the data in the session and retrieve it when you needed.
Hope this helps
Laravel POST return type?
I'm setting up an API alpha version using basic auth single sign on.
Cant debug with postman/js because its not returning JSON but redirecting me instead. From what Ive read AJAX calls shouldnt be redirected? Do I have errors in the configuration?
Routes:
Route::post('/test1', 'tradesCtrl#test1']);
//Route::post('/test1', ['middleware' => 'auth.basic.once', 'uses' => 'tradesCtrl#test1']);
AJAX call:
$.post(
"http://localhost:8000/test1",
{
p1:"100a",
p2:80
},
function(data, status,jqXHR){
console.log(data);
});
Controller (this will echo HTTP (!):
public function test1(Request $request)
{
if($request->ajax()) {
echo "AJAX!";
} else {
echo "HTTP!";
}
}
Allow cross domain for now in App.php
// allow origin
header('Access-Control-Allow-Origin: *');
Kernel.php disable csrf protection by commeting out
app\Http\Middleware\VerifyCsrfToken::class
If I try to insert validation for parameters
public function test1(Request $request)
{
$this->validate($request, [
'p1' => 'Integer',
'p2' => 'Integer'
]);
echo "Serving stuff";
}
This immediately return in a 404 page not found when failing validation, probably a redirect to something else that's not working??
I have some data that loads into a Kendo grid via the Ajax binding.
Within one of the columns there's a ClientTemplate that calls a javascript method (showAll).
This method will call an action and get the details of the data, putting it into a json response, and then open a jquery-ui dialog to show the details.
When the user clicks on the link in the grid the HttpGet is triggered for the GetDetails action BUT, the problem is, it is also triggered for the entire page's action (Index).
The question, I guess, is what is causing the Index action to be triggered? Because, the dialog will show, the detailed data will populate, but once I close the dialog all the filter textboxes will be reset and the grid will reload and the data within it.
Shouldn't the only action called be the GetDetails?
Any hints will be greatly appreciated!
Code:
#(Html.Kendo().Grid<LogViewModel>()
.Name("LogGrid")
.Columns(column =>
{
column.Bound(x => x.StuffCount).Title("Stuff").Width(70)
.ClientTemplate("<a onclick=\"showAll('" + "#= Id #')\"" + " href=''>#= StuffCount #</a>");
})
.DataSource(dataBinding => dataBinding
.Ajax()
.PageSize(50)
.Read(read => read.Action("GetData", "Summary")
.Data("getSearchFilters"))
.Model(model => model.Id(o => o.Id)))
.Events(e => e
.DataBound("onGridItemsDatabound"))
.Pageable(paging => paging.Refresh(true))
)}
<div id="dialog-message" title="" style="display: none">
<p id="msg"></p>
</div>
<script type="text/javascript">
var showAll= function (id) {
var url = '#Url.Action("GetDetails", "Summary")' + "/" + id;
var sTitle = 'title text';
$.getJSON(url, null,
function (data) {
$("#dialog-message").dialog({ title: sTitle });
$("#msg").text(data.details);
showMessage();
});
};
var showMessage = function () {
$("#dialog-message").dialog({
modal: true,
draggable: false,
resizable: false,
buttons: {
Ok: function() {
$(this).dialog("close");
}
}
});
};
</script>
The controller methods (content removed for brevity
public ActionResult Index(...)
{
...
}
public ActionResult GetDetails(Guid id)
{
... (get data from repository)
return Json(data, JsonRequestBehavior.AllowGet);
}
I posted the same question on the Telerik forum. Their admin pointed me in the right direction:
http://www.kendoui.com/forums/mvc/grid/kendo-ui-grid---clienttemplate-calling-mvc-url-action-calls-(incorrectly)-two-different-actions.aspx
Which "href" value should I use for JavaScript links, "#" or "javascript:void(0)"?
Turns out I had to add the void to the href to call the javascript and stay on the page.
href="javascript:void(0)"
I'm using a listener in the background page to know when a tab is loaded:
chrome.tabs.onUpdated.addListener(function (tabId) { })
But the listener is fired twice: when the page has started loading, and when the page has finished.Is there a way to differentiate the two cases?
Luckily have found the solution.
There is an additional parameter that holds the status value:
chrome.tabs.onUpdated.addListener(function (tabId , info) {
if (info.status === 'complete') {
// your code ...
}
});
Status can be either loading or complete.
I wanted a easier way to do this after opening a tab
function createTab (url) {
return new Promise(resolve => {
chrome.tabs.create({url}, async tab => {
chrome.tabs.onUpdated.addListener(function listener (tabId, info) {
if (info.status === 'complete' && tabId === tab.id) {
chrome.tabs.onUpdated.removeListener(listener);
resolve(tab);
}
});
});
});
}
so it would be
let tab = await createTab('http://google.com');