Countries drobdown list dynamic data
Countries drobdown list dynamic data
hanyelbana
Posts: 24Questions: 0Answers: 0
Hi,
I have countries table : id, country
and towns table : id, town, country_id
With town datatable I display a town and a country which related to by join select.
For add and edit I fill countries dropdown list by separate selection from countries table.
Tha problem is the add or update action tor town return the row with town, country_id data but the datatable is built to show country name from the join select by JSON. I get error and after accept the datatable comes without country name but after reload the join select get country name.
How can I solve this?
Thanks
I have countries table : id, country
and towns table : id, town, country_id
With town datatable I display a town and a country which related to by join select.
For add and edit I fill countries dropdown list by separate selection from countries table.
Tha problem is the add or update action tor town return the row with town, country_id data but the datatable is built to show country name from the join select by JSON. I get error and after accept the datatable comes without country name but after reload the join select get country name.
How can I solve this?
Thanks
This discussion has been closed.
Replies
{
$towns = DB::table('countries')
->join('towns', 'countries.id', '=', 'towns.country_id')
->select('towns.id', 'towns.name', 'towns.country_id', 'countries.name as country')->get();
return $towns;
}
public function store()
{
if(Input::has('data'))
{
$input = Input::get('data');
$town = new Town;
$town->country_id = $input['country_id'];
$town->name = $input['name'];
$town->save();
return Response::json(array(
'id' => $town->id,
'row' => $town->toArray()
),
200
);
}
}
public function create()
{
$towns = DB::table('countries')
->join('towns', 'countries.id', '=', 'towns.country_id')
->select('towns.id', 'towns.name', 'towns.country_id', 'countries.name as country')->get();
return $towns;
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
if(Input::has('data'))
{
$input = Input::get('data');
$town = new Town;
$town->country_id = $input['country_id'];
$town->name = $input['name'];
$town->save();
return Response::json(array(
'id' => $town->id,
'row' => $town->toArray()
),
200
);
}
}
[/code]
function loader(){
var test = [];
$.ajax({
url: 'list/countries',
async: false,
dataType: 'json',
success: function (json) {
$.each(json, function(key, val) {
obj= { "label" : val.country, "value" : val.country_id };
test.push(obj);
});
}
});
return test;
}
var editor; // use a global for the submit and return data rendering in the examples
$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
"ajaxUrl": {
"create": "towns",
"edit": "PUT towns/_id_",
"remove": "DELETE towns/_id_"
},
"domTable": "#towns",
"dbTable": "towns",
"idSrc": "id",
"fields": [
{
"label": "الدولة:",
"name": "country_id",
"type": "select",
"ipOpts": loader()
},
{
"label": "المدينة:",
"name": "name"
}
],
@include('layout.editortranserror')
"events": {
"onPreSubmit": function ( o ) {
if ( o.data.name === "" ) {
this.error('name', 'يجب إدخال اسم المدينة');
return false;
}
// else if ( o.data.browser.length >= 10 ) {
// this.error('browser', 'The browser name length must be less that 10 characters');
// return false;
// }
// ... etc
}
}
} );
$('#towns').dataTable( {
"sDom": "<'row-fluid'<'span6'T><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
"sAjaxSource": "towns/create",
"sAjaxDataProp": "",
"aoColumns": [
{ "mData": "id" },
{ "mData": "country" },
{ "mData": "name" }
],
"oTableTools": {
"sRowSelect": "single",
"aButtons": [
{ "sExtends": "editor_create", "editor": editor },
{ "sExtends": "editor_edit", "editor": editor },
{ "sExtends": "editor_remove", "editor": editor }
]
},
@include('layout.editortrans')
} );
} );
[/code]