Default value of Grocery CRUD datetime input? - codeigniter

This answer is great! everything works fine with the datepicker.
But I have a problem with datetime-picker. as I see from another datetime field (without default value), the class used by that field defined as datetime-input.
So I tried to use :
$crud->set_js_config('assets/grocery_crud/js/jquery_plugins/config/jquery.datetime.config.js');
...
<script type="text/javascript">var js_date_format = "dd/mm/yyyy HH:mm:ss"; </script>
<input name="date" type="text" value="'.date("d/m/Y H:i:s").'" class="datetime-input" />
<a class="datetime-input-clear ui-button " tabindex="-1" role="button">Clear</a>
even i've tried using class "timepicker" :
<input name="date" type="text" value="'.date("d/m/Y H:i:s").'" class="datetime-input timepicker" />
but the jquery datetime picker function is not available. Any idea how to make it works? Thank you

try to use callback field values.
$crud->callback_add_field('date_feild_name',array($this,'set_default_date'));
public function set_default_date(){
$value = date("d/m/Y");
$return = '<input id="field-date_feild_name" name="date_feild_name" type="text" value="'.$value.'" maxlength="10" class="datepicker-input hasDatepicker">
<a class="datepicker-input-clear" tabindex="-1">Date feild name</a> (dd/mm/yyyy)';
return $return;
}

Related

datepicker not visible with the date in edit form

I am trying to do a editForm , which is getting all the values correctly from the database. The problem is that either the date is visible or just datepicker. The 'View Page Source is showing the correct value'
This is how I am displaying the property in Model
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime DateOfBirth { get; set; }
I have tried both the input tag and the #Html tag and both give different results
With just the input tag it just shows the datepicker with dd/mm/yyyy
<div class="form row">
<div class="form-group col-md-6">
<label asp-for="DateOfBirth" class="col-auto col-form-label small form-text">Date Of Birth</label>
<div class="col">
<input asp-for="DateOfBirth" class="form-control ui-datepicker" type="date" placeholder="Date of Birth" value="#Model.DateOfBirth.ToShortDateString()" asp-format="dd/mm/yyyy" />
and with the #Html it just shows the correct date but not datepicker
#*#{
string parameterValue = #Model.DateOfBirth.ToShortDateString();
}
#Html.TextBoxFor(m => parameterValue, new { #class = "form-control ui-datepicker" })*#
<span asp-validation-for="DateOfBirth"></span>
</div>
</div>
How can I get both the date and datepicker displayed in the edit form?
just the input tag it just shows the datepicker with dd/mm/yyyy
This is because you specified the type of input as date.
type="date"
About <input type="date">,you need to know:
The displayed date format will differ from the actual value — the displayed date is formatted based on the locale of the user's browser, but the parsed value is always formatted yyyy-mm-dd.
Therefore, when your input value is not in yyyy-mm-dd format, it will not be displayed correctly.
input elements of type="date"
How can I get both the date and datepicker displayed in the edit form?
I suggest you combine jquery Datepicker to achieve your needs.
I wrote an example, you can refer to it.
You need to reference the jquery-ui.css and jquery-ui.js files.
You can install jqueryui by right-clicking wwwroot and selecting Add->Client-Side Library.
Model
public class TestDate
{
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime DateOfBirth { get; set; }
}
Controller
public IActionResult Index()
{
TestDate model = new TestDate { DateOfBirth=DateTime.Now};
return View(model);
}
View
#model WebApplication24.Models.TestDate
<link href="~/jqueryui/jquery-ui.css" rel="stylesheet" />
<input asp-for="DateOfBirth" type="text" class="form-control" placeholder="Date of Birth" />
#section scripts{
<script src="~/jqueryui/jquery-ui.js"></script>
<script>
$(function () {
$("#DateOfBirth").datepicker({ dateFormat: 'dd/mm/yy' });
});
</script>
}
Result
Changed the input to this and it solved the issue
`<input asp-for="DateOfBirth" class="form-control ui-datepicker" type="date"
placeholder="Date of Birth" value="#Model.DateOfBirth.ToString("yyyy-MM-dd")" />

Pass a date input value from View to Controller using Codeigniter

I am trying to give the user to select the date he wants, and it will display the reservations for the day he selected. I am using Codeigniter, the MVC framework.
How am I setting a default value of the current date for an input date selection?
How am I sending the value to the controller and then I am using it for a function in the model.
<form id="form_reDate" name="form_reDate" method="POST" action="form_reDate.php">
<?php $r_date=#date('d-m-y'); ?>
<label for="reservations">Display reservations for :
<input type="date" id="date" name="re_date" style="margin-left: 0;"
min="2020-01-01" max="2020-12-31" value="<?php $r_date?>">
</label>
</form>
Controller:
public function form_reDate(){
$newDate = date("Y-m-d",strtotime($this->input->post('re_date')));
$data['tables'] = $this->Hosting_model->get_tables($newDate);
}
If you're using the default html of input type="date" to set its value you only use this php code.
<?php echo date('Y-m-d') ?>
to sum it:
<input type="date" id="date" name="re_date" style="margin-left: 0;"
min="2020-01-01" max="2020-12-31" value="<?php echo date('Y-m-d') ?>">
and for question #2 on how to send your input value to your Controller. Just make sure you get the data before sending it to your Model.

How to set default value in thymeleaf th:field

I have a form and I want to set default value in the field below but it's not working.
<span>ID User:</span>
<input type="text" th:value="${session.name}" th:field="*{userid}" th:errorclass="field-error" />
</div>
<div>
<span class="name-in">ID Room:</span>
<input type="text" th:value="${id}" th:field="*{room}" th:errorclass="field-error" />
</div>
I read some topic about this problem and I try to change as given below
th:attr="value = ${session.name}"
But It's still not working. Field ID User is empty. I don't know how to solve this problem.
Although your question contain less information, but i think you want to put default value for all field. If you like to do so change
`<input type="text" th:value="${session.name}" th:field="*{userid}" th:errorclass="field-error" />`
to
<input type="text" name="userid" value="as your wish" th:errorclass="field-error" />
Instead of changing the html, you should instead set the value of *{userid} in your controller. That way you can keep your html the same:
// Controller
modelObject.setUserId(session.name);
// HTML
<input type="text" th:field="*{userid}" th:errorclass="field-error" />

Parsing data from blade form to controller using request

I want to parsing my label name="predictDataTemp" in form into my controller, I already set the value form my label, but when I want to request the data still null
content.blade.php
<div class="form-group" align="center">
<label for="exampleResult" name="result">Result</label>
<label for="examplePredict" id="predictData" class="form-control">
<input type="hidden" name="predictDataTemp">
</label>
</div>
controller
public function result(Request $request){
$this->validate($request,[
'mCalories'=>'required',
'mCholesterol'=>'required',
'mFat'=>'required',
'mProtein'=>'required',
'mSugars'=>'required'
]);
$item= array();
array_push($item,array('Calories'=>$request->mCalories,'Cholesterol'=>$request->mCholesterol,'Fat'=>$request->mFat,'Protein'=>$request->mProtein,'Sugars'=>$request->mSugars,'Predict'=>$request->predictDataTemp));
return json_encode($item);
}
Your input has no value.
If you want to give it a value with jQuery (looking at your previous comments)
Give the input an id
<input type="hidden" name="predictDataTemp" id="predictDataTemp">
Then assign it in jQuery
$('#predictDataTemp').val('pass value here');
label don't have name attribute, it has only two attribute for and form so you can pass value in hidden input tag, Read this article
Instead
<label type="text" for="examplePredict" id="predictData" name="predictDataTemp" class="form-control"></label>
Use this
<label type="text" for="examplePredict" class="form-control"></label>
<input type="hidden" name="predictDataTemp" id="predictData" value="something">

How to make dynamic url-pattern using Spring MVC #PathVariable?

I'm working that make search function.
I have a search form on jsp view.
<form action="<c:url value="/community/board/list/search" />">
<p class="serch_Area">
<select name="searchCategory">
<option value="subject" selected="selected">subject</option>
<option value="contents">contents</option>
</select>
<input type="text" name="searchWord" class="inputCom" style="width:150px; height:17px;" value="" maxlength="15" />
<input type="image" src="<c:url value="/resources/images/common/btn_search.gif" />" alt="search" />
</p>
</form>
I want to make request url-pattern like "/community/board/list/search/subject/abc". But this form action url like "/community/board/list/search?subject=abc"
How can I make request url pattern like RESTful?
This is my controller.
#RequestMapping("/list/search/{searchCategory}/{searchWord}/{pageNum}")
public String getSearchList(#PathVariable(value = "searchCategory") String searchCategory,
#PathVariable(value = "searchWord") String searchWord,
#PathVariable(value = "pageNum") int pageNum, ModelMap model) {
Please help me.
You can do this with JavaScript. You should add an onSubmit listener on your form and change your form action within that method.

Resources