Invalid data source source type. The data source must be of type IListSource, IEnumerable or IDataSource - ext.net

I am prety new on MVC app.my problem is that vs gives me an error something like this.
"Invalid data source source type. The data source must be of type IListSource, IEnumerable or IDataSource."
**what I wanna do in this senario is using this sample making pagination with my entitiy.
here is the my view
#(
Html.X().Panel()
.ID("ImagePanel")
.Cls("images-view")
.Frame(true)
.Width(535)
.Collapsible(true)
.Title("URUNLER")
.Items(
Html.X().DataView()
.ID("ImageView")
.Store(
Html.X().Store()
.RemotePaging(true)
.AutoLoad(false)
.ID("Store1")
.PageSize(5)
.DataSource(Model)
.Model(
Html.X().Model()
.IDProperty("ID")
.Fields(
Html.X().ModelField().Name("Ad"),
Html.X().ModelField().Name("Aciklama")
)
)
.Proxy(Html.X().AjaxProxy()
.Url(Url.Action("Read"))
.Reader(Html.X().JsonReader().Root("data").TotalProperty("data.TotalRecords"))
)
)
.EmptyText("")
.TrackOver(true)
.Tpl(
Html.X().XTemplate()
.Html(
#<text>
<tpl for=".">
<div class="thumb-wrap" id="{Ad}">
<div class="thumb"><img src="{Ad}" title="{Ad}"></div>
<span class="x-editable">{Aciklama}</span>
</div>
</tpl>
<div class="x-clear"></div>
</text>
)
)
)
.BottomBar(
Html.X().PagingToolbar().StoreID("Store1").HideRefresh(true)
)
)
and my controlerr
public ActionResult Index()
{
return View();
}
public ActionResult Read(StoreRequestParameters parameters)
{
int TotalRecords;
int start = parameters.Start;
int limit = parameters.Limit;
string sort = "1";
return this.Store(PlantsPaging(start, limit, sort, out TotalRecords));
}
public Paging<Urun> PlantsPaging(int start, int limit, string sort, out int TotalRecords)
{
Urun_BLL urun_bll = new Urun_BLL();
var data = urun_bll.Getir(start, limit, sort, out TotalRecords);
return new Paging<Urun>(data, TotalRecords);
}

Since you are loading the data via an AjaxProxy and are not passing any Model to the View, you should remove this:
.DataSource(Model)
At runtime it is:
.DataSource(null)
And yes, it throws an exception, because it is not something that a Store's DataSource expects.

Related

LARAVEL 9: How to export data to excel based on filter data (id, month and years) using maatwebsite

I'm making an export data to excel based on employee_id filter, month and year of absence:
.
when the filter is submitted the data appears in the table below:
I have managed to get the data, but when the Download Excel button is clicked, the contents are just empty Excel, like this:
the data does not enter the excel.
My Controller:
public function rekapabsensiExcel(Request $request)
{
$idkaryawan = $request->id_karyawan;
$bulan = $request->query('bulan',Carbon::now()->format('m'));
$tahun = $request->query('tahun',Carbon::now()->format('Y'));
// simpan session
$idkaryawan = $request->session()->get('idkaryawan');
$bulan = $request->session()->get('bulan');
$tahun = $request->session()->get('tahun',);
// dd($idkaryawan,$bulan,$tahun );
if(isset($idkaryawan) && isset($bulan) && isset($tahun))
{
$data = Absensi::where('id_karyawan', $idkaryawan)
->whereMonth('tanggal', $bulan)
->whereYear('tanggal',$tahun)
->get();
// dd($data);
}else{
$data = Absensi::all();
}
return Excel::download(new RekapabsensiExport(['data'=>$data, 'idkaryawan'=>$idkaryawan]),'rekap_absensi_bulanan.xlsx');
}
My RekapAbsensiExport.php:
<?php
namespace App\Exports;
use App\Models\Absensi;
use Maatwebsite\Excel\Concerns\FromCollection;
class RekapabsensiExport implements FromCollection
{
protected $id_karyawan;
// function __construct($id_karyawan) {
// $this->id_karyawan = $id_karyawan;
// }
public function headings(): array {
return [
"No. ID","ID Karyawan","NIK","Tanggal","Jam Kerja","Jam Masuk","Jam Pulang",
"Scan Masuk","Scan Pulang","Normal","Riil","Terlambat","Plg Cepat","Absent",
"Lembur","Jml Jam Kerja","pengecualian","Harus C/I","Harus C/O","Departemen",
"Hari Normal","Akhir Pekan","Hari Libur","Jml Kehadiran","Lembur Hari Normal",
"Lembur Akhir Pekan","Lembur Hari Libur"
];
}
/**
* #return \Illuminate\Support\Collection
*/
public function collection()
{
return Absensi::where('id_karyawan',$this->id_karyawan)->get();
}
}
What part did I go wrong? Please help
Currently, you get no response to this statement because $this->id_karyawan is null as you have not properly passed the value through.
return Absensi::where('id_karyawan', $this->id_karyawan)->get();
Above, you are passing an array of values to the Export class. But your commented-out constructor function is only configured to accept a single parameter. If we stick to single parameters, you could do something like this.
return Excel::download(new RekapabsensiExport($data, $idkaryawan),'rekap_absensi_bulanan.xlsx');
Then your export constructor would look like this.
protected $data;
protected $id_karyawan;
function __construct($data, $id_karyawan) {
$this->data = $data;
$this->id_karyawan = $id_karyawan;
}

Laravel allocate LIKE query not giving results

I am using a LIKE query to show results from the Posts table where the category field matches one of the array elements in the Users table's skills field. But it is not giving me any results.
Controller
class TaskerController extends Controller
{
public function index()
{
return view('index');
}
public function tasks()
{
$skills = Auth::user()->skills;
$posts = Post::where('category', 'LIKE', '%'.$skills.'%');
return view('task')->with('posts', $posts);
}
}
You can not concatenate array like a string.
Since your skills is an array use whereIn method
public function tasks(){
$skills = Auth::user()->skills;
$skills = explode (",", $skills); // since you got comma separated skills you need to convert to array
//$skills =['skill1', 'skill2', 'skill3'];
$posts = Post::whereIn('category', $skills );
return view('task')->with('posts', $posts);
}

How does Enumerate work in MonoTouch?

In MonoTouch I need to process each object in an NSSet. My attempt, using Enumerate, is as follows:
public override void ReturnResults ( BarcodePickerController picker, NSSet results )
{
var n = results.Count; // Debugging - value is 3
results.Enumerate( delegate( NSObject obj, ref bool stop )
{
var foundCode = ( obj as BarcodeResult ); // Executed only once, not 3 times
if ( foundCode != null )
{
controller.BarcodeScannedResult (foundCode);
}
});
// Etc
}
Although the method is invoked with three objects in results, only one object is processed in the delegate. I would have expected the delegate to be executed three times, but I must have the wrong idea of how it works.
Unable to find any documentation or examples. Any suggestion much appreciated.
You have to set the ref parameter to false. This instructs the handler to continue enumerating:
if ( foundCode != null )
{
controller.BarcodeScannedResult (foundCode);
stop = false; // inside the null check
}
Here is the ObjC equivalent from Apple documentation.
Or you could try this extension method to make it easier..
public static class MyExtensions {
public static IEnumerable<T> ItemsAs<T>(this NSSet set) where T : NSObject {
List<T> res = new List<T>();
set.Enumerate( delegate( NSObject obj, ref bool stop ) {
T item = (T)( obj ); // Executed only once, not 3 times
if ( item != null ) {
res.Add (item);
stop = false; // inside the null check
}
});
return res;
}
}
Then you can do something like:
foreach(BarcodeResult foundCode in results.ItemsAs<BarcodeResult>()) {
controller.BarcodeScannedResult (foundCode);
}
Note: Keep in mind this creates another list and copies everything to it, which is less efficient. I did this because "yield return" isn't allowed in anonymous methods, and the alternative ways I could think of to make it a real enumerator without the copy were much much more code. Most of the sets I deal with are tiny so this doesn't matter, but if you have a big set this isn't ideal.

SubSonic How to provide a column name in a generic method

Using SubSonic3, I have this generic method (thanks to linq guy, James Curran):
public List<T> GetFromList<T>( List<Guid> _IDs,
Func<T, Guid> GetID,
Func<IQueryable<T>> GetAll )
where T : class, IActiveRecord
{
List<T> rc = null;
var Results =
from item in GetAll( )
where ( _IDs as IEnumerable<Guid> ).Contains( GetID( item ) )
select item;
rc = Results.ToList<T>( );
return rc;
}
It is called with something like
List<Job> jresults = GetFromList( IDList,
item => item.JobID,
( ) => Job.All( ) );
Where IDList is a List of guids that are keys to the table.
When not generic, the linq looks like this and works perfectly. I was quite impressed that SubSonic's linq provider could take this code and turn it into SELECT * FROM Job WHERE JobID IN (a, b, c):
var Results =
from item in Job.All( )
where ( _IDs as IEnumerable<Guid> ).Contains( item.JobID )
select item;
I want to be able to call this method on tables other than Job, with keys other than JobID. The GetAll Func works because it returns the same IQueryable that Job.All( ) does, but GetID throws a run-time exception, "LINQ expression node of type Invoke is not supported". GetID returns a value, but what I really need from it is something that Contains( item.JobID) would recognize as a column name and that the "where" syntax would accept. (I don't show it here, but I have the same problem with orderby.)
Is that possible, with what you know of SubSonic3?
My solution was to pass in the expression that Where needed:
public List<T> GetFromList( List<Guid> _IDs,
Func<IQueryable<T>> GetAll,
Expression<Func<T, bool>> _where )
where T : class, U, IActiveRecord
{
List<T> rc = new List<T>( );
if ( 0 < _IDs.Count )
{
if ( MAX_ITEMS > _IDs.Count )
{
var Results = GetAll( ).Where( _where );
rc = Results.ToList( );
}
else
{
var Results =
from id in _IDs
join item in GetAll( ) on id equals item.KeyValue( )
select item;
rc = Results.ToList( );
}
}
return rc;
}
called by
rc = GetFromList(
IDList,
( ) => Job.All( ),
( item => ( IDList as IEnumerable<Guid> ).Contains( item.JobID ) ) );

Anonymous type and getting values out side of method scope

I am building an asp.net site in .net framework 4.0, and I am stuck at the method that supposed to call a .cs class and get the query result back here is my method call and method
1: method call form aspx.cs page:
helper cls = new helper();
var query = cls.GetQuery(GroupID,emailCap);
2: Method in helper class:
public IQueryable<VariablesForIQueryble> GetQuery(int incomingGroupID, int incomingEmailCap)
{
var ctx = new some connection_Connection();
ObjectSet<Members1> members = ctx.Members11;
ObjectSet<groupMember> groupMembers = ctx.groupMembers;
var query = from m in members
join gm in groupMembers on m.MemberID equals gm.MemID
where (gm.groupID == incomingGroupID) && (m.EmailCap == incomingEmailCap)
select new VariablesForIQueryble(m.MemberID, m.MemberFirst, m.MemberLast, m.MemberEmail, m.ValidEmail, m.EmailCap);
//select new {m.MemberID, m.MemberFirst, m.MemberLast, m.MemberEmail, m.ValidEmail, m.EmailCap};
return query ;
}
I tried the above code with IEnumerable too without any luck. This is the code for class VariablesForIQueryble:
3:Class it self for taking anonymouse type and cast it to proper types:
public class VariablesForIQueryble
{
private int _emailCap;
public int EmailCap
{
get { return _emailCap; }
set { _emailCap = value; }
}`....................................
4: and a constructor:
public VariablesForIQueryble(int memberID, string memberFirst, string memberLast, string memberEmail, int? validEmail, int? emailCap)
{
this.EmailCap = (int) emailCap;
.........................
}
I can't seem to get the query result back, first it told me anonymous type problem, I made a class after reading this: link text; and now it tells me constructors with parameters not supported. Now I am an intermediate developer, is there an easy solution to this or do I have to take my query back to the .aspx.cs page.
If you want to project to a specific type .NET type like this you will need to force the query to actually happen using either .AsEnumerable() or .ToList() and then use .Select() against linq to objects.
You could leave your original anonymous type in to specify what you want back from the database, then call .ToList() on it and then .Select(...) to reproject.
You can also clean up your code somewhat by using an Entity Association between Groups and Members using a FK association in the database. Then the query becomes a much simpler:
var result = ctx.Members11.Include("Group").Where(m => m.Group.groupID == incomingGroupID && m.EmailCap == incomingEmailCap);
You still have the issue of having to do a select to specify which columns to return and then calling .ToList() to force execution before reprojecting to your new type.
Another alternative is to create a view in your database and import that as an Entity into the Entity Designer.
Used reflection to solve the problem:
A: Query, not using custom made "VariablesForIQueryble" class any more:
//Method in helper class
public IEnumerable GetQuery(int incomingGroupID, int incomingEmailCap)
{
var ctx = new some_Connection();
ObjectSet<Members1> members = ctx.Members11;
ObjectSet<groupMember> groupMembers = ctx.groupMembers;
var query = from m in members
join gm in groupMembers on m.MemberID equals gm.MemID
where ((gm.groupID == incomingGroupID) && (m.EmailCap == incomingEmailCap)) //select m;
select new { m.MemberID, m.MemberFirst, m.MemberLast, m.MemberEmail, m.ValidEmail, m.EmailCap };
//select new VariablesForIQueryble (m.MemberID, m.MemberFirst, m.MemberLast, m.MemberEmail, m.ValidEmail, m.EmailCap);
//List<object> lst = new List<object>();
//foreach (var i in query)
//{
// lst.Add(i.MemberEmail);
//}
//return lst;
//return query.Select(x => new{x.MemberEmail,x.MemberID,x.ValidEmail,x.MemberFirst,x.MemberLast}).ToList();
return query;
}
B:Code to catch objects and conversion of those objects using reflection
helper cls = new helper();
var query = cls.GetQuery(GroupID,emailCap);
if (query != null)
{
foreach (var objRow in query)
{
System.Type type = objRow.GetType();
int memberId = (int)type.GetProperty("MemberID").GetValue(objRow, null);
string memberEmail = (string)type.GetProperty("MemberEmail").GetValue(objRow, null);
}
else
{
something else....
}

Resources